Close

More Code

A project log for ELo (Electric Longboard)

A pretty smart electric longboard.

joeJoe 08/24/2016 at 21:120 Comments

I have taken the pseudocode and created version 1.0 of the code. Since last time, I have decided to take a slightly different path and further complicate the electronics and software side of things. Since I want the connection for commands from my phone to be constantly polled, I was struggling with combining all parts of the code into one file to run on one board. As such, I have moved to a two-board idea where a 5V 16Mhz Adafruit Trinket controls the motor, reads sensor data, and has most of the features the pseudocode mentioned, and the 3.3V Adafruit Feather Bluefruit M0 handles polling for commands and converting these into meaningful signals for the Trinket to read.

In the future, it may be possible to combine all parts of the code into the Feather, but for now this is the approach I am taking. The Trinket will have 3 analog inputs, 2 for the force sensors and 1 for the speed command from the Feather. It will have one digital input from the Feather as a killswitch pin to cause the Trinket to initiate the deceleration loop if it goes high. It will have one digital output as the PWM output to the ESC.

The Feather will have an analog output to the Trinket for speed control, and an analog input from the ESC for the current speed (just realized this may be a 5V signal while the Feather is 3.3V logic). It will be commanded by the Bluefruit companion app with 3 commands; 1) increase speed 2) decrease speed and 3) stop the longboard.

My current concerns involve the difference in logic voltage levels between the two boards, the increasingly complex nature of the whole electronics side of things, and controlling the ESC with just a simple PWM.

/*********************************************
 * Note the numbering of analog pins: Pin 2 is Analog 1, Pin 3 is Analog 3, Pin 4 is Analog 2. 
 * For the Uno, the terms A1, A2, and A3 are mapped for you. For ATtiny85's, they are not. 
 * So for the pinMode calls, use the Pin number (stenciled on Trinket), for analogRead, use the analog number.
 * 
 */


#include <Servo.h>


int desiredSpeedFrmCtrl; //analog input connected to analog pin #2
int force1; //first force sensor (analog) connected to analog input pin #4
int force2; //second force sensor (analog) connected to analog input pin #3
int forceThres; //threshold for force sensors to cutoff and read an error
int forceVal1;
int forceVal2;
int stopPin = 0;
int stopSpeed; //speed variable for the deceleration loop to use
Servo ESC; //ESC object to be able to write PWM to




void setup() {
  // put your setup code here, to run once
  ESC.attach(0);
  pinMode(2, INPUT); //analog input
  pinMode(3, INPUT); //analog input
  pinMode(4, INPUT); //analog input from auxillary board for BLE commands
  pinMode(0, INPUT);
  forceThres = 400; //arbitrary threshold until the sensors are calibrated
  Serial.begin(115200);


}


void loop() {
  // put your main code here, to run repeatedly
  startupLoop();
  runLoop();
}


void runLoop() {
  //code for the main running loop of the board
  forceVal1 = analogRead(2);
  forceVal2 = analogRead(3);
  while((forceVal1 > forceThres) || (forceVal2 > forceThres)){
    desiredSpeedFrmCtrl = analogRead(1);
    ESC.write(desiredSpeedFrmCtrl);
    
    Serial.print("Speed sent to ESC: "); //for debugging purposes (speed read from feather and sent to ESC)
    Serial.print(desiredSpeedFrmCtrl);
    Serial.println();
    
    forceVal1 = analogRead(2);
    forceVal2 = analogRead(3);


    if(digitalRead(0) == HIGH){ //if there was a command from the controller to stop the decel loop is called
      decelerationLoop();
      return; //the runLoop breaks becak into the main loop
    }
    
  }
}


void startupLoop() {
  //code to determine if the board can startup
  while (1 > 0){
    forceVal1 = analogRead(2); //read the analog value from the first force sensor
    forceVal2 = analogRead(3); //read the analog value from the second force sensor
     if((forceVal1 > forceThres) && (forceVal2 > forceThres)){
      ESC.write(0); //set speed to 0
      return; //exit to main loop
     }
     delay(200);
  }
}


void decelerationLoop (){
  //code to slow the board down if there is an error
  stopSpeed = desiredSpeedFrmCtrl;
  while(stopSpeed > 0){
    ESC.write(stopSpeed - 200); //arbitrary step size until the ESC is calibrated
    delay(200); //wait so the deceleration is smooth
  }
}

Discussions