Gramps
Senior Member
This code (below) runs one motor perfectly.
We want to use this code to service three motor controllers.
The new pin numbers would be as follows.
MotorCon (original code)
Symbol desired_pot = 13 'B.5
Symbol feedback_pot = 11'B.4
Symbol MOTOR = B.0' Energize PWM
Symbol Direction = B.7
MotorCon2
Symbol desired_pot 2= 9 'B.3
Symbol feedback_pot2 = 8 'B.2
Symbol MOTOR2 = C.2' Energize PWM
Symbol Direction2 = B.6
MotorCon3
Symbol desired_pot3 = 10 'B.1
Symbol feedback_pot3 = 19' C.7
Symbol MOTOR3 = C.1 ' Energize PWM
Symbol Direction3 = C.6
We added a "2" and a "3" to each entire set of symbols, and that passes syntax.
Now, must we change all the variables?
Question.
In the first symbol:
symbol xb0 = b0 ' reserved for bit variables
I get a syntax error when changed to:
symbol xb8 = b8 ' reserved for bit variables
Why? Does that symbol need to remain as a stand-alone for all three motor controllers?
We want to use this code to service three motor controllers.
The new pin numbers would be as follows.
MotorCon (original code)
Symbol desired_pot = 13 'B.5
Symbol feedback_pot = 11'B.4
Symbol MOTOR = B.0' Energize PWM
Symbol Direction = B.7
MotorCon2
Symbol desired_pot 2= 9 'B.3
Symbol feedback_pot2 = 8 'B.2
Symbol MOTOR2 = C.2' Energize PWM
Symbol Direction2 = B.6
MotorCon3
Symbol desired_pot3 = 10 'B.1
Symbol feedback_pot3 = 19' C.7
Symbol MOTOR3 = C.1 ' Energize PWM
Symbol Direction3 = C.6
We added a "2" and a "3" to each entire set of symbols, and that passes syntax.
Now, must we change all the variables?
Question.
In the first symbol:
symbol xb0 = b0 ' reserved for bit variables
I get a syntax error when changed to:
symbol xb8 = b8 ' reserved for bit variables
Why? Does that symbol need to remain as a stand-alone for all three motor controllers?
Code:
'Shadow-Bot feedback control code with limits.
#picaxe 28x2
#no_data
#no_table
symbol xb0 = b0 ' reserved for bit variables
symbol bReverseForward=bit1 ' 0=reverse,=forward
symbol bMotorState=bit0 ' 0=off,1=on
symbol REVERSE_=0
symbol FORWARD_=1
Symbol desired_pot_value = b1
Symbol feedback_pot_value = b3
Symbol diff=b4
Symbol desired_pot = 13 'B.5
Symbol feedback_pot = 11'B.4
Symbol MOTOR = B.0' Energize PWM
Symbol Direction = B.7
Symbol MAXPWMVALUE = 399
Symbol bSlowState=bit2
Symbol old_feedback_value= b5
Symbol old_desired_value= b6
Symbol ENDSTOP_A = 35 ; desired_pot MINimum value
Symbol ENDSTOP_C = 210 ; desired_pot MAXimum value
;Lower shoulder min=35 max=210
;Upper shoulder min=?? max=???
Main:
Readadc Desired_pot, desired_pot_value
Desired_pot_value = Desired_pot_value MIN ENDSTOP_A MAX ENDSTOP_C
'sertxd (#desired_pot_value," ",#b1,13,10)
Readadc feedback_pot, feedback_pot_value
' sertxd (#feedback_pot_value," ",#b3,13,10)
'sertxd("Desired/Feedback: ",#desired_pot_value," ",#feedback_pot_value,13,10)
If old_feedback_value<>feedback_pot_value or old_desired_value<> desired_pot_value then
old_feedback_value=feedback_pot_value
old_desired_value=desired_pot_value
sertxd("Desired/Feedback: ",#desired_pot_value," ",#feedback_pot_value,13,10)
endif
if desired_pot_value > feedback_pot_value then
diff=desired_pot_value - feedback_pot_value
else
diff=feedback_pot_value - desired_pot_value
endif
if diff < 12 then ' from 11 below a match to 11 above
if bMotorState=1 then
if diff > 4 then ' this value may need to be set experimentally
if bSlowState = 0 then
bSlowState = 1 ' note that we are in the "slow speed" state
pwmout MOTOR, 199, 300 ' slow down to half speed (change to 100 if necessary)
endif
else
pwmout MOTOR, OFF ' STOP
bMotorState=0
bSlowState = 0
endif
endif
else ' motor must be set for proper direction and then activated
if desired_pot_value > feedback_pot_value then
if bReverseForward = REVERSE_ then
high Direction ' go forward
bReverseForward = FORWARD_
endif
elseif desired_pot_value < feedback_pot_value then
if bReverseForward = FORWARD_ then
low Direction ' go in reverse
bReverseForward = REVERSE_
endif
endif
if bMotorState=0 then
bMotorState=1
pwmout MOTOR, 199, MAXPWMVALUE' rotate Reverse
endif
endif
goto main