I see that I am way late to the party, sorry for that. However, it is only recently that I started messing around with the Pi so you will have to forgive me.
I believe I have found a possible answer for you, if you are still in need of one, if not no biggy. I will provide it anyways in case someone else stumbles upon this thread as I did, and is in search of a way to control an ESC with a Raspberry Pi.
I currently have a very simple bit of code to control a 320A ESC (Zjchao 7.2-16v High Voltage Version 320a Brushed ESC Tow Way with Brake for Rc Car Truck <- found on amazon) using the Raspberry Pi AND a PCA9685 PWM Board (ADAFRUIT 16-CHANNEL 12-BIT PWM/SERVO DRIVER - I2C INTERFACE <- found on adafruit). You will have to forgive my code ahead of time, as I am no master. I did some searching ahead of time to see if anyone has done this, but I kept drawing short straws. I was unsuccessful in controlling an ESC straight from the Pi and found some code here and there, but never anything that worked.
I almost gave up, but later ran across this thread which suggested using a PCA9685. So I grabbed my used one laying in my drawer from an old project, connected everything up, and ran the code from the previously mentioned thread. It had some issues, mainly I was having an issue "simulating fully engaging the throttle and then fully reversing it" for the ESC to do a proper boot. I figured it out by reading he actual thread and seeing that you need to press and hold the UP arrow first, then press and hold the DOWN arrow. Alright, sure, no problem.
Once realizing that, I disconnected everything, reconnected everything, restarted the python script (from the aforementioned thread), and proceeded to "follow directions" and BOOOOOM!!! I heard the magical Beep-BEEP-Boop-Beep-Boop tone and realized it was now working! I then proceeded to alter the code as I did not want to have to manually do this sequence every time I want to control my motors from a disconnect. The following is my flavor of code altered from the previously mentioned thread once more... just giving credit where its due.
from Adafruit_PWM_Servo_Driver import PWM
import time, curses
pwm = PWM(0x40, debug=True)
pwm.setPWMFreq(60)
screen = curses.initscr()
curses.noecho()
curses.cbreak()
screen.keypad(True)
running = True
fwdmax = 600
stop = 400
revmax = 200
go = 400
inc = 20
spinup = 1
def bootup():
boot = 200
while boot < fwdmax:
boot += inc
pwm.setPWM(0,0,boot)
time.sleep(0.1)
if boot > fwdmax:
while boot > revmax:
boot -= inc
pwm.setPWM(0,0,boot)
time.sleep(0.1)
if boot < revmax:
boot = 400
pwm.setPWM(0,0,boot)
spinup = 0
break
while running:
char = screen.getch()
if char == ord('q'):
running=False
else:
if char == ('b') and spinup == 1:
bootup()
if char == curses.KEY_UP:
if go < fwdmax:
go += inc
elif char == curses.KEY_DOWN:
if go > revmax:
go -= inc
elif char == ord(' '):
go = stop
pwm.setPWM(0, 0, go)
# shut down cleanly
curses.nocbreak(); screen.keypad(0); curses.echo()
curses.endwin()
So what I do is this:
- connect pi to bread board and connect PWM9685 for testing
- connect external power source to PWM board
- connect motor and battery for motor to ESC as you would your RC car
Here is where you need to be careful in case your ESC does not like the 60hz freq or the values being pushed via my code
- run the provided code, but do NOT push anything.
- connect ESC to first spot on PWM (0)
- press 'b' to boot the ESC
UP arrow = go faster x20
DOWN arrow = go slower x20
which I've altered from the code found from previously mentioned thread
'q' = quit program
'Space Bar' will stop the motor
I hope this helps!
Here is the reason for me needing to accomplish this task, a fun side project: 3d printed tank