Mixing analog outputs for differential drive robot

Hi,

I have implemented PS2 remote control protocol and i am getting all the inputs properly from remote.

Now i want to set my left and right motor PWM such that they give proportional drive to my robot from my analog controller stick. let us say when i get y=0 and x=128 i should have full motor speed for left and right motor such that my robot moves forward.

Now my problem is i tried to mix it praportionally with equations but it does not give proper control for my robot.

Has anyone tried this?? :unamused:

Regards.

What have you tried?

Basically, you start off with the X joystick value, and modify (multiply) by the Y value for the left wheel. The right wheel gets the same treatment, but a -Y value. The results get scaled down to the range needed by the PWM parameters for the two motors.

So, full “UP” X (right stick vertical) and no Y gives full forward on both motors.

Full “right” Y gives (right stick horizontal) gives full left forward, and full right reverse.

Full X “up” AND Y “right” gives 1.4 forward on left motor and .7 forward on right motor (assumes joysticks give .7 each in this 45 degree setting).

Full “down” X and no Y gives full reverse on both motors.

ETC.

You’ll have to scale these settings to the PWM as mentioned. You’ll be multiplying values; so observe the results obtained, and figure your scaling accordingly.

HTH

Alan KM6VV

sorry for bring up an old topic but I need exactly this.

I programming in Basic 5.3 and I need help with some code that will take the left joy, mix and control the dual H-bridge with PWM. I already have the logic code that will switch the PWM pins and low pins, but I need help in mixing to produce two values that will indicate the direction and speed.

Please help.

I’ll post the code that I have shortly.

Bane

here is the code that lynx has. My question is why are they using Temp(3) and Temp(4) when the left joy is only Temp(5) and Temp(6)? I’ll try some stuff with this code, but the end result of the mixing is about what I need.

[code]'Work in progress!
LED con P0

;PS2 Controller / BotBoard I
DAT con P4
CMD con P5
SEL con P6
CLK con P7
;PS2 Controller / BotBoard II
;DAT con P12
;CMD con P13
;SEL con P14
;CLK con P15

temp1 var byte
index var byte
temp var byte(6)
cntr var byte
mode var byte

rhori var word
rvert var word
lhori var word
lvert var word
lservo var sword
rservo var sword
small var byte
large var byte

high CLK

test
low SEL
shiftout CMD,CLK,FASTLSBPRE,$1\8,$43\8,$0\8,$1\8,$0\8] ;Config Enable
high SEL

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 Fix(Analog
high SEL

pause 100

low SEL
shiftout CMD,CLK,FASTLSBPRE,$01\8,$4D\8,$00\8,$00\8,$01\8,$ff\8,$ff\8,$ff\8,$ff\8] ;Vibration Enable
high SEL

low SEL
shiftout CMD,CLK,FASTLSBPRE,$01\8,$4F\8,$00\8,$FF\8,$FF\8,$03\8,$00\8,$00\8,$00\8] ;Enable Pressure
high SEL

low SEL
shiftout CMD,CLK,FASTLSBPRE,$01\8,$43\8,$00\8,$00\8,$5A\8,$5A\8,$5A\8,$5A\8,$5A\8] ;Press_trans_start
high SEL

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 Exit
high SEL

main
’ This section find the mode the PSX controller is in.

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

low SEL
shiftout CMD,CLK,FASTLSBPRE,$1\8,$42\8]
for index = 0 to 2
shiftin DAT,CLK,FASTLSBPOST,[temp(index)\8]
next
high SEL

low SEL
shiftout CMD,CLK,FASTLSBPRE,$1\8,$42\8,$0\8,small\8,large\8]
for index = 3 to 6
shiftin DAT,CLK,FASTLSBPOST,[temp(index)\8]
next
high SEL

’ This section does the mixing for throttle steering on a diff steered vehicle.
rhori = (255 + temp(3))
rvert = (255 + temp(4))
lhori = (255 + temp(5))
lvert = (255 + temp(6))

lservo = 383 + ((lhori-383) + (lvert-383))
rservo = 383 + ((lvert-383) - (lhori-383))

if lservo < 255 then
lservo = 255
endif
if lservo > 510 then
lservo = 510
endif
if rservo < 255 then
rservo = 255
endif
if rservo > 510 then
rservo = 510
endif

lservo = 4 * (lservo - 383)
rservo = 4 * (rservo - 383)

’ The following can be used as a debug to see the values of the joysticks and buttons.
’ serout S_OUT,i57600,[13]
’ for index = 1 to 6
’ serout S_OUT,i57600," ",dec3 temp(index)\3]
’ next

’ serout S_OUT,i57600," “,dec3 rhori\3]
’ serout S_OUT,i57600,” ",dec3 rvert\3]

’ serout S_OUT,i57600," LH “,dec3 lhori\3]
’ serout S_OUT,i57600,” LV “,dec3 lvert\3]
’ serout S_OUT,i57600,” LS “,sdec3 lservo\6]
’ serout S_OUT,i57600,” RS ",sdec3 rservo\6]

’ The following code relates to the internal vibrator motors and is not used in this application.
small = 0 'temp(7)
large = 0 'temp(8)

’ This section is where the servo style motor controller is directed.
servo 0, lservo, 1
servo 1, rservo, 1

goto main

[/code]

My guess? They are probably just reading, scaling the values for the other joystick, for completeness? The program can be expanded.

The mixing and scaling can be seen in:

lservo = 383 + ((lhori-383) + (lvert-383)) rservo = 383 + ((lvert-383) - (lhori-383))

Alan KM6VV

Thanks, this code works but I can’t seem to get 255 for maximum speed.

[code]X = temp(5)
Y = temp(6)

if Y < 115 then
		Y =  (230 - Y) * 2
	low 0
	pwm p1, Y, 5000
	low 2
	pwm p3, Y, 5000
	
		
elseif Y > 141 
		Y = (Y - 141) * 2
	pwm p0, Y, 5000
	low 1
	pwm p2, Y, 5000
	low 3
	
		
elseif X < 115 
		X = (230 - X) * 2
		dydx = X/2
	pwm p0, dydx, 5000
	low 1
	pwm p2, x, 5000
	low 3
	
		
elseif X > 141 
		X = (X - 141) * 2
		dydx = X/2
	pwm p0, dydx, 5000
	low 1
	pwm p2, x, 5000
	low 3
		endif

if temp(11) > 128 then
pwm p5, 255, 100000
endif
if temp(12) > 128 then
pwm p5, 175, 100000
endif
if temp(14) > 128 then
low 5
endif
goto main[/code]

Where did this code come from? It’s not the same as the demo you last posted.

Alan KM6VV

I created it. Of course there is the ps2 data code above it and not included here. I still don’t understand why temp(3) and temp(4) were included in the first code.

Bane

here is the entire code

[code];-----------Bot Board Selection----------
'Project AXIOM TANK
'program: PS2 and dual h-bridge with PWM

;PS2 Controller / BotBoard II
DAT con P12
CMD con P13
SEL con P14
CLK con P15
HIGH 4 'motor enable
LOW 5 'fan disable
;-----------------------------

index var byte
temp var byte(19)
mode var byte
Small_Motor var byte
Large_Motor var byte

Y var sword
X var sword
dydx var word

;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,$4D\8,$00\8,$00\8,$01\8,$FF\8,$FF\8,$FF\8,$FF\8] ;VIBRATION_ENABLE
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

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

pause 1

;-----------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

;-----------Basic Micro IDE terminal----------

'serout S_OUT,I8N1_57600,[13, hex2 mode\2] ; Basic Micro IDE <= 05.3.0.0

'for index = 1 to 18
'serout S_OUT,I8N1_57600," ", dec3 temp(index)\3] ; Basic Micro IDE <= 05.3.0.0
'next
;-----------------------------
X = temp(5)
Y = temp(6)

if Y < 115 then
		Y =  (230 - Y) * 2
	low 0
	pwm p1, Y, 5000
	low 2
	pwm p3, Y, 5000
	
		
elseif Y > 141 
		Y = (Y - 141) * 2
	pwm p0, Y, 5000
	low 1
	pwm p2, Y, 5000
	low 3
	
		
elseif X < 115 
		X = (230 - X) * 2
		dydx = X/2
	pwm p0, dydx, 5000
	low 1
	pwm p2, x, 5000
	low 3
	
		
elseif X > 141 
		X = (X - 141) * 2
		dydx = X/2
	pwm p0, dydx, 5000
	low 1
	pwm p2, x, 5000
	low 3
		endif

'fan pwm

if temp(11) > 128 then
pwm p5, 255, 100000
endif
if temp(12) > 128 then
pwm p5, 175, 100000
endif
if temp(14) > 128 then
low 5
endif
goto main[/code]

It’s common practice to “flush out” an interface (PS2), and provide “stubs” for future use of data being passed (received).

What more can I say?

Alan KM6VV

Hmm, Ok will doing this make it loop faster? I’ve noticed that the commands will back up some times. But temp(3-4) is the right joy, I dont want to use this joy. Wouldn’t be faster not to use them?

Bane

I don’t think it will make any difference in the execution speed. Take them out if they bother you! I tend to want as many controls as possible available to my program. You never know when you might need them. How about for a camera tilt/pan?

Alan KM6VV

thanks