-
Notifications
You must be signed in to change notification settings - Fork 3
dinu_intro_project #1
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -112,6 +112,20 @@ you end up with lots of useless formatting changes. You should install | |
| clang-format as soon as possible and set it up :) | ||
| */ | ||
|
|
||
| typedef struct { // These 4 variables are shared by two threads - Dinu | ||
| float current; | ||
| float wheelspeeds; | ||
| float steering_angle; | ||
| float torque; | ||
| } movement_vehicle_data; | ||
|
|
||
| typedef int mutex_t mutex_create(void); | ||
|
|
||
| extern bool mutex_take(mutex_t mutex); | ||
| extern bool mutex_give(mutex_t mutex); | ||
|
|
||
| for(int j = 0; j < movement_vehicle_data.wheelspeeds.length(); j++) {} | ||
|
|
||
| /* | ||
| Initialize the CAN peripheral with given RX and TX pins at a given baudrate. | ||
| */ | ||
|
|
@@ -161,6 +175,118 @@ extern float bms_get_voltage(uint8_t n); | |
| */ | ||
| extern float bms_get_temperature(uint8_t n); | ||
|
|
||
| void setup(void) {} | ||
|
|
||
| void loop(void) {} | ||
| #define CAN_BAUDRATE 500000 | ||
| #define MOTOR_TEETH 17 | ||
| #define SPEED_TIME 10 | ||
| #define MOTOR_CONTROL_TIME 1 | ||
| #define DISPLAY_DELAY 100 | ||
| #define SAFETY_DELAY 20 | ||
|
|
||
| static movement_vehicle_data vehicle_data; | ||
| static mutex_t vehicle_mutex; | ||
|
|
||
| void calculate_rpm(void) { | ||
| int rotation = 0; | ||
|
|
||
| for(;;) { | ||
| delay(SPEED_TIME); | ||
|
|
||
| if(wheel_pin == low) { | ||
| rotation++; | ||
| } | ||
|
|
||
| vehicle_data.wheelspeeds = rotation / (SPEED_TIME * 6000); | ||
| } | ||
| } | ||
|
|
||
| void motor_control_thread(void) { | ||
| for(;;) { // Infinite For Loop | ||
| delay(MOTOR_CONTROL_TIME); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| while(!mutex_take(vehicle_mutex)) { | ||
| } // Return true or false, to know whether mutex lock was acquired or not | ||
|
|
||
| calculate_torque_cmd( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function signature looks like this: The biggest issue rn (aside from the order ur passing them in), is that torques and wheelspeeds should be arrays, but in your struct The idea behind |
||
| vehicle_data.torque, vehicle_data.current, vehicle_data.steering_angle, | ||
| vehicle_data.wheelspeeds | ||
| ); | ||
|
|
||
| mutex_give(vehicle_mutex); | ||
|
|
||
| while(!mutex_take(vehicle_mutex)) {} | ||
| can_send(front_motorL_ID, torque[0]); | ||
| can_send(frontR_motor_ID, torque[1]); | ||
| can_send(rearL_motor_ID, torque[2]); | ||
| can_send(rearR_motor_ID, torue[3]); | ||
|
|
||
| mutex_give(vehicle_mutex); | ||
| } | ||
| } | ||
|
|
||
| void display_thread(void) { | ||
| for(;;) { | ||
| delay(DISPLAY_DELAY); | ||
|
|
||
| while(!mutex_take(vehicle_mutex)) {} | ||
|
|
||
| lcd_printf( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. very small thing but |
||
| vehicle_data.current, vehicle_data.wheelspeeds[0], | ||
| vehicle_data.wheelspeeds[1], vehicle_data.wheelspeeds[2], | ||
| vehicle_data.wheelspeeds[3], vehicle_data.torque[0], | ||
| vehicle_data.torque[1], vehicle_data.torque[2], vehicle_data.torque[3] | ||
| ); | ||
|
|
||
| mutex_give(vehicle_mutex); | ||
| } | ||
| } | ||
|
|
||
| void safety_thread(void) { | ||
| for(;;) { | ||
| delay(SAFETY_DELAY); | ||
|
|
||
| double v_min = 5.0; | ||
| double v_max = 0.0; | ||
| double t_max = 0.0; | ||
| double v = 0; | ||
| double t = 0; | ||
|
|
||
| for(int i = 0; i < num_cells; i++) { | ||
| v = bms_get_voltage(i); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The bms shares a SPI bus with the LCD, so this isn't safe |
||
| t = bms_get_temperature(i); | ||
|
|
||
| if(v < v_min) { | ||
| v = v_min; | ||
| } | ||
| if(v > v_max) { | ||
| v = v_max; | ||
| } | ||
| if(t > t_max) { | ||
| t = t_max; | ||
| } | ||
| } | ||
|
|
||
| if(v < 2.8 || v > 4.3 || t > 60.0) { | ||
| mcu_shutdown(); // Shutdown the car | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void setup(void) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to actually spawn threads and start the scheduler in order for it to run. Right now your code will run whats in |
||
| // Configure pins | ||
| pinMode(TS_ON, INPUT); | ||
| pinMode(RTD, INPUT); | ||
|
|
||
| pinMode(AIR_PLUS, OUTPUT); | ||
| pinMode(AIR_MINUS, OUTPUT); | ||
| pinMode(PRECHARGE, OUTPUT); | ||
|
|
||
| // Initialize peripherals | ||
| can_init(CAN_RX, CAN_TX, CAN_BAUDRATE); | ||
| lcd_init(MOSI, MISO, SCK, LCD_CS); | ||
| bms_init(MOSI, MISO, SCK, LCS_CS); | ||
|
|
||
| data_mutex = mutex_t(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you meant to call |
||
| spi_mutex = mutex_t(); | ||
| } | ||
|
|
||
| void loop(void) {} | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is fine, but it might not work at high speeds, and there's a better way to do it. Think about how you could do this with interrupts