piRotor - Raspberry Pi Quadrotor : Part 1
Building a Raspberry Pi Zero powered quadrotor, drone thingy
This is a project I’ve been looking to start for a while and I’ve finally got started on an initial prototype. The idea is to build a drone that uses a Raspberry Pi as the flight controller and to build something that will take off, hover and land without breaking something. I don’t think I’ll be able to use it for FPV racing or anything that requires quick input, I’m aiming for autonomous flight in good conditions.
To get started I gathered a few parts:
- Raspberry Pi Zero
- Adafruit 16-Channel PWM / Servo HAT
- Afro ESC 12Amp BEC UltraLite Multirotor ESC V3 (SimonK Firmware)
- DYS BE1806-2300kv Brushless Multirotor Motor 3S~4S
- Gemfan 5030 GRP/Nylon Propellers CW/CCW Set (Black) 5 x 3
- Turnigy nano-tech 1300mah 2S 25~50C Lipo Pack
For this first round of prototyping the goal was to get the Raspberry Pi to control a motor! Sounds simple enough!
Step 1 - Put all the parts together!
The first stage of the project was to solder (Which I haven’t needed to do since I was in school) the header pins to the Pi and PWM Hat, then stack them together.
Once finished you should have something that looks like this:
(Note in the image above I have already connected the ESC to channel 0 of the PWM Hat)
The ESC then connects to the motor and power supply. In the image below the white and red crocodile clips are connecting the power supply. The ESC to motor connectors are pretty self explanatory.
Step 2 - Powering everything
The trickiest part of this prototype was working out how to power everything. (pro tip: don’t jam ESC power wires into a LiPo battery, sparks may fly, Colins may cry)
- Raspberry Pi - Connected to mains using official raspberry pi power supply
- PWM HAT - Requires an additional 5-6v power supply, we hooked ours up to an old PC power supply, will be connected to the drone battery when we work out how.
- ESC - Tried to connect to the same 5v power supply, but didn’t work, crocodile clips in the battery (7.4v), connected to the ESC did the trick. When the green LED illuminated on the ESC, the celebrations were mighty.
In terms of powering on everything, the order that worked for us was:
- PWM HAT
- Raspberry Pi
Step 3 - Preparing the Pi
I followed Adafruit’s guide for setting up the Pi with the necessary libraries - https://learn.adafruit.com/adafruit-16-channel-pwm-servo-hat-for-raspberry-pi/overview
The only issue was the GitHub repo they mention for some sample code seems to have moved to here - https://github.com/adafruit/Adafruit_Python_PCA9685
Step 4 - Writing the code
The first step in the code is to ‘arm’ the ESC, this involves sending a PWM signal to channel 0, that is in the neutral or power-off area, the Afro ESC datasheet describes this as:
For PWM input, motor stop / arm below 1060μs
I found a helpful formula for converting this 1060 to something that the PWM library can use:
1060 / (1000000.0/4096/60) = 260.5056
The code in
pirotor.py looks like this:
print('Arming motor') pwm.set_pwm(0,0,260) time.sleep(5
This causes the ESC led to illuminate green and emit a reassuring bleep, matching what the datasheet describes: “the ESC will arm (long beep), and the green LED will light”
I then tried setting the pwm value to 560:
But this caused the motor to jump and stop, and the ESC light turned off. The datasheet describes the full power as 1860μs, which when we pass into our formula:
1860 / (1000000.0/4096/60) = 457.1136
Which is less than 560, so that’s why it didn’t work!
You also can’t jump straight to full speed, you got to ease it in, our final code block for controlling the motor speed ended like this:
print('Arming motor') pwm.set_pwm(0,0,260) time.sleep(5) print('Starting motor at 300') pwm.set_pwm(0,0,300) time.sleep(3) print('increasing to 360') pwm.set_pwm(0,0,360) time.sleep(3) print('increasing to 400') pwm.set_pwm(0,0,400) time.sleep(3) print('increasing to 450') pwm.set_pwm(0,0,450) time.sleep(3) print('stopping') pwm.set_pwm(0,0,260)
This gradually gets the motor up to full speed then stops.
The full sample code can be found in the piRotor repo
Next step is to get some sensors and try and pull data from them, and use this to control what the motor needs to do to stay level.