Skip to content

dinu_intro_project#1

Open
dinud2 wants to merge 1 commit intoUTFR:mainfrom
dinud2:dinu_intro_project
Open

dinu_intro_project#1
dinud2 wants to merge 1 commit intoUTFR:mainfrom
dinud2:dinu_intro_project

Conversation

@dinud2
Copy link

@dinud2 dinud2 commented Jan 19, 2026

No description provided.

Copy link
Contributor

@IbrahimFadel IbrahimFadel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall it looks good :) There are two big things I would change

  1. Try to be more granular with mutexes. Rn u have one mutex for everything, which will work but it kind of defeats the purpose of concurrency if only one thread can ever do work at a time. Mutexes can be created for every shared resource (individually). I think you were planning to do something more granular because you made two mutexes in setup, but never used them?
  2. Use the FreeRTOS API - so instead of declaring the magic mutex_create etc. Use the actual FreeRTOS functions and datastructures for mutexes and tasks etc.

I also left comments in the code


void motor_control_thread(void) {
for(;;) { // Infinite For Loop
delay(MOTOR_CONTROL_TIME);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delay will pause every thread, not just the one you call it in. Look for how to do delays in FreeRTOS

while(!mutex_take(vehicle_mutex)) {
} // Return true or false, to know whether mutex lock was acquired or not

calculate_torque_cmd(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function signature looks like this:

extern void calculate_torque_cmd(
  float *torques, float current, float *wheelspeeds, float steering_angle
);

The biggest issue rn (aside from the order ur passing them in), is that torques and wheelspeeds should be arrays, but in your struct movement_vehicle_data they are single values.

The idea behind calculate_torque_cmd is that the output is an array of floats for torque. So I must initialize an array of floats somewhere, then give calculate_torque_cmd a pointer to that array so it can populate it with values. On the other hand, wheelspeeds is an input, and should be an array containing 4 wheelspeeds (one for each wheel).


while(!mutex_take(vehicle_mutex)) {}

lcd_printf(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very small thing but lcd_printf should take a format string as the first parameter, something that looks like "Current: %f, FL Wheelspeed: %f, FR Wheelspeed: %f" etc.

lcd_init(MOSI, MISO, SCK, LCD_CS);
bms_init(MOSI, MISO, SCK, LCS_CS);

data_mutex = mutex_t();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you meant to call mutex_create

}
}

void setup(void) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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 setup, then since the scheduler never starts, it'll go into loop where it does nothing forever

double t = 0;

for(int i = 0; i < num_cells; i++) {
v = bms_get_voltage(i);
Copy link
Contributor

Choose a reason for hiding this comment

The 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

static movement_vehicle_data vehicle_data;
static mutex_t vehicle_mutex;

void calculate_rpm(void) {
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants