Shop OBEX P1 Docs P2 Docs Learn Events
Help With a Script (Calculating Joystick to servos) — Parallax Forums

Help With a Script (Calculating Joystick to servos)

EverettEverett Posts: 1
edited 2013-11-13 23:27 in BASIC Stamp
Could someone give ma a hand with this script?
i have a joystick that i want to operate two servos on a boe-bot,
this is the readings i get from the joystick
UD Max 54 Center 27 min 1
RL Max 54 Center 26 min 1

This is a script i found in this thread



' {$STAMP BS2}
' {$PBASIC 2.5}

' Running 2 servos with a joystick using RCTIME to record the pot's values.
' Make sure that the JoyStickRCTimeRecorder.bs2 has been run and you have recorded
' the Values for the Max and Min Pot on the Joystick.
'
' Calculate the RCUDMin and RCUDMax scaling using your numbers that you got
' Use this formula to insert it into the Variables settings for TimeUDMin, TimeUDMax, TimeRlMin, TimeRLMax
' TimeMax: YourMaxReadng x (500 / YourMaxReading)= YourMaxReading x YourMinReading
' TimeMin: YourMinReading x 500 / YourMaxReading scaling and Offset
' Do this for each Pot to start your Sc
' by GWJax

'=================================================================================
'
Set variables and Pins
'=================================================================================

RTS PIN 12 ' For Right Servo
LTS PIN 13 ' Set Left Servo
RCUD PIN 7 ' Set Up Down Pot to pin 7
RCRL PIN 8 ' Set Right Left Pot to pin 8
ScaleUD CON 185 ' Scale by 0.724 (X 256 for */) 0.724 is from your calculations for Scale Right
' Change 185 to your correct calculation
ScaleRL CON 185 ' Scale for left servo
Offset CON 500 ' Offset by 500
TimeUD VAR Word ' Records value for UD RC results
TimeRL VAR Word ' Records value for RL RC results
CenterUD VAR Word ' Records Center Value
CenterRL VAR Word ' Records Center Value

'=================================================================================
'
Main Routine
'=================================================================================

LOW RCUD ' Discharge Cap
LOW RCRL ' Discharge Cap
' GOSUB JoyStickCenter
IF TimeUD = 1 OR TimeUD = 0 THEN GOTO Bug ' Stop program POT failed
IF TimeRL = 1 OR TimeRL = 0 THEN GOTO Bug ' Stop program POT failed

Main:
DO
GOSUB PotRCTime
TimeUD = TimeUD */ ScaleUD
TimeUD = TimeUD + Offset
TimeRL = TimeRL */ ScaleRL
TimeRL = TimeRL + Offset
PULSOUT RTS, TimeUD ' Send pulses to servo.
PULSOUT LTS, TimeRL
PAUSE 18
LOOP

'=================================================================================
'
Get Joystick Center Values
'=================================================================================

JoyStickCenter:

HIGH RCUD
PAUSE 1 ' Charge Cap
RCTIME RCUD, 1, CenterUD ' Get Recording for TimeUD
LOW RCUD ' Discharge UD Cap
HIGH RCRL
PAUSE 1 ' Charge Cap
RCTIME RCRL, 1, CenterRL ' Get Recording for TimeRL
LOW RCRL ' Discharge RL Cap
RETURN

'=================================================================================
'
Record Pot Values
'=================================================================================
PotRCTime:

HIGH RCUD
PAUSE 1 ' Charge Cap
RCTIME RCUD, 1, TimeUD ' Get Recording for TimeUD
LOW RCUD ' Discharge UD Cap
HIGH RCRL
PAUSE 1 ' Charge Cap
RCTIME RCRL, 1, TimeRL ' Get Recording for TimeRL
LOW RCRL ' Discharge RL Cap
RETURN

'==================================================================================
'
Pot Failure
'==================================================================================
Bug:

STOP
Sign In or Register to comment.