Sunday, April 19, 2020

Arduino code for Spark EV PWM controller

///////////////////////////////////////////////////////////////////////
// PWM motor controller Part 7
// Built for Spark EV DC controller
// Arduino Adafruit Trinket Pro 5V
//
// Changing PWM output frequency to 8kHz
//
// Analog designation:
// A0 throttle input
// 10 motor PWM output (digital output)
// A3 current 1 sense input
//
//
// Designed for Adafruit Trinket Pro 5V on the generic test board
// By Travis Shorten
// 4/19/20
// Rev -
//
//
///////////////////////////////////////////////////////////////////////


//constants
const int motor_delay = 10; // System clock for updating output
const int numReadings = 4; // this is the number of reading for smoothing
const int motorout = 10; // output pin for motor pwm
const int throttle_in = A0; // input pin for throttle
const int motoramp_in = A3; // motor amperage input
const int motorampmax = 98; // motor amperage is 3x per phase! 100A phase current is 300A total
// cont. 2.5V=0A=128, 2.85mV/A 100A*0.00285V/A=0.285V
// cont. 2.5V-0.285V=2.215V at the sensor output. 2.215V*128/2.5V=113 at the A/D
// bit = (128-0.146*Amperage)
// Amp = (128-bit)/0.146)
// 98 = 600A total
// 113 = 300A total
// 123 = 100A total
const int motorup = 2; // motor throttle up increment
const int motordown = 2; // motor throttle down increment
const int motorlimit; // motor throttle down increment with current limit

//variables and initial values
unsigned int mthr = 0; // throttle value to write to pwm, pos only
unsigned int mthr_inv = 255; // throttle value to write to pwm, pos only
int pedal_read = 10; // pedal_read
unsigned int pedal_ave = 0; // pedal average is set to 0. This sets PWM duty. Pos only
int motor_torque = 128; // motor output torque is set to 0

// average table listings
int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
int total = 0; // the running total



void setup(){
TCCR0B = TCCR0B & B11111000 | 0x02;
TCCR1B = TCCR1B & B11111000 | 0x02;
TCCR2B = TCCR2B & B11111000 | 0x02; // 0x01 1 31372.55 changes PWM output frequency for pin 9/10
// 0x02 8 3921.16
// 0x03 64 490.20 <--DEFAULT // 0x04 256 122.55 // 0x05 1024 30.64 pedal_read = analogRead(throttle_in) / 4; while (pedal_read >= 20){
pedal_read = analogRead(throttle_in) / 4;
delay(100);
}
}

void loop(){

//***************************************************
// master throttle reset for over current. Reduces throttle
// with while as long as over current is sensed
//***************************************************


//the 0A current voltage is 2.5V. Going down increases amperage to the motor

//uses stock PWM control. mthr goes to pwm

motor_torque = analogRead(motoramp_in) / 4;
while (motor_torque < motorampmax) { mthr = mthr - motordown; mthr_inv = 255 - mthr; analogWrite(motorout, mthr_inv); motor_torque = analogRead(motoramp_in) / 4; } //***************************************************

// average matrix

//***************************************************





total = total - readings[readIndex];
readings[readIndex] = analogRead(throttle_in) / 4;
total = total + readings[readIndex];
readIndex = readIndex + 1;
if (readIndex >= numReadings) {
readIndex = 0;
}
pedal_ave = total / numReadings;

//***************************************************
// if pedal ave is greater than master throttle
//***************************************************

if (pedal_ave > mthr){
mthr = mthr + motorup;
}

//***************************************************
// if pedal ave is less than master throttle
//***************************************************

if (pedal_ave < mthr){ //if the pedal is lower, reduce motor output mthr = mthr - motordown; } //*************************************************** // if pedal ave is less than min allowed //*************************************************** if (mthr <= 3){ //if the pedal is higher, increase motor output mthr = 3; } //*************************************************** // if pedal ave is greater than greater than max allowed //*************************************************** if (mthr > 252){
mthr = 252;
}

mthr_inv = 255 - mthr; // high voltage control board is active low. 0% duty is wide open
analogWrite(motorout, mthr_inv); //uses stock PWM control. mthr goes to pwm

delay(motor_delay); //adds a little delay to motor speed adjustments to prevent jarring drivetrain

}