Vehicle Control Unit 0.01
This is the c library for controlling the car.
Loading...
Searching...
No Matches
Functions
TorqueControl.c File Reference
#include "../../../Inc/Systems/Controller/TorqueControl.h"
#include "../../../Inc/Utils/Common.h"
Include dependency graph for TorqueControl.c:

Go to the source code of this file.

Functions

void initTorqueControl (TorqueControl *tc, int hz, float maxTorque)
 Initializes the Torque Control Actuator with initial settings. More...
 
int startTorqueControl (TorqueControl *tc)
 
void setDesiredTorque (TorqueControl *tc, float torque)
 Sets the desired torque for the Torque Control Actuator. More...
 
void setActualTorque (TorqueControl *tc, float torque)
 
void updateTorqueControl (void *tc)
 Updates the Torque Control Actuator based on sensor inputs and calculations. More...
 
TorqueStatus checkTorqueLimits (TorqueControl *tc)
 Checks if the actual torque is within the defined limits and desired range. More...
 
int torqueSafetyCheck (void *tc)
 Checks the safety of the torque with multiple monitors. More...
 

Function Documentation

◆ checkTorqueLimits()

TorqueStatus checkTorqueLimits ( TorqueControl tc)

Checks if the actual torque is within the defined limits and desired range.

TODO: move into torque police

Note
FIXME: When should each torque condition be in place?
Parameters
tcA pointer to the TorqueControl structure.
Returns
The actual status of the torque.

Definition at line 58 of file TorqueControl.c.

58 {
59 if (tc->actualTorque > tc->maxAllowedTorque) {
60 return TORQUE_OVER_LIMIT;
61 }
62 else if (tc->actualTorque < 0) {
63 return TORQUE_UNDER_LIMIT;
64 }
65
66 float lowerBound = tc->desiredTorque * (1 - TORQUE_ERROR_MARGIN);
67 float upperBound = tc->desiredTorque * (1 + TORQUE_ERROR_MARGIN);
68
69 if (tc->actualTorque < lowerBound || tc->actualTorque > upperBound) {
71 }
72
73 return TORQUE_OK;
74}
@ TORQUE_OK
Definition: TorqueControl.h:10
@ TORQUE_UNDER_LIMIT
Definition: TorqueControl.h:12
@ TORQUE_SENSOR_ERROR
Definition: TorqueControl.h:13
@ TORQUE_OVER_LIMIT
Definition: TorqueControl.h:11
#define TORQUE_ERROR_MARGIN
Definition: TorqueControl.h:6
float maxAllowedTorque
Definition: TorqueControl.h:20
float actualTorque
Definition: TorqueControl.h:18
float desiredTorque
Definition: TorqueControl.h:19
Here is the caller graph for this function:

◆ initTorqueControl()

void initTorqueControl ( TorqueControl tc,
int  hz,
float  maxTorque 
)

Initializes the Torque Control Actuator with initial settings.

Parameters
tcA pointer to the TorqueControl structure.
hzRate at which the sensor is called (in hz).
maxTorqueThe maximum torque limit set for the system (in Nm).

Definition at line 4 of file TorqueControl.c.

4 {
5 initControllerSystem(&tc->base, "Torque Control", hz, c_TORQUE);
7 tc->actualTorque = 0;
8 tc->desiredTorque = 0;
9 tc->maxAllowedTorque = maxTorque;
10 tc->status = TORQUE_OK;
12}
@ c_TORQUE
void initControllerSystem(ControllerSystem *controller, const char *name, int hz, ControllerType type)
Initializes the Controller System with initial settings.
void updateTorqueControl(void *tc)
Updates the Torque Control Actuator based on sensor inputs and calculations.
Definition: TorqueControl.c:43
int torqueSafetyCheck(void *tc)
Checks the safety of the torque with multiple monitors.
Definition: TorqueControl.c:76
int(* safety)(void *self)
Updateable updateable
Definition: System.h:18
ControllerSystem base
Definition: TorqueControl.h:17
TorqueStatus status
Definition: TorqueControl.h:21
void(* update)(void *self)
Definition: Updateable.h:26
Here is the call graph for this function:
Here is the caller graph for this function:

◆ setActualTorque()

void setActualTorque ( TorqueControl tc,
float  torque 
)

Definition at line 39 of file TorqueControl.c.

39 {
40 tc->actualTorque = torque;
41}

◆ setDesiredTorque()

void setDesiredTorque ( TorqueControl tc,
float  torque 
)

Sets the desired torque for the Torque Control Actuator.

Note
FIXME: Update to this to a map in the future.
Parameters
tcA pointer to the TorqueControl structure.
torqueThe desired torque to be set (in Nm).

Definition at line 27 of file TorqueControl.c.

27 {
28 if (tc->base.safety(tc) == FAILURE) {
29 printf("Torque Control Actuator is not in a safe state\n");
30 return;
31 }
32 if (torque > tc->maxAllowedTorque) {
33 printf("Desired torque exceeds the maximum allowed torque\n");
34 return;
35 }
36 tc->desiredTorque = torque;
37}
#define FAILURE
Definition: Common.h:6

◆ startTorqueControl()

int startTorqueControl ( TorqueControl tc)

Definition at line 14 of file TorqueControl.c.

14 {
15 if (tc->base.safety == NULL) {
16 printf("Safety system not set for Torque Control\n");
17 return FAILURE;
18 }
19 else if (tc->base.safety(tc) == FAILURE) {
20 printf("Torque Control Actuator is not in a safe state\n");
21 return FAILURE;
22 }
23 ENABLE(tc->base.system);
24 return SUCCESS;
25}
#define SUCCESS
Definition: Common.h:5
#define ENABLE(item_)
Definition: Updateable.h:10

◆ torqueSafetyCheck()

int torqueSafetyCheck ( void *  tc)

Checks the safety of the torque with multiple monitors.

Parameters
tcA pointer to the TorqueControl structure.

Definition at line 76 of file TorqueControl.c.

76 {
77 TorqueControl* tcPtr = (TorqueControl*)tc;
78 if(tcPtr->base.num_monitors == 0) {
79 printf("No monitors set for Torque Control\n");
80 return FAILURE;
81 }
82 else if (tcPtr->status != TORQUE_OK) {
83 printf("Torque Control Actuator is not in a safe state\n");
84 return FAILURE;
85 }
86 return SUCCESS;
87}
Here is the caller graph for this function:

◆ updateTorqueControl()

void updateTorqueControl ( void *  tc)

Updates the Torque Control Actuator based on sensor inputs and calculations.

Parameters
tcA pointer to the TorqueControl structure.

Definition at line 43 of file TorqueControl.c.

43 {
44 TorqueControl* tcPtr = (TorqueControl*)tc;
45 if (tcPtr->base.safety(tc) == FAILURE) {
46 printf("Torque Control Actuator is not in a safe state\n");
47 return;
48 }
49 tcPtr->status = checkTorqueLimits(tc);
50 if (tcPtr->status != TORQUE_OK) {
51 printf("Torque Control Actuator is not in OK\n");
52 return;
53 }
54 // TODO: Implement some way to send the torque where it needs to go
55 // sendTorqueCommand(tc);
56}
TorqueStatus checkTorqueLimits(TorqueControl *tc)
Checks if the actual torque is within the defined limits and desired range.
Definition: TorqueControl.c:58
Here is the call graph for this function:
Here is the caller graph for this function: