Since there seem to be quite a few questions about the controller I thought I’d share some early code that went into the first prototype.
Important note: The code running in the first production versions has been completely rewritten. We will share that when those are shipping.
There are few main features I’m going to highlight:
- Throttle Arming
- Throttle Smoothing
- Battery Voltage Display
One of the first safety features we worked on was making sure when turning on the unit there was no way the motors would instantly spin up.
Gas systems have attempted to tackle this issue with devices like Scouts safe-start module but they are slow to react and damage may already have been done after a few seconds.
checkArmRange function is called on boot up and before the ESCs are initialized. (Most ESCs also have high throttle on boot protection built in as well). The function simply loops and checks roughly once a second to make sure the throttle/potentiometer is at zero (or within the margin of error)
As @Pdwhite talked about in his video throttle smoothing is important because of how responsive electric motors are compared to (one big) gas one. Also because we are reading the throttle position from a simple analog potentiometer noise interference is a small factor.
Luckily the hard part was already done and with the help of the ResponsiveAnalogRead library. I simply had to initialize it to read from the throttle input, tweak a few things, and it was ready to go.
Battery Voltage Display
This was a fun one. We wanted a simple way to tell how much “fuel” was left. Without going into too much detail the most accurate way to do this is to measure amperage flow but voltage gives the pilot a good idea.
Since the Arduino only accepts the max of 5.0V input a voltage divider was needed to step down the 50V batteries.
handleBattery function takes care of converting everything between “empty” and “full” voltage reading to increments of 10% - since there are 10 LEDs. When the voltage gets down to 30% and 20% the lights change from green to yellow and red respectively.
Check out the main
loop function. This is what is run after everything is booted up. You’ll notice that it handles the throttle each time through but only checks the battery every once in a while. This is important because many simple embedded systems only run in one thread so they can only do one thing at a time. Reading the battery etc. is less important than controlling the throttle so that’s why it is done less often.
As you might guess reading and writing to multiple inputs and outputs in a time-sensitive manner gets pretty complicated. That’s where a real-time operating system comes (RTOS) in such as FreeRTOS
As stated before this code was primarily created for testing and getting a few prototypes off the ground and flying. You’re welcome to use it in your own builds but I were you I’d wait for the official production code to be released
Let me know if you have any questions!