Vehicle Control Unit 0.01
This is the c library for controlling the car.
Loading...
Searching...
No Matches
TorqueControl.c
Go to the documentation of this file.
1#include "../../../Inc/Systems/Controller/TorqueControl.h"
2#include "../../../Inc/Utils/Common.h"
3
4void initTorqueControl(TorqueControl* tc, int hz, float maxTorque) {
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}
13
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}
26
27void setDesiredTorque(TorqueControl* tc, float torque) {
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}
38
39void setActualTorque(TorqueControl* tc, float torque) {
40 tc->actualTorque = torque;
41}
42
43void updateTorqueControl(void* tc) {
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}
57
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}
75
76int torqueSafetyCheck(void* tc) {
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}
#define FAILURE
Definition: Common.h:6
#define SUCCESS
Definition: Common.h:5
@ c_TORQUE
void initControllerSystem(ControllerSystem *controller, const char *name, int hz, ControllerType type)
Initializes the Controller System with initial settings.
TorqueStatus checkTorqueLimits(TorqueControl *tc)
Checks if the actual torque is within the defined limits and desired range.
Definition: TorqueControl.c:58
void setDesiredTorque(TorqueControl *tc, float torque)
Sets the desired torque for the Torque Control Actuator.
Definition: TorqueControl.c:27
int startTorqueControl(TorqueControl *tc)
Definition: TorqueControl.c:14
void setActualTorque(TorqueControl *tc, float torque)
Definition: TorqueControl.c:39
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
void initTorqueControl(TorqueControl *tc, int hz, float maxTorque)
Initializes the Torque Control Actuator with initial settings.
Definition: TorqueControl.c:4
TorqueStatus
Definition: TorqueControl.h:9
@ 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
#define ENABLE(item_)
Definition: Updateable.h:10
int(* safety)(void *self)
Updateable updateable
Definition: System.h:18
ControllerSystem base
Definition: TorqueControl.h:17
float maxAllowedTorque
Definition: TorqueControl.h:20
float actualTorque
Definition: TorqueControl.h:18
TorqueStatus status
Definition: TorqueControl.h:21
float desiredTorque
Definition: TorqueControl.h:19
void(* update)(void *self)
Definition: Updateable.h:26