Skip to content

Instantly share code, notes, and snippets.

@tomeaton17
Created May 15, 2018 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomeaton17/40cf5a9bfc8c5dc26fb65d43e37b7b39 to your computer and use it in GitHub Desktop.
Save tomeaton17/40cf5a9bfc8c5dc26fb65d43e37b7b39 to your computer and use it in GitHub Desktop.
/*
Name: BatteryLogger.ino
Created: 5/14/2018 11:02:28 AM
Author: tomea
*/
#include <Servo.h>
Servo myServo;
const float PD_FACTOR = 3.504943957968f;
float curFactor = 1.0f;
int32_t voltageBAT_ADC, voltageCUR_ADC, counter;
float voltageBAT, voltageCUR, voltageBAT_a, voltageCUR_a;
bool shouldSpin = 0;
int incomingByte = 0;
int val = 5;
// the setup function runs once when you press reset or power the board
void setup() {
Serial.begin(9600);
// Averaging loop
while (counter <= 1000)
{
voltageBAT_ADC += analogRead(A1);
voltageCUR += analogRead(A0);
counter += 1;
}
// reset averaging counter
counter = 0;
// calculated scaled voltage
voltageBAT_a = (voltageBAT / 1000.0) * (5.0 / 1023.0);
voltageCUR_a = (voltageCUR / 1000.0) * (5.0 / 1023.0) * curFactor;
// calculate current factor. At 0A, voltage should be 0.5V, so this makes sure of that
curFactor = 0.5f / (voltageCUR_a);
// reset averaging variables
voltageBAT = 0;
voltageCUR = 0;
// initialise servo
myServo.attach(9);
myServo.writeMicroseconds(1000);
delay(2000);
}
// the loop function runs over and over again until power down or reset
void loop() {
// check is Serial data available
if (Serial.available() > 0)
{
// read incoming byte
incomingByte = Serial.read();
if (incomingByte == 83)
shouldSpin = 1;
if (incomingByte == 115)
shouldSpin = 0;
if (incomingByte == 43)
{
val += 5;
//Serial.println(val);
if (val > 100)
val = 100;
}
if(incomingByte == 45)
{
val -= 5;
if (val < 0)
val = 0;
}
}
// map 0-100 to ESC input range
int writeVal = map(val, 0, 100, 1000, 2000);
// write PWM value to ESC
if (shouldSpin)
{
myServo.writeMicroseconds(writeVal);
}
else if (!shouldSpin)
{
myServo.writeMicroseconds(1000);
}
// averaging loop
while (counter <= 1000)
{
voltageBAT_ADC += analogRead(A1);
voltageCUR_ADC += analogRead(A0);
counter += 1;
}
// reset averaging counter
counter = 0;
// calculate scaled voltages
voltageBAT_a = (voltageBAT_ADC / 1000.0) * (5.0 / 1023.0) * PD_FACTOR;
voltageCUR_a = (voltageCUR_ADC / 1000.0) * (5.0 / 1023.0) * curFactor;
// reset averaging variables
voltageBAT_ADC = 0;
voltageCUR_ADC = 0;
// if ACS770 voltage < 0.5 something has gone wrong, so clamp to 0.5V if it goes below
if (voltageCUR_a < 0.5)
{
voltageCUR_a = 0.5f;
}
// if battery voltage goes below 0V something has gone wrong, so clamp to 0V. Unlikely to happen!!
else if (voltageBAT_a < 0.0f)
{
Serial.println("Battery voltage below 0V!!!");
voltageBAT_a = 0.0f;
}
// get actual current using current/mV factor found in ACS770 datasheet.
float current = (voltageCUR_a - 0.5) / 40e-3;
// Send data over serial. Data sent in format BAT_V,CUR_V,CUR_A\n
Serial.print(voltageBAT_a, 4);
Serial.print(",");
Serial.print(voltageCUR_a, 4);
Serial.print(",");
Serial.println(current, 4);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment