Author Topic: differential steering code  (Read 4924 times)

0 Members and 1 Guest are viewing this topic.

Offline BANETopic starter

  • Supreme Robot
  • *****
  • Posts: 639
  • Helpful? 4
  • E=roboticsC^2
differential steering code
« on: May 27, 2009, 10:23:47 AM »
Hello,

Need some help here (mostly math) with differential steering code.  I'm programming in Basic; I have two values (X and Y from left joystick) coming from my ps2 controller (0-255).  I have two servos driving the robot (500 - 2500).  I just can't seem to get the math right so that the robot will move porportonial to the joy.  Any ideas?

Bane

Offline Razor Concepts

  • Supreme Robot
  • *****
  • Posts: 1,856
  • Helpful? 53
Re: differential steering code
« Reply #1 on: May 27, 2009, 03:04:20 PM »
EDIT: oops, this is a tough problem.
« Last Edit: May 27, 2009, 03:08:16 PM by Razor Concepts »

Offline SmAsH

  • Supreme Robot
  • *****
  • Posts: 3,959
  • Helpful? 75
  • SoR's Locale Electronics Nut.
Re: differential steering code
« Reply #2 on: May 27, 2009, 03:28:55 PM »
wait, so you want the robot to move forward when you push the stick forward and same for left/right?
does the ps2 controller have to stay intact?
Howdy

Offline HDL_CinC_Dragon

  • Supreme Robot
  • *****
  • Posts: 1,261
  • Helpful? 5
Re: differential steering code
« Reply #3 on: May 27, 2009, 04:15:31 PM »
hmmm heres a shot in the dark that popped into my head right away. Ill try to explain the idea but its kind of complex for just text lol. Ok here goes nothing:
Imagine you have a standard 4 quadrant graph and the origin of that graph (coords 0,0) is dead center on the center of the joystick. Now the Y-Axis of this hypothetical graph is the forward and backward speed of the robot and the X-Axis is the turning speed. Lets set the graph values as percentages in speed. So moving the stick halfway forward would correlate on the graph as coords (50,0) and the robot would be going forward at 50% max speed so that means both motors are at 50%. Now lets say you move the stick to the right from the origin to lets say 10%. So the robot should now be giving 10% forward speed to the left motor and 10% reverse speed to the right motor which would result in the robot turning right. Now lets say you move the joystick upwards 50% and to the right 10% at the same time. So you have a base of 50% forward speed to both motors but taking into account the 10% turn to the right, the right motor would have 10% reverse speed added or 10% forward speed subtracted whilst the left motor would have 10% forward speed added. After the quick math with that 10% right turn and 50% base forward speed, the right motor would be getting 40% forward speed and the left motor would be getting 60% forward speed.

I hope you can tell what im talking about... If not ill just make a small video to explain it better if needed
United States Marine Corps
Infantry
Returns to society: 2014JAN11

Offline paulstreats

  • Supreme Robot
  • *****
  • Posts: 1,381
  • Helpful? 21
Re: differential steering code
« Reply #4 on: May 27, 2009, 05:09:32 PM »
this might be too easy to work but have you tried somethingm like:


SERVO_DELAY = ((2000/255) * READINGFROMJOYSTICK) + 500

you use 2000 rather than 2500 because there is a missing 500 due to the servo pulse range.

so you just divide 2000 by 255. This gives a multiplier base.
Multiply the base by the actual reading of the joystick.

then just add 500 to make up for the missing 500 servo pulse range.

with this you should get

joystick = 127           --     servo = 1496  (and the rest changeing linearly)

i.e. with the joystick at centre (reading 127) the servo pulse comes out at 1496. the central value of the servo is 1500 but because of rounding problems youre 4 out. you might have to add 4 to make sure its at all stop in the centre.

edit
---------------------------------

Ive just realised the problem is trickier than my first solution. I'll have another think later.... (i do have another idea though)
« Last Edit: May 27, 2009, 06:04:33 PM by paulstreats »

Offline jakx12

  • Robot Overlord
  • ****
  • Posts: 183
  • Helpful? 2
Re: differential steering code
« Reply #5 on: May 27, 2009, 05:23:53 PM »
bit of the topic but are you using a bx-24?
Need help with an algorithm or a maths related problem? Ill be glad to help :)

Offline BANETopic starter

  • Supreme Robot
  • *****
  • Posts: 639
  • Helpful? 4
  • E=roboticsC^2
Re: differential steering code
« Reply #6 on: May 27, 2009, 08:37:58 PM »
thanks all,

I've been doing some thinking and test programming... not working though :P.   I know something is wrong with my math but just look at the concept here.  Having the vertical joy value control the base servo speed and have the horizontal joy subtract from ether the left or right to give the difference. 
Code: [Select]
ldiff var word
rdiff var word
lhori var word
rhori var word

lhori = (8 * temp(5)) + 476) 'temp(5) = left joy horizontal
lvert = (8 * temp(6)) + 476) 'temp(6O = left joy vertical

if lhori > 127 then
ldiff = 1500 - ((lhori * 8) + 476)
rdiff = lhori
lservo = lvert + ldiff
rservo = lvert - rdiff

elseif lhori < 127
ldiff = lhori
rdiff = 1500 - ((lhori * 8) + 476)
lservo = lvert - ldiff
rservo = lvert + rdiff
endif

'servo pulse code and other stuff (500 to 2500)

Offline BANETopic starter

  • Supreme Robot
  • *****
  • Posts: 639
  • Helpful? 4
  • E=roboticsC^2
Re: differential steering code
« Reply #7 on: May 28, 2009, 04:58:20 AM »
Quote
bx-24
  Not sure what this is... but if your referring to my microcontroller I'm using the Basic Atom 28 with bot board II from lyxnmotion.com.  And the I'm using the wireless ps2 controller also available at their website

Bane

Offline jakx12

  • Robot Overlord
  • ****
  • Posts: 183
  • Helpful? 2
Re: differential steering code
« Reply #8 on: May 28, 2009, 04:07:30 PM »
aah ok, its a micro controller with 24 i/o . Sorry just wondering
Need help with an algorithm or a maths related problem? Ill be glad to help :)

Offline BANETopic starter

  • Supreme Robot
  • *****
  • Posts: 639
  • Helpful? 4
  • E=roboticsC^2
Re: differential steering code
« Reply #9 on: May 29, 2009, 05:03:33 AM »
it has 16 i/o and four analog inputs...  :P I know, i bought this just be for the axon came out.  Will have to redeem myself and get the axon II and start learning C instead of toddler Basic :D.

Bane

Offline SmAsH

  • Supreme Robot
  • *****
  • Posts: 3,959
  • Helpful? 75
  • SoR's Locale Electronics Nut.
Re: differential steering code
« Reply #10 on: May 29, 2009, 05:05:39 AM »
if the exchange rate turns decent, once i get a job im definitely getting an axon II.
@bane, do you want the controller to be useable with a regular ps2 after you are done?
Howdy

Offline BANETopic starter

  • Supreme Robot
  • *****
  • Posts: 639
  • Helpful? 4
  • E=roboticsC^2
Re: differential steering code
« Reply #11 on: May 29, 2009, 05:06:22 AM »
wow, I've been looking around at other sites and no one has seemed to figure out the differential steering code ether :P.  Does any one have any alternative ideas?  

Bane

Offline BANETopic starter

  • Supreme Robot
  • *****
  • Posts: 639
  • Helpful? 4
  • E=roboticsC^2
Re: differential steering code
« Reply #12 on: May 29, 2009, 05:08:54 AM »
you are a fast one smash (just posted after yours :D)

The controller is wireless and untouched but the receiver and cable is far beyond repair :-[  Its ok, i got a ps3 ;D  just need to figure out how to hack the six axis controller

bane

Offline SmAsH

  • Supreme Robot
  • *****
  • Posts: 3,959
  • Helpful? 75
  • SoR's Locale Electronics Nut.
Re: differential steering code
« Reply #13 on: May 29, 2009, 05:19:59 AM »
ohh, wireless eh?
whats wrong with the receiver?
i was going to say just measure the pot... but eh... ok... if its wireless then maybe not...
Howdy

Offline BANETopic starter

  • Supreme Robot
  • *****
  • Posts: 639
  • Helpful? 4
  • E=roboticsC^2
Re: differential steering code
« Reply #14 on: May 29, 2009, 05:34:45 AM »
once the data is received it comes into the program as a value ranging from 0 to 255 with 128 being the center position.  Nothing is wrong with the receiver as far as functionality it just that i've taken the cover off so all it is is a PCB with a few wires coming out going to the microcontroller.   In other words it will never be apart of my ps3 controller receiver collection :D

Offline paulstreats

  • Supreme Robot
  • *****
  • Posts: 1,381
  • Helpful? 21
Re: differential steering code
« Reply #15 on: May 29, 2009, 05:50:47 PM »
 Having had a think ??? lets look at providing differential forward movement first and then including spot steering and reverse later

There are a few ways of doing this too. at full forward (Y-axis), do you want the robot to go full speed forward and then when a turn command occurs (x - axis) just slow 1 wheel down OR .. at full forward (Y - axis), just move the bot 50% of the full speed then say when you press left, the right wheel can speed up to create the turn (you can even slow the left wheel down too).

I'll probably look at the first example above

 First use the x - axis to determine an acceleration base for each wheel.
 
 In the center position(128) both wheels should be registering the highest base value. at the far left (0 ?probably) the right wheel should still be at full base value but the left wheel should have a 0 base value causing the robot to steer left. using this rule, at a base value of 64 the left wheel should have 50% base value and the right wheel should still have full base value.

if (X_AXIS_READING < 128){
LEFT_WHEELBASE = (2000/(128*(128-X_AXIS_READING)));
RIGHT_WHEELBASE = 2000/128;
}else if (X_AXIS_READING > 128){
RIGHT_WHEELBASE = (2000/(128*(X_AXIS_READING-128)));
LEFT_WHEELBASE = 2000/128;
}else if(X_AXIS_READING == 128){
LEFT_WHEELBASE = 2000/128;
RIGHT_WHEELBASE = 2000/128;
}


As the multiplier, we need to use the Y_AXIS. Im going to presume to pushing the joystick up will yield a 0 and pushing it down will yield 255?

LEFT_SERVO_DELAY = (LEFT_WHEELBASE * (128 - Y_AXIS_READING)) + 500;
RIGHT_SERVO_DELAY = (RIGHT_WHEELBASE * (128 - Y_AXIS_READING)) + 500;

This only works for forard movement, if you attempt backwards than out of range values will be sent to the servos causing them to behave erratically. So you should enlose the code in another if conditional to stop it going out of range. At the same time you could make another if conditional to create the backwards motion by swapping the sums about a bit, aswell as creating another one for on the spot turning...
This should hopefully give us the wheel bases


Offline Resilient

  • Full Member
  • ***
  • Posts: 111
  • Helpful? 4
Re: differential steering code
« Reply #16 on: May 29, 2009, 08:49:41 PM »
As you come up with different equations, this may help you visualize what they are actually outputting:

http://www.livephysics.com/ptools/online-3d-function-grapher.php

And you may want to look at an equation like:

For positive X and Y (forward and turning right)

left_wheel = (y-128)+(x-128)^(.0078*(256-y))
left_wheel =  (7.8125*left_wheel + 1500)

and then the right wheel will be some variation on that.
« Last Edit: May 29, 2009, 08:57:30 PM by Resilient »

Offline BANETopic starter

  • Supreme Robot
  • *****
  • Posts: 639
  • Helpful? 4
  • E=roboticsC^2
Re: differential steering code
« Reply #17 on: May 29, 2009, 09:16:07 PM »
@ paulstreats: I think your on to it here.  I'll go ahead and try this.  Another line that needs to be in the code is then the X is ether 0 or 255 (min/max values), here the servos need to turn in different directions. 

also I've noticed with continuous rotation servos that (for example) the y joy is reading 160 (if going forward servo speed = 25%)  if the reading is 192 the forward servo speed should = 50% but it has already reached 100%.  So apparently it is not scaled 1:1 or not linear or something; anyone know how to make this proportional?  Just to be clear i would like 128 - 255 be forward speed of 0% - 100% and of course 0 - 128 be backward speed of 100% - 0%. 

@ resilient, this is a really cool website, have to remember this when taking calc. III spring semester :D

Offline Resilient

  • Full Member
  • ***
  • Posts: 111
  • Helpful? 4
Re: differential steering code
« Reply #18 on: May 29, 2009, 09:50:55 PM »
So do you know that 500-2500 is right for the servo speeds?

Like if you just directly feed the servo 2000, is it slower than if you feed it 2500?

Because if those numbers are right, and 1500 is the middle then the conversion should be

/* wheel is some value between 0-255 with 128 being stopped*/
wheel = (7.8125*(wheel-128) + 1500)
/*wheel is now some value between 500-2500 with 1500 being stopped*/

Offline BANETopic starter

  • Supreme Robot
  • *****
  • Posts: 639
  • Helpful? 4
  • E=roboticsC^2
Re: differential steering code
« Reply #19 on: May 30, 2009, 05:00:39 AM »
you have a point here; i haven't used continuous servos and probably forgot the min and max values. www.parallax.com/dl/docs/prod/motors/crservo.pdf
here is the servo

I'll do some testing to figure out what the min and max are.   if i recall it might be something like 750 to 2250.

Bane

Offline BANETopic starter

  • Supreme Robot
  • *****
  • Posts: 639
  • Helpful? 4
  • E=roboticsC^2
Re: differential steering code
« Reply #20 on: May 31, 2009, 10:08:15 AM »
Yes!  about 4 hours of screwing around and figured it out.  First problem was my continuous rotation servos are not 500 - 2500, much to my surprise they are 1386 - 1628 :o :o :o.  Parallax makes some screwy servos :D.  Heres my complete code :)
Code: [Select]
;PS2 Controller / BotBoard II / SSC-32
;Tank Differential Steering Algorithm
;05/31/09
;program status: Successful
;problems: none

pause 1000 'intial pause
;PS2 controller constants
DAT con P12
CMD con P13
SEL con P14
CLK con P15
;-----------------------------
index var byte
temp var byte(19)
mode var byte
;drive and camera servo variables
Lservo var word
Rservo var word
tiltservo var word
panservo var word
X var word
Y var word

;SSC-32 timing variable
ttm var byte
ttm = 50

;Drive and Camera servo intial position
Lservo = 1500
Rservo = 1500
tiltservo = 1200
panservo = 1526
;send intial servo positions to SSC-32
SEROUT P11, I38400, ["#0P",DEC Lservo, "#1P",DEC Rservo, "#2P",DEC panservo, "#3P",DEC tiltservo,"T",DEC TTM,13]

;PS2Init
high CLK

low SEL
shiftout CMD,CLK,FASTLSBPRE,[$1\8,$43\8,$0\8,$1\8,$0\8] ;CONFIG_MODE_ENTER
high SEL
pause 1

low SEL
shiftout CMD,CLK,FASTLSBPRE,[$01\8,$44\8,$00\8,$01\8,$03\8,$00\8,$00\8,$00\8,$00\8] ;SET_MODE_AND_LOCK
high SEL
pause 100

low SEL
shiftout CMD,CLK,FASTLSBPRE,[$01\8,$4F\8,$00\8,$FF\8,$FF\8,$03\8,$00\8,$00\8,$00\8] ;SET_DS2_NATIVE_MODE
high SEL
pause 1

low SEL
shiftout CMD,CLK,FASTLSBPRE,[$01\8,$43\8,$00\8,$00\8,$5A\8,$5A\8,$5A\8,$5A\8,$5A\8] ;CONFIG_MODE_EXIT_DS2_NATIVE
high SEL
pause 1

low SEL
shiftout CMD,CLK,FASTLSBPRE,[$01\8,$43\8,$00\8,$00\8,$00\8,$00\8,$00\8,$00\8,$00\8] ;CONFIG_MODE_EXIT
high SEL
pause 1

main ;main loop

;-----------PS2 Mode----------
low SEL
shiftout CMD,CLK,FASTLSBPRE,[$1\8]
shiftin DAT,CLK,FASTLSBPOST,[mode\8]
high SEL
;-----------------------------

;-----------PS2 Data----------
low SEL
shiftout CMD,CLK,FASTLSBPRE,[$1\8,$42\8]
shiftin DAT,CLK,FASTLSBPOST,[temp(0)\8,temp(1)\8,temp(2)\8,temp(3)\8,temp(4)\8,temp(5)\8,temp(6)\8,temp(7)\8,temp(8)\8, |
temp(9)\8,temp(10)\8,temp(11)\8,temp(12)\8,temp(13)\8,temp(14)\8,temp(15)\8,temp(16)\8,temp(17)\8,temp(18)\8]
high SEL
;-----------------------------
X = temp(5) + 1372 'left horizonial joystick data input
Y = temp(6) + 1372 'left vertical joystick data input 

Lservo = 1500 'if no data input
Rservo = 1500 'if no data input

'foward
if Y < 1499 and X = 1500 then 
Lservo = Y
Rservo = 3000 - Y

'backward
elseif Y > 1501 and x = 1500
Lservo = Y
Rservo = 3000 - Y

'power spin left
elseif Y = 1500 and X < 1374
Lservo = 1372
Rservo = 1372

'power spin right
elseif Y = 1500 and X > 1625
Lservo = 1627
Rservo = 1627

'foward left
elseif Y < 1499 and X < 1499
Lservo = 1499 - (1499 - X)
Rservo = 3000 - Y

'foward right
elseif Y < 1499 and X > 1501
Lservo = Y
Rservo = 3000 - (1501 + (1501 - X))



'backward left
elseif Y > 1501 and X < 1499
Lservo = 3000 - (1499 - (1499 - X))
Rservo = 3000 - Y

'backward right
elseif Y > 1501 and X > 1501
Lservo = 3000 - (1501 + (1501 - X))
Rservo = 3000 - Y

endif

tiltservo = (4 * temp(4)) + 688 'camera tilt servo
panservo = (7 * temp(3)) + 630 'camera pan servo

;send data to SSC-32
SEROUT P11, I38400, ["#0P",DEC Lservo, "#1P",DEC Rservo, "#2P",DEC panservo, "#3P",DEC tiltservo,"T",DEC TTM,13]

goto main

Offline BANETopic starter

  • Supreme Robot
  • *****
  • Posts: 639
  • Helpful? 4
  • E=roboticsC^2
Re: differential steering code
« Reply #21 on: May 31, 2009, 01:15:57 PM »
just for the records heres a pic of the Jim Dandy :)

 


Get Your Ad Here

data_list