Meghan FragomeniGreta PowellDylan CollinsJustin Watts
Created April 19, 2023

MEGR 3092 - Electric Drive Trains

The goal of this project is to dive into electric drive trains and improve the original system while also incorporating instrumentation

41
MEGR 3092 - Electric Drive Trains

Things used in this project

Hardware components

Argon
Particle Argon
×1
Songhe BTS7960 43A High Power Motor Driver
×1
Stage V Super Speed Motors/Gearboxes for C7 Corvette, Mustang, and Porshe
×1
Rechargeable Battery, 24 V
Rechargeable Battery, 24 V
×1
Wheels
×1
Electric Gas Pedal
×1
HiLetGo ACS758 Linear Current Sensor
×1
Breadboard (generic)
Breadboard (generic)
×1

Software apps and online services

Particle Build Web IDE
Particle Build Web IDE
Adafruit

Hand tools and fabrication machines

Butane Torch
Wire Cutters
Wire Strippers
Soldering Iron
Butt Connectors

Story

Read more

Code

Electric Drive Trains: Variable Throttle and Slew Traction Control

C/C++
// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_IO_Particle.h>
#include <Adafruit_DHT.h>
#include "lib1.h"
#include <Adafruit_IO_Particle.h>
#include <Adafruit_DHT.h>

// This example assumes the sensor to be plugged into CONN2
#define DHTPIN 2     // what pin we're connected to

// Here we define the type of sensor used
#define DHTTYPE DHT11        // DHT 11 

#define IO_USERNAME  "gretapowell"
#define IO_KEY       "aio_NTEO38U25j0SeHgNG7xLSThSaQiH"

SYSTEM_THREAD(ENABLED);


// Define pins for analog input
int Current_Sensor = A1;
int Voltage_Sensor = A3;
int pedalValue = 0;

// Define the maximum rate of increase in throttle per loop
int THROTTLE_RATE =1; 
int prevThrottle = 0;
int throttleOutput = 0;
int throttleInput=0;

// Define variables for raw analog values
int analog_value_0 = 0;
int analog_value_1 = 0;

// Voltage Divider
int R1 = 3800;
int R2 = 1000;
int acceleration = 0;
int previous_speed = 0;
int acc_limit = 5;
int speed;

// Define variables for filtered analog values
float filtered_value_0 = 0.0;
float filtered_value_1 = 0.0;

// Define variables for filtering algorithm
float alpha = 0.0183;
float prev_filtered_value_0 = 0.0;
float prev_filtered_value_1 = 0.0;

// Time variables
unsigned long previous_time = 0;
unsigned long current_time = 0;
unsigned long filter_time = 10000; // in milliseconds (10 seconds)


// Pedal pin
const int PEDAL_PIN = A0;

// PINS
const int Dir_Pin = D2; 
const int PWM_R = D3; 
const int PWM_L = D4;
const int ENA = D5;
const int ENB = D6;
int outputTimer = 0;
int OUTPUT_INTERVAL = 10000;



void setup()
{
  // Initialize serial communication
  Serial.begin(9600);
 
 // WEBHOOK SETUP
         
  // Subscribe to the integration response event
  Particle.subscribe("hook-response/current", myHandler, MY_DEVICES);
  Particle.subscribe("hook-response/voltage", myHandler, MY_DEVICES);
  Particle.subscribe("hook-response/throttle", myHandler, MY_DEVICES);

  // Set motor pins as outputs
  //initHBridge();
  initHB();

  // Set pedal pin as input
  pinMode(PEDAL_PIN, INPUT);
  
  
  // Set current sensor input
  pinMode(Current_Sensor, INPUT);
  pinMode(Voltage_Sensor, INPUT);

}

void myHandler(const char *event, const char *data) {
  // Handle the integration response
}


void loop()
{
 
    // Motor direction     
      int direction = digitalRead(Dir_Pin); 
      
    // Motor speed  
      if (direction = HIGH){
          digitalWrite(ENA, HIGH);
          digitalWrite(ENB, LOW);
          analogWrite(PWM_R, throttleOutput);
      }
      else{
          digitalWrite(ENA, LOW);
          digitalWrite(ENB, HIGH);
          analogWrite(PWM_L, throttleOutput);
      }
     
  
  // Voltage and Current Readings
  
  // Read analog values at 50 Hz
  current_time = millis();
  
  float _voltage = 5.0;
  
  if (current_time - previous_time >= 20)
  { // 1000 ms / 50 Hz = 20 ms
    analog_value_0 = analogRead(Current_Sensor);
    analog_value_1 = analogRead(Voltage_Sensor);
    previous_time = current_time;

    // Convert analog values to voltage/current
    float voltage_0 = (analog_value_0 * _voltage / 4095.0) * ((R1 + R2)/R2); // 5V reference, 10:1 divider
    float current_1 = (analog_value_1 - 2047) * _voltage / 4095.0 / 0.04; // ACS758LCB - 050B - PFF - T current sensor

    // Filter analog values
    filtered_value_0 = alpha * voltage_0 + (1 - alpha) * prev_filtered_value_0;
    filtered_value_1 = alpha * current_1 + (1 - alpha) * prev_filtered_value_1;
    
    prev_filtered_value_0 = filtered_value_0;
    prev_filtered_value_1 = filtered_value_1;

    
      // Read pedal value
      pedalValue = analogRead(PEDAL_PIN);
      
      
      // Map pedal value to motor speed
      speed = map(pedalValue, 1050, 3100, 0, 255);
      speed = constrain(speed,0,255);
      
      throttleInput = speed;
      // If accelerating, slew traction control to speed up
      if (throttleInput > prevThrottle)
      {
        throttleOutput = min(throttleInput, prevThrottle + THROTTLE_RATE);
      }
      // If de-accelerating, slew traction control to slow down
      else {
        throttleOutput = max(throttleInput, prevThrottle - THROTTLE_RATE); 
      }
      prevThrottle = throttleOutput; 

    
  }
 
 
 // Check if it's time to output the filtered values

if (millis() - outputTimer >= OUTPUT_INTERVAL) {

// Output the filtered values

//++++++++++++++++++++++++++ WEBHOOK INTEGRATION START +++++++++++++++++++++++++++
   
// Get some data
  float current = filtered_value_0;
  float voltage = filtered_value_1;
  float throttle = speed;
  // Trigger the integration
  Particle.publish("current", String(current), PRIVATE);
  Particle.publish("voltage", String(voltage), PRIVATE);
  Particle.publish("throttle", String(throttle), PRIVATE);

//+++++++++++++++++++++++++ WEBHOOK INTEGRATION END ++++++++++++++++++++++++++++

// Reset the output timer

outputTimer = millis();
}

}

// HBridge setup
void initHB(){
    pinMode(ENA, OUTPUT);
    pinMode(ENB, OUTPUT);
    pinMode(PWM_R, OUTPUT);
    pinMode(PWM_L, OUTPUT);
    pinMode(Dir_Pin, INPUT_PULLDOWN);
    
    analogWrite(PWM_R, 0);
    analogWrite(PWM_L, 0);
}

Credits

Meghan Fragomeni
2 projects • 2 followers
I am a mechanical engineering student at UNC Charlotte.
Contact
Greta Powell
2 projects • 1 follower
I am also a Mechanical Engineering student at UNC Charlotte
Contact
Dylan Collins
2 projects • 0 followers
Contact
Justin Watts
2 projects • 0 followers
Contact

Comments

Please log in or sign up to comment.