RC control add on.

Gramps

Senior Member
We are still in Florida and I'm itching to get back to my shop.
The code below reads the value of the desired pot. I think it's basically watching for a voltage change from zero to 5 volts.
Correct?
A 1/10th scale ESC (electronic speed control) delivers a variable voltage output, zero to 6 volts to power a DC motor.
So it should be pretty simple to replace the desired pot with an ESC without even changing the code!
(Of course, it will NOT be that simple 😜)
Gramps

Code:
~
'Shadow-Bot feedback control code with limits updated 11-13-20
#picaxe 28x2
#no_data
#no_table ; The next line can reduce the PWM frequency ** NEW ** (Cannot put comments in #define lines)
#define pwmfreq PWMDIV16,
; #define pwmfreq
symbol DEADZONE = 2 ; Switch motor off (Make as small as practical) ;
; symbol SLOWZONE = 12 ; Slow motor down (NB: May depend on motor speed ! )
symbol MotorSpeed = w4 ; (To replace the "300" in the present PWMOUT code)
symbol LOOPGAIN = 33 ; i.e. MAXPWMVALUE / SLOWZONE (= 399 / 12)
symbol PWMPERIOD = 199 ; Equivalent Max Duty Cycle is 799 ;
; 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 ; ** ONLY HALF OF THE AVAILABLE DRIVE VOLTAGE ! ;
; Symbol bSlowState=bit2
Symbol old_feedback_value= b5
Symbol old_desired_value= b6
Symbol ENDSTOP_A = 60 ; desired_pot MINimum value
Symbol ENDSTOP_C = 210 ; desired_pot MAXimum value
Symbol CREEP = 160 ; PWM value to run slowly * HIGH ENOUGH TO START MOTOR TURNING !

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)
pause 100
Readadc feedback_pot, feedback_pot_value
' sertxd (#feedback_pot_value," ",#b3,13,10)
pause 100
' 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 > DEADZONE then
MotorSpeed = diff * LOOPGAIN max MAXPWMVALUE min CREEP ; Limit range of motor speed
if desired_pot_value > feedback_pot_value then
high Direction ; Set forward direction
else
low Direction ; Set reverse direction
endif
; pwmout MOTOR , PWMPERIOD , MotorSpeed ; Slows motor within SLOWZONE
pwmout pwmfreq MOTOR , PWMPERIOD , MotorSpeed ; Slows motor within SLOWZONE ** NEW **
else
pwmout MOTOR , OFF ; STOP ;
; bMotorState=0 ; Not sure what these do (now) ;
; bSlowState = 0 ;
endif
Go to main
[code]
 
Last edited:

oracacle

Senior Member
You can put your code in [ code] ... [ /Code] tags. Then use the preview to check formating and make changes, preview again until you get what's readable
 

AllyCat

Senior Member
We are still in Florida and I'm itching to get back to my shop.
The code below reads the value of the desired pot. I think it's basically watching for a voltage change from zero to 5 volts.
Correct?
A 1/10th scale ESC (electronic speed control) delivers a variable voltage output, zero to 6 volts to power a DC motor.
So it should be pretty simple to replace the desired pot with an ESC without even changing the code!
(Of course, it will NOT be that simple 😜)
Gramps
Edit: sorry about the code format. Editor is not here!
Code:
'Shadow-Bot feedback control code with limits updated 11-13-20
#picaxe 28x2
#no_data
#no_table ; The next line can reduce the PWM frequency ** NEW ** (Cannot put comments in #define lines)
#define pwmfreq PWMDIV16,
; #define pwmfreq
symbol DEADZONE = 2 ; Switch motor off (Make as small as practical) ;
; symbol SLOWZONE = 12 ; Slow motor down (NB: May depend on motor speed ! )
symbol MotorSpeed = w4 ; (To replace the "300" in the present PWMOUT code)
symbol LOOPGAIN = 33 ; i.e. MAXPWMVALUE / SLOWZONE (= 399 / 12)
symbol PWMPERIOD = 199 ; Equivalent Max Duty Cycle is 799 ;
; 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 ; ** ONLY HALF OF THE AVAILABLE DRIVE VOLTAGE ! ;
; Symbol bSlowState=bit2
Symbol old_feedback_value= b5
Symbol old_desired_value= b6
Symbol ENDSTOP_A = 60 ; desired_pot MINimum value
Symbol ENDSTOP_C = 210 ; desired_pot MAXimum value
Symbol CREEP = 160 ; PWM value to run slowly * HIGH ENOUGH TO START MOTOR TURNING !

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)
pause 100
Readadc feedback_pot, feedback_pot_value
' sertxd (#feedback_pot_value," ",#b3,13,10)
pause 100
' 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 > DEADZONE then
  MotorSpeed = diff * LOOPGAIN max MAXPWMVALUE min CREEP ; Limit range of motor speed
  if desired_pot_value > feedback_pot_value then
    high Direction ; Set forward direction
  else
    low Direction ; Set reverse direction
  endif
; pwmout MOTOR , PWMPERIOD , MotorSpeed ; Slows motor within SLOWZONE
  pwmout pwmfreq MOTOR , PWMPERIOD , MotorSpeed ; Slows motor within SLOWZONE ** NEW **
else
  pwmout MOTOR , OFF ; STOP ;
;   bMotorState=0 ; Not sure what these do (now) ;
;   bSlowState = 0 ;
endif
goto main
Hi,

I think I had some involvement with that code, but it does appear to have been almost 2.5 years ago!
I've worked through it putting in <Newlines> and indenting where I believe they should be, but various lines appear to have been "commented out", which I've duplicated. It still needs some Syntax (and INTENTION) checking to be carried out. :)

Cheers, Alan.
 

oracacle

Senior Member
To me it looks like code for a DIY servo. It takes a reading from the desired position put and compares or the current position put. If they are different it moves a motor until they match.
Does work on 5v,I don't know it maybe 3v it's hard to say without a schematic
 

oracacle

Senior Member
My knoledge of ESCs needs a lot of work as it doesn't really existy beyond the fact that i know that exist. However does the ESC feed back into the controller or is it self regulating by the voltage that is fed into it. My lmited understanding is they do not return anything to the controller - more like a normal servo - send it a PWM and assume its done what it's been told. The one you linked literally uses servo PWM signals, you could just ask the picaxe to write the servopos to the ESC

The code actively looks for feedback from the motor postion, if the ESC does not feed back a voltage (most likely in refrence to current speed) the controller is just going to keep increasing the voltage until it is at max.

The other option is to have some other feed back mechanism that is read to determin what the motor is doing, The control system is called closed loop and thusly needs some way to determine the state of the system. Further to this most closed loop systems used to control motors will have some sort of PID control. I would not like to even try an implement that on picaxe. Heres a wiki article: PID controller - Wikipedia. Yeah its pretty brain melting and needs some time to study.

To be honest if you just want to have a variable open loop speed control, you just need to map a pot postion onto a pwm signal and send that to the ESC. Or connect the pot directly to the ESC

Overall it shouldn't be, in theory, too to hard to write a potentiometer to speed controller.

Here is some work done on using to control servo postion (and thus ESC speed) using a pot being mapped to the servopos command: Creating a map subroutine | PICAXE Forum

Hopefully that helps.
 
Top