Close

Code

A project log for ELo (Electric Longboard)

A pretty smart electric longboard.

joeJoe 04/28/2016 at 04:430 Comments

I got around to start writing a basic feature list and some pseudocode for the longabord, which will all be written in Arduino running on the Adafruit feather M0 with Bluefruit. I have uploaded the document to the files section but also want to go over the code here.

First off a basic feature list:

  1. Failsafe mechanisms (Connectivity loss, rider falls off, user indicates an error, low voltage)
  2. Smooth acceleration/deceleration ability
  3. Error state to enter and stop the board in case of one of the failsafes triggering
  4. LED control

Now for some pseudocode to accomplish these goals:


Initialization of variables

Variables for speed/throttle
Variables for LED color
Declare ESC Servo object
Variables for sensors
There will be variables for the speed and throttle, at least two. These two will be the current speed, obtained from the RPM sense wire coming from the ESC, and the desired speed or throttle, coming from commands from the bluetooth connection to the phone app. LEDs will be declared, and the ESC will exist as a Servo object as it needs to be sent PWM for throttle control. There will also be other sensors such as 2 piezoresistive force sensors, and potentially voltage sensors, which would be analog inputs.

void setup ()

Attach ESC to correct pin
Attach LEDs to correct pins
Start Serial for debugging
The ESC Servo object will be attached to a pin as well as the LEDs to their respective pins. The Serial interface will also be started to send information to either the computer or phone if debugging is needed.

void loop () (main loop)

Call startup function if all conditions are met
	Set speed to 0
Call run loop
	run loop loops within itself, only exits if base/error conditions are met
Here are the first instances of "startup function" and "run loop," which are just names at this point but will be described later. The main loop of the code will basically exist to call other loops, but it is important because if the run loop throws an error, calls the "deceleration loop," and breaks, we want it to run the startup function again to start everything over.

run loop

Run forever
Read all sensor data
Read all throttle/speed data
Read throttle command from app, reassign to desired speed variable every loop
	Calculate difference between current speed and desired speed
	 Write first step above/below current speed (fixed step size)
Delay fixed time period (probably 50-200ms)
If any bases cases are met (sensors, connection, user command)
	Call deceleration error loop
	break (Will break to main loop, which will loop around to startup function)
The run loop is where most of the time will be spent while riding. It is called after the startup function, and loops while everything is "OK," only changing throttle when prompted by the user. It will first read the sensor data from the force sensors, then get the current speed from the RPM sense wire, and also read the current speed command from the app. This "desired speed" should be reassigned to the same variable every time the loop runs to ensure the user can change speed before the board actually gets to that speed. It should then calculate the difference between the current speed and the desired speed, and then move in the correct direction by telling the ESC to run at a speed a fixed step size higher or lower than the current speed. This happens only once per loop so the board can accelerate or decelerate at a comfortable pace, but also continuously check to see if anything has gone wrong. The delay is built in so the loop only runs ideally 5-20 times per second, instead of 1000s of times per second so the board doesn't jump to the desired speed and leave the rider in the dust (The motor can go 0-full speed (albeit, unloaded) in 300us, according to the spec sheet). The run loop then checks the numerous "failsafe" cases, if both force sensors read below a threshold (rider has fallen off), if connection is lost, and if the user wants to board to slow down immediately. If any of these conditions are true, the loop will call the "deceleration loop" and then break out to the main loop.

deceleration loop

Called by run loop with 1 arg passed by value, last sent speed
While speed > 0
Read last sent speed
	Calculate difference between current speed and 0
	 Write first step below current speed (use fixed step size)
Store this last sent speed, used by loop for next write to ESC
Delay fixed time period (probably 50-200ms)
Break to run loop (which will then break to main loop) after speed goes to 0
This is the "error state" loop so to say, as whenever an erroneous or bad condition is met, this function is called and decelerates the board, then breaks out to the main loop, which as you recall, will run the startup function before letting the run loop go again.

startup function

While 1 > 0
Check to make sure all base cases are ok and board is ready to go
	Connection
	User wants to go ahead (no kill switch flipped basically)
	Sensors read ok
If yes, set speed to 0, cool LED sequence proceeds to run loop
If no, continue looping
Delay 500ms
Will run forever until conditions to enter the run loop are met. Plan is to run this twice a second but it could probably be a lot quicker.

That's it so far, and hopefully I will get to actual code tomorrow.

Discussions