Skip to content

Commit

Permalink
[SOFT-452] Regen braking toggling: MCI (#416)
Browse files Browse the repository at this point in the history
Implemented the regen braking on MCI side by
 - Adding a regen braking state in regen_braking.c
 - Making test cases in test_regen_braking.c to ensure the functionality of regen_braking.c
 - Adding the internal regen_brake state to mci_output.c to set the current to zero when regen is diabled and the target velocity is less than the current velocity.
 - Added additional test cases to 'test_mci_output.c` to test all driving and pedal modes both when regen barking is enabled and disabled.
  • Loading branch information
farazkh80 authored Jul 29, 2021
1 parent 985d582 commit c49fcda
Show file tree
Hide file tree
Showing 8 changed files with 413 additions and 13 deletions.
2 changes: 2 additions & 0 deletions projects/mci/inc/mci_output.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ typedef struct {
PedalRxStorage pedal_storage;
} MotorControllerOutputStorage;

void mci_output_update_velocity(float actual_velocity_ms);

StatusCode mci_output_init(MotorControllerOutputStorage *storage, Mcp2515Storage *motor_can);
19 changes: 19 additions & 0 deletions projects/mci/inc/regen_braking.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

// A module to set the regen braking state in MCI
// upon receiving a CAN_TRANSMIT_REGEN_BRAKING can
// message. The regen braking state is used in
// mci_output.c which determines whether we are
// regen braking.
// Requires CAN to be initialized.

#include <stdbool.h>

#include "can.h"
#include "can_msg_defs.h"
#include "can_unpack.h"
#include "status.h"

StatusCode regen_braking_init(void);

bool get_regen_braking_state(void);
2 changes: 2 additions & 0 deletions projects/mci/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "motor_can.h"
#include "motor_controller.h"
#include "precharge_control.h"
#include "regen_braking.h"

#include "log.h"

Expand Down Expand Up @@ -83,6 +84,7 @@ int main(void) {
prv_fan_control_init();

drive_fsm_init();
regen_braking_init();
Event e = { 0 };
while (true) {
while (event_process(&e) != STATUS_CODE_OK) {
Expand Down
1 change: 1 addition & 0 deletions projects/mci/src/mci_broadcast.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ static void prv_handle_speed_rx(const GenericCanMsg *msg, void *context) {
critical_section_end(disabled);
if (motor_id == LEFT_MOTOR_CONTROLLER) {
cruise_rx_update_velocity(can_data.velocity_measurement.vehicle_velocity_ms);
mci_output_update_velocity(can_data.velocity_measurement.vehicle_velocity_ms);
}
break;
}
Expand Down
17 changes: 17 additions & 0 deletions projects/mci/src/mci_output.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#include "mci_output.h"

#include <math.h>
#include <string.h>

#include "cruise_rx.h"
#include "drive_fsm.h"
#include "mci_events.h"
#include "motor_can.h"
#include "regen_braking.h"
#include "wavesculptor.h"

#include "exported_enums.h"
Expand All @@ -23,6 +25,12 @@ static float s_velocity_lookup[] = {

static float s_max_regen_in_throttle = 0.0f;
static float s_regen_threshold = 0.0f;
static float s_actual_velocity_ms = 0.0f;

// Updates s_actual_velocity_ms
void mci_output_update_velocity(float actual_velocity_ms) {
s_actual_velocity_ms = actual_velocity_ms;
}

// Basic implementation of throttle/brake maps
static float prv_brake_to_regen_map(float brake_value) {
Expand Down Expand Up @@ -59,6 +67,8 @@ static void prv_handle_drive(SoftTimerId timer_id, void *context) {
MotorCanDriveCommand drive_command = { 0 };
EEDriveOutput drive_state = drive_fsm_get_drive_state();
bool is_cruise = drive_fsm_is_cruise();
bool is_regen_brake = get_regen_braking_state();

// TODO(SOFT-122): Make sure test ensures that maps are continues
if (drive_state == EE_DRIVE_OUTPUT_OFF) {
drive_command.motor_current = 0.0f;
Expand All @@ -78,6 +88,13 @@ static void prv_handle_drive(SoftTimerId timer_id, void *context) {
drive_command.motor_velocity =
is_cruise ? cruise_rx_get_target_velocity() : s_velocity_lookup[drive_state];
}

// Set current to zero if regen braking is disabled
// target velocity is less than actual velocity
if (!is_regen_brake && fabs(drive_command.motor_velocity) < fabs(s_actual_velocity_ms)) {
drive_command.motor_current = 0.0f;
}

// Handling message
prv_send_wavesculptor_message(storage, MOTOR_CAN_LEFT_DRIVE_COMMAND_FRAME_ID, drive_command);
prv_send_wavesculptor_message(storage, MOTOR_CAN_RIGHT_DRIVE_COMMAND_FRAME_ID, drive_command);
Expand Down
26 changes: 26 additions & 0 deletions projects/mci/src/regen_braking.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "regen_braking.h"

#include "can.h"
#include "can_msg_defs.h"
#include "can_unpack.h"
#include "status.h"

static bool s_regen_braking_state;

// Callback function to set the regen braking state
static StatusCode prv_regen_braking_callback(const CanMessage *msg, void *context,
CanAckStatus *ack_reply) {
CAN_UNPACK_REGEN_BRAKING(msg, (uint8_t *)&s_regen_braking_state);
return STATUS_CODE_OK;
}

StatusCode regen_braking_init(void) {
// Default to enabled
s_regen_braking_state = true;
can_register_rx_handler(SYSTEM_CAN_MESSAGE_REGEN_BRAKING, prv_regen_braking_callback, NULL);
return STATUS_CODE_OK;
}

bool get_regen_braking_state(void) {
return s_regen_braking_state;
}
Loading

0 comments on commit c49fcda

Please sign in to comment.