Brookesa05
Posts: 22
Joined: Mon Apr 15, 2013 7:53 pm

Controlling ESC (Electronic speed controller) with RPi.

Mon Jun 10, 2013 6:29 pm

Hi, I have been trying for a while now to control a brushless motor using my raspberry pi. I would like some help in working out weather it is possible to use the Adafruit 16-channel 12-bit pwm/servo driver to do it. I have one set up on my pie and im using their Sample_Example.py code to test the motor and not getting anywhere. It might be that i am not using the right pulse lengths / frequencies etc. Any help on this topic would be much appreciated!

bouncymat
Posts: 38
Joined: Thu Feb 28, 2013 8:24 am

Re: Controlling ESC (Electronic speed controller) with RPi.

Tue Jun 11, 2013 1:55 pm

hi
I spent a while trying to get the RPi PWM to drive my ESC with no luck so i bought the same Adafruit 16Ch servo board.
I have got it working, with my brushless motor and ESC.
The problem you are having may be the same as that which stumped me (i just had a beeping twiching motor for quite some time)......The safety features on your ESC - I have to start mine with the PWM set to max then switch it to min within a couple of seconds, then the motor plays me a tune, this indicates that the ESC is set up and ready to work.

saftey notes (from a silly man)...
*screw the motor down - mine lifts the piece of wood i have it on
*don't put a prop on it until you are really sure that you know what it is doing - it starting randomly is terrifying
*when you do put the prop on attach it properly - it is very scary having come off and shoot across the room at full speed(i now wear goggles when fiddling with this)

SiriusHardware
Posts: 500
Joined: Thu Aug 02, 2012 9:09 pm
Location: UK

Re: Controlling ESC (Electronic speed controller) with RPi.

Tue Jun 11, 2013 5:03 pm

bouncymat wrote:hi

*when you do put the prop on attach it properly - it is very scary having come off and shoot across the room at full speed(i now wear goggles when fiddling with this)
Off topic, but in a similar vein - i once got chased around my shower by the shower pump motor which had broken its mountings and smashed its way out through the casing of the shower. Fortunately, it left the wiring and therefore the power behind it in the shattered remains of the housing, so I only had to evade it for a couple of seconds until it stopped richocheting around the bath.

Brookesa05
Posts: 22
Joined: Mon Apr 15, 2013 7:53 pm

Re: Controlling ESC (Electronic speed controller) with RPi.

Tue Jun 11, 2013 5:40 pm

Thankyou so much for your reply, have just set up my pi and testing your suggestion. Im really no good with this python stuff also no good at using esc. What values do you think i should use for Freq, puslelength etc. i know that 2ms pulse is full throttle and i think the frequency shuld be about 500hz at 2ms but not much esle. i am trying to adapt the Servo_Example.py code supplied by adafruit but it confuses me. Like whats the differance between pulseLength and pulseLength / ? any ideas?


Thankyou for your help

bouncymat
Posts: 38
Joined: Thu Feb 28, 2013 8:24 am

Re: Controlling ESC (Electronic speed controller) with RPi.

Tue Jun 11, 2013 7:24 pm

I also have very little idea what i am doing (as you will see when i post the code in a moment).

Firstly - you do have the motor set up properly? i used a lipo battery connected to the esc, then connect the earth and signal wire from the esc to the control board - I do not know if you can connect all three wires from the esc, but the +ve wire is carrying +5v(ish - in fact i have powered the pi of it before) so i do not know if that would damage the servo board.

This (prepare to be shocked) is the code i used - mostly just modified from the example you are talking about.

Code: Select all

#!/usr/bin/python

from Adafruit_PWM_Servo_Driver import PWM
import time
import curses

pwm = PWM(0x40, debug=True)

pwm.setPWMFreq(60)                        # Set frequency to 60 Hz

screen = curses.initscr()
# turn off input echoing
curses.noecho()
# respond to keys immediately (don't wait for enter)
curses.cbreak()
# map arrow keys to special values
screen.keypad(True)

done=False
numbmax = 600
numbmin = 200
numb = numbmin
inc = 10

try:
    while not done:
		char = screen.getch()
		if char == ord('q'):
			done=True
		else:
			if char == curses.KEY_UP:
				if numb < numbmax:
					numb += inc
			elif char == curses.KEY_DOWN:
				if numb > numbmin:
					numb -= inc
			elif char == ord('t'):
				numb = numbmax
			elif char == ord('b'):
				numb = numbmin
		
		pwm.setPWM(0, 0, numb)
		
finally:
    # shut down cleanly
	curses.nocbreak(); screen.keypad(0); curses.echo()
	curses.endwin()
	print(numb)
i use the curses to enable me to use the arrow keys to control the motor, i really don't know if that is what it is supposed to be for but after alot of googling i found it and it seems to work!

what i cannot remember is if i tweeked tose top and bottom values(if i did it was not by much)

so with all of the above this is what i do and it works every time.
1. connect everything accept power to the esc
2 run program
3 with my code, once it is running press t to get the top value
4 connect power to esc
5 wait a moment
6 if using my code press b to get bottom value
7 esc plays tune
8 tap up arrow and motor starts spinning.

of course the other thing is to just plug in a servo to see if that works - then at least you know the servo board is working
Last edited by bouncymat on Wed Jun 12, 2013 4:11 am, edited 1 time in total.


Brookesa05
Posts: 22
Joined: Mon Apr 15, 2013 7:53 pm

Re: Controlling ESC (Electronic speed controller) with RPi.

Wed Jun 12, 2013 10:36 pm

Thank you so much for the code. I have been doing some testing with a bit of luck. I run through the testing and had some funny results. When i started your program some times after pressing t and connecting power it would beeb a few times then i press b and it would beeb again and constantly beeb and twitch each time. Then a few times when I've closed the program down the motor spins at full power un-explained. then raspberry pi should not even be out putting at that point.

bouncymat
Posts: 38
Joined: Thu Feb 28, 2013 8:24 am

Re: Controlling ESC (Electronic speed controller) with RPi.

Thu Jun 13, 2013 12:56 pm

I think you may be beyond where i can help you!

If you put a servo on the board does it work with the original example code? - this is a good test to check if the servo board is working, made a big smile on my face when i got the servo swiveling about.

Other than that have a look at the data sheet for your esc to see what signals it expects to get it started??

User avatar
Hove
Posts: 1205
Joined: Sun Oct 21, 2012 6:55 pm
Location: Cotswolds, UK
Contact: Website

Re: Controlling ESC (Electronic speed controller) with RPi.

Thu Jun 13, 2013 1:55 pm

Not sure if this will be any help, but I have brushless motors for my quadracopter driven by the Adafruit PWM via I2C. I've not put any code up there yet, since until the drone is in a stable hover, it's dangerous! I'm about to try the RPIO PWM instead of the Adafuit as although the Adafruit works perfectly for me, it's sitting on my breadboard where I want to move the MPU6050 to live.

Take a look at the top few entries to see what I'm wittering on about...

www.pistuffing.co.uk
www.pistuffing.co.uk - Raspberry Pi and other stuffing!

ofe
Posts: 47
Joined: Wed Jun 05, 2013 4:05 am
Location: Italy,Milan
Contact: Website

Re: Controlling ESC (Electronic speed controller) with RPi.

Thu Jul 11, 2013 3:37 am

Hello,
I'm just starting my quadcopter project.
I'm still in the "theory phase" .
I see that rpi has a pwm output available.
So way I need a ESC to move a motor? Can't I use directly the rpi signal?
Oscar

Visit my quadcopter blog
http://solenerotech1.wordpress.com

User avatar
Hove
Posts: 1205
Joined: Sun Oct 21, 2012 6:55 pm
Location: Cotswolds, UK
Contact: Website

Re: Controlling ESC (Electronic speed controller) with RPi.

Thu Jul 11, 2013 5:37 am

The motors on a quad may draw over 10 amp each. The PWM can only put out a few milliamps. At the same time, the brushless motors are build up of 3 sets of coils of wires (the rotors), and many sets of magnets (the stators). The 3 wires feeding the motors have 3 signals that are 120 degrees apart, and change all the time to make the motor move.

The ESC essentially translated between the PWM and the motor requirements.

I've got a little more detail on my site here: http://blog.pistuffing.co.uk/?p=1128

Cheers,

Hove
www.pistuffing.co.uk - Raspberry Pi and other stuffing!

joefly
Posts: 64
Joined: Thu May 31, 2012 4:10 pm

Re: Controlling ESC (Electronic speed controller) with RPi.

Tue Jul 16, 2013 4:52 am

BOUNCYMAT:

Thanks for the code. It worked perfectly for me.

Question: your variable numb... How does it relate to pulse width. Others talk about 1-2ms. How does numb calculate to arrive at the ms?

Question for other: I got a bunch of harddrive motors taken out from useless computers. Are these not the same as RC brushless motors? I tried it with your code and tries to go but I get the feeling the pwm parameters are different than RC motors. Anybody have any idea how to get the code to work with hdd motors?

ofe
Posts: 47
Joined: Wed Jun 05, 2013 4:05 am
Location: Italy,Milan
Contact: Website

Re: Controlling ESC (Electronic speed controller) with RPi.

Fri Jul 19, 2013 9:07 am

Regarding recycled brushless:
This type of motor spins its outer shell around its windings, much like motors found in ordinary CD-ROM computer drives. In fact, CD-ROM motors are frequently rewound into brushless outrunner motors for small park flyer aircraft. Parts to aid in converting CD-ROM motors to aircraft use are commercially available.

from https://en.wikipedia.org/wiki/Outrunner
Oscar

Visit my quadcopter blog
http://solenerotech1.wordpress.com

WellardAW
Posts: 1
Joined: Sat Jul 20, 2013 5:47 pm

Re: Controlling ESC (Electronic speed controller) with RPi.

Sat Jul 20, 2013 5:55 pm

Hi,

Brilliant script, i have been searching for days for something like this, is it possible to drive a servo on pwm1 in the same script as the DC motor (pwm0) and assign different keys to turn it?

ofe
Posts: 47
Joined: Wed Jun 05, 2013 4:05 am
Location: Italy,Milan
Contact: Website

Controlling ESC - wiring

Thu Sep 05, 2013 3:37 pm

Hi, just to double check about the hardware setup suggested by bouncymat.
If understand correctly the rpi is powered by an external power supply, like the pc usb, while the esc is using the lipo.
so i need to connect on the esc only the white (signal) and the black (0v).Correct?
could it be not a problem to mix the ov from pc to the ov from battery?
(as you can image i'm just a poor mechanical engineer...) :D
Thanks
Oscar
Oscar

Visit my quadcopter blog
http://solenerotech1.wordpress.com

User avatar
joan
Posts: 14179
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Controlling ESC - wiring

Thu Sep 05, 2013 4:21 pm

ofe wrote:Hi, just to double check about the hardware setup suggested by bouncymat.
If understand correctly the rpi is powered by an external power supply, like the pc usb, while the esc is using the lipo.
so i need to connect on the esc only the white (signal) and the black (0v).Correct?
could it be not a problem to mix the ov from pc to the ov from battery?
(as you can image i'm just a poor mechanical engineer...) :D
Thanks
Oscar
You need to connect the grounds. If you don't the control signal from the Pi won't get to the ESC as there won't be a circuit.

ofe
Posts: 47
Joined: Wed Jun 05, 2013 4:05 am
Location: Italy,Milan
Contact: Website

Re: Controlling ESC (Electronic speed controller) with RPi.

Tue Sep 10, 2013 5:13 am

Hi finally I make it works!
Thanks for the support.
I put the wiring connection and the python code I used in a tutorial:
http://solenerotech1.wordpress.com/2013 ... pberry-pi/

Oscar
Oscar

Visit my quadcopter blog
http://solenerotech1.wordpress.com

vistauser
Posts: 8
Joined: Mon Mar 18, 2013 6:48 pm

Re: Controlling ESC (Electronic speed controller) with RPi.

Tue Sep 10, 2013 12:32 pm

Hi Oscar,
nice Approach to activate the ESC by Raspi.

I get an error from python:
Traceback (most recent call last):
File "motor_test.py", line 6, in <module>
from motor import motor
File "/home/pi/Prgs/copter/motor.py", line 48
else:
^
IndentationError: expected an indented block

Can you help me please?
Olaf

ofe
Posts: 47
Joined: Wed Jun 05, 2013 4:05 am
Location: Italy,Milan
Contact: Website

Re: Controlling ESC (Electronic speed controller) with RPi.

Wed Sep 11, 2013 3:41 pm

hi Olaf,
You r right.
it is necessry to comment line 46 and 48 by a #.
Anyhow I just modified the code on the server.

let me know hot it works now.
oscar
Oscar

Visit my quadcopter blog
http://solenerotech1.wordpress.com

vistauser
Posts: 8
Joined: Mon Mar 18, 2013 6:48 pm

Re: Controlling ESC (Electronic speed controller) with RPi.

Wed Sep 11, 2013 7:48 pm

Hi Oscar,
thank you for the answer, i changed this with the comments
but now a new stop:

Traceback (most recent call last):
File "motor_test.py", line 30, in <module>
res = raw_74input()
NameError: name 'raw_74input' is not defined
shutting down dma channel 0
clear_channel: channel=0

If i change raw_74input() to raw_input() the program is running and accepts
all the inputs from keyboard but the motor doesnt react.
ESC and motor are parts of a functioning MultiWii Copter.

Olaf

ofe
Posts: 47
Joined: Wed Jun 05, 2013 4:05 am
Location: Italy,Milan
Contact: Website

Re: Controlling ESC (Electronic speed controller) with RPi.

Thu Sep 12, 2013 11:17 am

Hi Olaf,
it can be caused by some different types of problem.
When I faced the same behaviour it was 1)no motor connected 2) no ground connected
Be the way my ESC did some beeps evenif I just connected teh power and motors (without connect rpi).

If we go trough my tutorial, it could be hardware problem:
*is the groud cable connected ,and the vcc has not to be connected on GPIO?
*do you know what is your esc start up procedure?
*Are you using the same pin I used for PWM ? pin 11=GPIO17
it could be software problem:
*do you run program with sudo?
*do yuo install RPIO library on your rpi? (you can see it if in the terminal appers
"Using Hardware: PWM..."
*After the first ENTER press, can you see the message add_channel_pulse width=200
*After that do you conenct the esc power?

And finally it could be something on my code,of course.I just teted on mine rpi Mod B.
Let me know.
Oscar
Oscar

Visit my quadcopter blog
http://solenerotech1.wordpress.com

vistauser
Posts: 8
Joined: Mon Mar 18, 2013 6:48 pm

Re: Controlling ESC (Electronic speed controller) with RPi.

Thu Sep 12, 2013 11:49 am

Hi Oscar,
thanks for your support again.

Be the way my ESC did some beeps evenif I just connected teh power and motors (without connect rpi).
- yes it does tic - tic - tic ..., no audible beeps

If we go trough my tutorial, it could be hardware problem:
*is the groud cable connected ,and the vcc has not to be connected on GPIO?
- yes

*do you know what is your esc start up procedure?
- no, I only know the meaning of the connections. (TURNIGY Plush 18A)

*Are you using the same pin I used for PWM ? pin 11=GPIO17
- yes

it could be software problem:
*do you run program with sudo?
- yes

*do yuo install RPIO library on your rpi? (you can see it if in the terminal appers
"Using Hardware: PWM..."
- yes

*After the first ENTER press, can you see the message add_channel_pulse width=200
*After that do you conenct the esc power?
- yes, and the program is reacting fine on the input of the a and z

And finally it could be something on my code,of course.I just teted on mine rpi Mod B.

May be my BEC doesnt understand the output of pin 11=GPIO17 (if there is an output, hi)
Also RPi ModB here.

Good luck Oscar with all your projects
Olaf

ofe
Posts: 47
Joined: Wed Jun 05, 2013 4:05 am
Location: Italy,Milan
Contact: Website

Re: Controlling ESC (Electronic speed controller) with RPi.

Sat Sep 14, 2013 9:11 am

no other ideas.
just test the output with a scope or try a different pin.
oscar
Oscar

Visit my quadcopter blog
http://solenerotech1.wordpress.com

Return to “Interfacing (DSI, CSI, I2C, etc.)”