Monday, March 9, 2015

Raspberry PI Controlling the ESC's!!

Today I finally did it! There was a good amount of research and an even better amount of banging my head on the table but I finally figured it out!

There was a lot of Google searching and I slowly figured out what I needed to do in order to get the electronic speed controllers to respond to a program from the Raspberry PI. As you'll recall from previous posts, I was wondering if there was some kind of boot sequence that each ESC goes through before they start spinning the motors. You may also recall that I mentioned some kind of programming mode in an earlier test but I had no clue what was going on. There are some helpful links I eventually found that were particularly helpful:

With this information, I started my test program. I would start the program with PWM output at 0. But I later found that this was incorrect. The ESC's wouldn't initialize. So I thought back to the test I did where I plugged in the ESC's to the RF receiver. I wasn't sure but one thought I had was that perhaps the RF receiver wasn't starting off at a PWM output of 0. I needed to figure out what it WAS sending. So I started looking into ways to read PWM signals and came across this page.


I chose to use the first option and I already had a few Arduino's lying around so I went ahead and constructed my PWM measurement device. Upon hooking this thing up, I found that my transmitter and receiver had a PWM output of about 1020 at low throttle and somewhere around 1950 at full throttle. (or something like that) So it makes sense to set the output to 1020 as well, right?

Well, when using the Adafruit library, you also get this amazing function called setPwmFreq which, as the name suggests, sets the frequency for the PWM. Apparently when I just typed in 1020 as the PWM output, I was getting a VERY different duty cycle output. After some frustration, I decided to hook up my PWM reader to my Raspberry PI and see what I'm actually sending. I was right. It was WAY off. So I did some research on what PWM frequency most ESC's use and found this page:


Basically, my program was using 60Hz but I needed to be much higher than that! According to this link, I should be more in the 300Hz - 400Hz range. So I changed my test program to use 400Hz and my output didn't match what I was typing. In code, I told the control program to output a signal of 1020 but it was too high. So leaving the output set at 1020, I brought down the frequency until the actual output matched what I saw in code. Once the PWM output was closer to 1020, I called it good. In the end, I was around 214Hz for my frequency.

For the test program, I wanted to start everything off on low and then spin up one motor at a time. I set the PWM output to be 1020 on each ESC and then set the output to be 1900. After one second, I would take it back down to 1020 and move on to the next motor. I ran the program and was extremely excited because it finally worked! The results are in the video below.



Now that I have the motors responding to signals being sent from the Raspberry PI, it's time to start using the MPU-6050 to get orientation data.

Friday, March 6, 2015

Progress on Electronic Speed Control Research

Last night, I was cleaning up my work space and found the RF receiver that was used in the old quadcopter that was used for parts. I noticed the receiver had 6 channels and 6 rows of pins. Each row had 3 pins. 3 pins is how I do PWM... (Signal, GND, +5v)
It would make sense to send all signals from part to part all using the same protocol. After all, these parts are used by aviation enthusiasts. They probably don't want to waste a bunch of time converting from one protocol to the other...
After some Youtube research, I found some content that suggested I was right. I hooked up the receiver to the motors and powered everything. I could then move the remote joysticks and spin up the motors individually! Now we're getting somewhere! Since they can only spin one direction, I took this opportunity to rewire the motors so they spin the right way. This diagram helps to show the correct rotation direction for each motor.
I still need to figure out what pwm signals are being sent to control the ESC's. I would also like to figure out if there is any kind of boot-time calibration that happens. It seemed like each motor had a different max speed as well but that might have something to do with the way I have been Jerry-rigging the motors to the receiver...
Here are the results of this work:

Wednesday, March 4, 2015

Early Software Setup

With the physical setup pretty much out of the way, I moved on to the software setup. As I mentioned in an earlier post, I chose to use the Adafruit 16 channel PWM hat and that took a little setup. There are some steps to be taken to be able to use I2C on the Raspberry PI. To set this up yourself, take a look at Adafruit's I2C setup tutorial and then their pwm hat setup tutorial. I downloaded some of their test code to make sure that I set everything up right and it worked! The only issue was that it didn't work when I replaced my test servo with one of the Electronic Speed Controller (ESC) control wires. The motor would beep which didn't make any sense and I later found out that I was somehow entering some kind of programming mode. But there was another issue I had to work out...

The original intention of this project was to write my flight control program in C++. The issue is that the PWM hat control library from Adafruit is in Python. Then I found some test code for the MPU-6050 Gyroscope and Accelerometer chip but that was also in Python. I looked around to see if anyone had ported the library to C++ and found that Adafruit made an Arduino library. This was interesting because Arduino code is basically C so I figured I could use it and convert it to C++ libraries and it should work! right? Well, it turned out that the library for Arduino has the Wire.h library as a dependency. This doesn't work for Raspberry PI. I then thought I could port Adafruit's Arduino library to Raspberry PI C++ by using Gordon Henderson's WiringPi project. And I must say, this library is VERY nice!! I install it on every Raspberry PI I use! So all I would have to do is change any reference from using Wire.h and tell the library to use wiringPi's I2C functionality. This may actually work but not in the time frame for my Capstone project. This needs to be presentable by April 20th or so. That is just before I present for Scholar's Day at BVU.

So, a day later, I decided that it would be wiser to just write my early flight controller programs in python rather than in C++. This was due to the fact that there are already libraries written for the PWM board and example code for the MPU-6050 all written in python. There is also not a library to use the PWM board written in C or C++ so this was the best option. So, rather than rewriting two pre-existing libraries, I just change the language of my flight control program to python for now.

For my Capstone checkpoint next week, I want to solve the ESC power problem. I need to get the PI to spin up each motor in turn. Once I get this done, I need to attach the MPU-6050 orientation sensor. Once I get values from this sensor read into the PI, I want to make a very basic first attempt at a flight control program. If one side is lower than the other, that side spins faster. No fine tuning yet but some response to real world environment.