Skip to content
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

Implement the Simple Vehicle Dynamics Interface in C++ #330

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
1c6fba0
Calculate TC scale factor function
Tegh25 Dec 7, 2024
e9531d5
Re-implement tc_scale_factor with a smooth ramp
BlakeFreer Jan 6, 2025
7a9b38f
VD Interface project for building and testing
Tegh25 Jan 4, 2025
b5e83cc
fix: Clean up VD Project
BlakeFreer Jan 6, 2025
14a3284
Fix: Scale factor initializes to full torque at start up
Tegh25 Jan 8, 2025
5b6b7f5
Minor formatting and locality
Tegh25 Jan 13, 2025
cfc3a6b
Fix: Test cases use assert close
Tegh25 Jan 13, 2025
a42138e
Pedal torque lut specified as constructor parameter
Tegh25 Jan 13, 2025
7030816
Use structs for left right values of torque vector and motor torque
Tegh25 Jan 13, 2025
856e736
Calculate tv only when torque vectoring enabled
Tegh25 Jan 13, 2025
f581b39
Fix: Corrected test cases and formatting
Tegh25 Jan 13, 2025
49058e7
Updated motor torque and torque vector struct field names
Tegh25 Jan 14, 2025
c3140f9
Moved running average calculation to simp vd class
Tegh25 Jan 14, 2025
b0a1777
Update tvFactor test cases
Tegh25 Jan 14, 2025
3e5ba46
Minor comment change for specificity
Tegh25 Jan 14, 2025
f83f83e
Fix: Formatting
Tegh25 Jan 14, 2025
1017b3a
Merge branch 'user/atelieyt/vd-interface' of github.com:macformula/ra…
Tegh25 Jan 14, 2025
12890a6
Fixed clang formatting
Tegh25 Jan 14, 2025
ceacee9
Refactor calc actual slip and torque running avg
Tegh25 Jan 17, 2025
0436bb2
Refactor to running avg value to smoothed torque request
Tegh25 Jan 17, 2025
48d8911
Refactor torque vector value assignment
Tegh25 Jan 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions firmware/projects/VD_Interface/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Teghveer Singh Ateliey
# January 4, 2025

# The target executable 'main' is created in the master CMakeLists. Do not change its name.
# We only need to add the source code files and include directories.

# include("${CMAKE_SOURCE_DIR}/cmake/cangen.cmake")

target_sources(main PRIVATE main.cc)

# Notice that we don't include any mcal/ subdirectory in this CMake file.
# The master CMakeLists handles platform selection and library linking.
49 changes: 49 additions & 0 deletions firmware/projects/VD_Interface/inc/simp_vd_interface.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/// @author Teghveer Singh Ateliey
/// @date 2024-11-23

#include "simp_vd_interface.hpp"

using namespace ctrl;

SimpVdInterface::SimpVdInterface(
const shared::util::Mapper<float>& pedal_to_torque, float target_slip)
: pedal_to_torque(pedal_to_torque), target_slip(target_slip) {}

VdOutput SimpVdInterface::update(const VdInput& input, int time_ms) {
VdOutput output{
.lm_torque_limit_positive = 0.0f,
.rm_torque_limit_positive = 0.0f,
.lm_torque_limit_negative = 0.0f, // negative limit fields fixed at
.rm_torque_limit_negative = 0.0f, // 0 in simulink model
.left_motor_speed_request = 1000,
.right_motor_speed_request = 1000,
};

float actual_slip =
CalculateActualSlip(input.wheel_speed_lr, input.wheel_speed_rr,
input.wheel_speed_lf, input.wheel_speed_rf);
float tc_scale_factor =
CalculateTCScaleFactor(actual_slip, target_slip, time_ms);

TorqueVector<float> torque_vector;
if (input.tv_enable) {
torque_vector = AdjustTorqueVectoring(input.steering_angle);
} else {
torque_vector.left = 1.0f;
torque_vector.right = 1.0f;
Tegh25 marked this conversation as resolved.
Show resolved Hide resolved
}

float motor_torque_request = ComputeTorqueRequest(
input.driver_torque_request, input.brake_pedal_postion);

running_average.LoadValue(
pedal_to_torque.Evaluate(motor_torque_request * tc_scale_factor));
float running_average_value = running_average.GetValue();
Tegh25 marked this conversation as resolved.
Show resolved Hide resolved

output.lm_torque_limit_positive =
running_average_value * torque_vector.left;
output.rm_torque_limit_positive =
running_average_value * torque_vector.right;

return output;
}
45 changes: 45 additions & 0 deletions firmware/projects/VD_Interface/inc/simp_vd_interface.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/// @author Teghveer Singh Ateliey
/// @date 2024-11-23

#pragma once

#include <cstdint>

#include "shared/controls/motor_torque.h"
#include "shared/controls/tc_scale_factor.h"
#include "shared/controls/tvFactor.h"
#include "shared/util/mappers/lookup_table.hpp"
#include "shared/util/mappers/mapper.hpp"

struct VdInput {
float driver_torque_request;
float brake_pedal_postion;
float steering_angle;
float wheel_speed_lr;
float wheel_speed_rr;
float wheel_speed_lf;
float wheel_speed_rf;
bool tv_enable;
};

struct VdOutput {
float lm_torque_limit_positive;
float rm_torque_limit_positive;
float lm_torque_limit_negative;
float rm_torque_limit_negative;
uint16_t left_motor_speed_request;
uint16_t right_motor_speed_request;
};

class SimpVdInterface {
public:
SimpVdInterface(
const shared::util::Mapper<float>& pedal_to_torque,
float target_slip = 0.2f); // default target slip is float 0.2
VdOutput update(const VdInput& input, int time_ms);

private:
const shared::util::Mapper<float>& pedal_to_torque;
shared::util::MovingAverage<float, 10> running_average;
Copy link
Contributor

Choose a reason for hiding this comment

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

can you give this a more descriptive name?

float target_slip;
};
Loading
Loading