Skip to content

removed pin underscores in iomanagement.h #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion embedded-pio
Submodule embedded-pio updated 3 files
+31 −31 adc/adc.cpp
+27 −0 dac/dac.cpp
+10 −0 dac/dac.h
13 changes: 7 additions & 6 deletions include/IOManagement.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,26 @@
#define __IO_MANAGER_H__

#include "PID.h"
#include "dac.h"
#include "adc.h"
#include "const.h"
#include "STM32TimerInterrupt_Generic.h"
#include <Arduino.h>

//Outputs
#define MCU_DIR PA_6
#define MCU_ECO PB_1
#define MCU_MC_ON PA_2
#define MCU_DIR PB7
#define MCU_ECO PB1

//Inputs
#define PRK_BRK_TELEM PB_4
#define MC_ON PA10
#define PRK_BRK_TELEM PB4

struct Digital_Data {
bool direction : 1; // output
bool eco_mode : 1; // output
bool mcu_mc_on : 1; // output
bool mc_on : 1; // input
bool park_brake : 1; // input
bool brakeLED : 1; // derived by state machine
};

extern volatile Digital_Data digital_data;
Expand All @@ -42,7 +44,6 @@ void readIO();
// Set the value of output pins
void set_direction(bool dir);
void set_eco_mode(bool eco);
void set_mcu_mc_on(bool mc_on);
void writeAccOut(float newAccOut);
void writeRegenBrake(float newRegenBrake);

Expand Down
3 changes: 1 addition & 2 deletions include/canPDC.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

//Macros for the CAN message IDs
#define FORWARD_AND_REVERSE_ID 0x300
#define FORWARD_AND_REVERSE_BIT_MASK 0x01

class CANPDC : public CANManager {
public:
Expand All @@ -15,8 +16,6 @@ class CANPDC : public CANManager {
void sendPDCData();
};

extern volatile bool brakeLED;
extern volatile bool forwardAndReverse;
extern volatile bool mc_on;

#endif
35 changes: 4 additions & 31 deletions include/const.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@
// How fast to transmit data over CAN in ms (and debug prints if on)
#define DATA_SEND_PERIOD 50

///////////////
// PID macros
///////////////
//Todo: tune these
#define POWER_P_PARAM 0.0
#define POWER_P_PARAM 0.0 // TODO: tune these
#define POWER_I_PARAM 0.0
#define POWER_D_PARAM 0.0
#define SPEED_P_PARAM 2.5
Expand All @@ -33,40 +30,16 @@
#define MAX_RPM 1000.0

// limits for outputs of PID
// is 0.0 to 1.0 due to how AnalogOut pins work.
// is 0.0 to 1.0 due to how analog output pins work.
#define MIN_OUT 0.0
#define MAX_OUT 1.0

//////////////////////
//State machine stuff
/////////////////////
enum class PDCStates : uint8_t {
OFF,
PARK,
IDLE,
FORWARD,
REVERSE,
CRUISE_POWER,
CRUISE_SPEED
};

enum class CRUZ_MODE : uint8_t {
OFF,
SPEED,
POWER,
};

extern volatile CRUZ_MODE cruzMode;
extern volatile PDCStates pdcState;

// State machine constants
#define FORWARD_VALUE 0
#define REVERSE_VALUE 1
#define BRAKE_SENSOR_THRESHOLD 0.14 // 0.7/5

///////////////
// Speed stuff
///////////////

// Speed constants
#define MIN_MOVING_SPEED 3.0 // speed threshold for idle state

#endif
23 changes: 19 additions & 4 deletions include/motor_control.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,30 @@
#include "STM32TimerInterrupt_Generic.h"
#include "canPDC.h"

enum class PDCStates : uint8_t {
OFF,
PARK,
IDLE,
FORWARD,
REVERSE,
CRUISE_POWER,
CRUISE_SPEED
};

enum class CRUZ_MODE : uint8_t {
OFF,
SPEED,
POWER,
};


void initPDCState();
void transition();
PDCStates get_state();

//Speed variables
extern volatile float rpm;
extern volatile float motorSpeedSetpoint;

// cruise control variables
PID *curr_PID;
extern volatile CRUZ_MODE cruzMode;
extern volatile PDCStates pdcState;

#endif
2 changes: 1 addition & 1 deletion include/speed_calc.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ void startSpeedCalculation();
// Starts speed (RPM and MPH) calculations at specified interval
extern volatile float rpm;
extern volatile float mph;
extern volatile unsigned int previousPulses[ARRAY_SIZE];
extern volatile uint8_t previousPulses[ARRAY_SIZE];

#endif
25 changes: 10 additions & 15 deletions src/IOManagement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ STM32TimerInterrupt IOTimer(TIM2);
void initIO() {
pinMode(MCU_DIR, OUTPUT);
pinMode(MCU_ECO, OUTPUT);
pinMode(MCU_MC_ON, OUTPUT);
pinMode(MC_ON, INPUT);
pinMode(PRK_BRK_TELEM, INPUT);

initADC(ADC1);

if(IOTimer.attachInterruptInterval(IO_UPDATE_PERIOD, readIO))
{
uint32_t channels[2] = {DAC_CHANNEL_1, DAC_CHANNEL_2};
initDAC(DAC1, channels, 2);

if(IOTimer.attachInterruptInterval(IO_UPDATE_PERIOD, readIO)) {
printf("starting IO timer\n");
}
else
{
} else {
printf("problem starting IO timer\n");
}
}

void readIO()
{
void readIO() {
digital_data.mc_on = digitalRead(MC_ON);
digital_data.park_brake = digitalRead(PRK_BRK_TELEM);

acc_in = readADC(ADC_CHANNEL_11); // PA_6
Expand All @@ -54,17 +54,12 @@ void set_eco_mode(bool eco){
digital_data.eco_mode = eco;
}

void set_mcu_mc_on(bool mc_on){
digital_data.mcu_mc_on = mc_on;
digitalWrite(MCU_MC_ON, mc_on);
}

void writeAccOut(float newAccOut) {
acc_out = newAccOut;
analogWrite(PA_5, acc_out);
writeDAC(DAC_CHANNEL_2, newAccOut); // PA_5
}

void writeRegenBrake(float newRegenBrake) {
regen_brake = newRegenBrake;
analogWrite(PA_4, regen_brake);
writeDAC(DAC_CHANNEL_1, newRegenBrake); // PA_4
}
7 changes: 3 additions & 4 deletions src/canPDC.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
#include "canPDC.h"

volatile bool brakeLED = false;
volatile bool forwardAndReverse = false;
volatile bool mc_on = false;

CANPDC::CANPDC(CAN_TypeDef* canPort, CAN_PINS pins, int frequency) : CANManager(canPort, pins, frequency) {};

void CANPDC::readHandler(CAN_message_t msg) {
switch(msg.id){
case FORWARD_AND_REVERSE_ID:
forwardAndReverse = msg.buf[0];
forwardAndReverse = msg.buf[0] & FORWARD_AND_REVERSE_BIT_MASK;
break;
default:
break;
Expand All @@ -25,6 +23,7 @@ void CANPDC::sendPDCData() {
this->sendMessage(0x205, (void*)&current_in_telem, sizeof(float));
this->sendMessage(0x206, (void*)&brake_pressure_telem, sizeof(float));
this->sendMessage(0x207, (void*)&digital_data, sizeof(digital_data));
this->sendMessage(0x20A, (void*)&brakeLED, sizeof(bool));
this->sendMessage(0x208, (void*)&mph, sizeof(float));
this->sendMessage(0x209, (void*)&acc_in, sizeof(float));

}
57 changes: 56 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,73 @@
#include <Arduino.h>
#include "IOManagement.h"
#include "dac.h"
#include "const.h"
#include "canPDC.h"
#include "speed_calc.h"
#include "motor_control.h"

CANPDC canBus(CAN1, DEF);

uint8_t counter = 0;
bool digital_output = false;

void setup() {
Serial.begin(115200);
initIO();
initPDCState();
startSpeedCalculation();
}

void loop() {
canBus.sendPDCData();
canBus.runQueue(DATA_SEND_PERIOD);
canBus.runQueue(50);
counter++;
if (counter >= 20) {
counter = 0;
// clear the screen
printf("\e[1;1H\e[2J");

// state
printf("State: ");
switch (pdcState) {
case PDCStates::OFF:
printf("OFF\n");
break;
case PDCStates::PARK:
printf("PARK\n");
break;
case PDCStates::IDLE:
printf("IDLE\n");
break;
case PDCStates::FORWARD:
printf("FORWARD\n");
break;
case PDCStates::REVERSE:
printf("REVERSE\n");
break;
case PDCStates::CRUISE_POWER:
printf("CRUISE_POWER\n");
break;
case PDCStates::CRUISE_SPEED:
printf("CRUISE_SPEED\n");
break;
default:
printf("unknown\n");
break;
}

// digital inputs
printf("mc_on: %d\n", digital_data.mc_on);
printf("park_brake: %d\n", digital_data.park_brake);
printf("Brake LED: %d\n", digital_data.brakeLED);

// analog inputs
printf("12V: %f\n", lv_12V_telem);
printf("5V: %f\n", lv_5V_telem);
printf("5V current: %f\n", lv_5V_current);
printf("current in: %f\n", current_in_telem);
printf("brake pressure: %f\n", brake_pressure_telem);
printf("mph: %f\n", mph);
printf("acc_in: %f\n", acc_in);
}
}
16 changes: 9 additions & 7 deletions src/motor_control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ volatile CRUZ_MODE cruzMode = CRUZ_MODE::OFF;
//PID interval is in seconds and macro is in microseconds
PID power_PID(POWER_D_PARAM, POWER_I_PARAM, POWER_P_PARAM, PID_UPDATE_INTERVAL);
PID speed_PID(SPEED_D_PARAM, SPEED_I_PARAM, SPEED_P_PARAM, PID_UPDATE_INTERVAL);
PID *curr_PID;

volatile float speed_pid_compute = 0.0;
STM32TimerInterrupt state_updater(TIM7);
Expand All @@ -18,7 +19,7 @@ void initPDCState(){
pdcState = PDCStates::OFF;

// initialize Ticker to run the transition method every pid update interval seconds
state_updater.attachInterruptInterval(PID_UPDATE_INTERVAL, transition);
state_updater.attachInterruptInterval(IO_UPDATE_PERIOD, transition);
// set limits of inputs and outputs
power_PID.setInputLimits(MIN_POWER, MAX_POWER);
power_PID.setOutputLimits(MIN_OUT, MAX_OUT);
Expand All @@ -43,11 +44,12 @@ void transition() {
// OUTPUT: set acc_out pin based on acc_in from the pedal
// this allows us to accelerate into REVERSE or FORWARD states
writeAccOut(acc_in);
digital_data.direction = forwardAndReverse;
set_direction(digital_data.direction);

// car moving fast, transition to locked direction state
if (rpm >= MIN_MOVING_SPEED) {
if (forwardAndReverse == FORWARD_VALUE) {
if (digital_data.direction == FORWARD_VALUE) {
pdcState = PDCStates::FORWARD;
} else {
pdcState = PDCStates::REVERSE;
Expand Down Expand Up @@ -109,12 +111,12 @@ void transition() {

// OFF state as our default
default:
if (mc_on) {
// OUTPUT: make sure the motor isn't spinning when it's OFF
writeAccOut(0.0);
if (digital_data.mc_on) {
pdcState = PDCStates::PARK;
break;
}
// OUTPUT: make sure the motor isn't spinning when it's OFF
writeAccOut(0.0);
// Set to known state since it is our default
pdcState = PDCStates::OFF;
break;
Expand All @@ -124,7 +126,7 @@ void transition() {

// use if, else if because we should only switch to 1 state.
// put higher priority checks up top.
if (!mc_on) {
if (!digital_data.mc_on) {
// the motor is off, so go into OFF state
pdcState = PDCStates::OFF;
writeAccOut(0.0);
Expand All @@ -138,5 +140,5 @@ void transition() {
writeAccOut(0.0);
}
// set brakeLED based on analog brake sensor
brakeLED = brake_pressure_telem > BRAKE_SENSOR_THRESHOLD;
digital_data.brakeLED = brake_pressure_telem > BRAKE_SENSOR_THRESHOLD;
}
Loading