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/Systems/ControllerSystem.h"
3#include "../../../Inc/Utils/Common.h"
4#include <math.h>
5
6void initTorqueControl(TorqueControl* tc, Apps* apps, int hz, float maxTorque) {
7 initControllerSystem(&tc->base, "Torque Control", hz, c_TORQUE, setDesiredTorque, tc);
8 tc->desiredTorque = 0;
9 tc->maxAllowedTorque = maxTorque;
10 tc->apps = apps;
11}
12
14 if (tc->base.safety == NULL) {
15 printf("Safety system not set for Torque Control\n");
16 return _FAILURE;
17 }
18 else if (tc->base.safety(&tc->base) == _FAILURE) {
19 printf("Torque Control Actuator is not in a safe state\n");
20 return _FAILURE;
21 }
22 ENABLE(tc->base.system);
23 tc->base.state = c_idle;
24 return _SUCCESS;
25}
26
28 TorqueControl* tc = (TorqueControl*)controller->child;
29 float pedalposition = getAppsPosition(tc->apps);
30 float torque = pedalposition * tc->maxAllowedTorque;
31
32 if (torque > tc->maxAllowedTorque) {
33 printf("Desired torque exceeds the maximum allowed torque, stepping down to max\n");
34 torque = tc->maxAllowedTorque;
35 }
36
37 // Perform any desired mapping, i.e. fit to sigmoid function.
38 double normalized = torque / tc->maxAllowedTorque; // Normalize to range [0, 1]
39 double s_curve = 1.0 / (1.0 + exp(-10 * (normalized - 0.5)));
40 tc->desiredTorque = s_curve * tc->maxAllowedTorque;
41 tc->base.state = c_computed;
42
43 #ifdef DEBUGn
44 printf("Desired Torque: %f\r\n", tc->desiredTorque);
45 #endif
46
47 return _SUCCESS;
48}
float getAppsPosition(Apps *apps)
Averages app sensors to get pedal position.
Definition: Apps.c:42
#define _FAILURE
Definition: Common.h:6
#define _SUCCESS
Definition: Common.h:5
void initControllerSystem(ControllerSystem *controller, const char *name, int hz, ControllerType type, int(*updateController)(ControllerSystem *controller), void *child)
Initializes the Controller System with initial settings.
@ c_TORQUE
@ c_idle
@ c_computed
int setDesiredTorque(ControllerSystem *controller)
Sets the desired torque for the Torque Control Actuator.
Definition: TorqueControl.c:27
int startTorqueControl(TorqueControl *tc)
Starts the Torque Control Actuator.
Definition: TorqueControl.c:13
void initTorqueControl(TorqueControl *tc, Apps *apps, int hz, float maxTorque)
Initializes the Torque Control Actuator with initial settings.
Definition: TorqueControl.c:6
#define ENABLE(item_)
Definition: Updateable.h:10
Definition: Apps.h:22
int(* safety)(struct ControllerSystem *controller)
ControllerState state
ControllerSystem base
Definition: TorqueControl.h:19
float maxAllowedTorque
Definition: TorqueControl.h:22
float desiredTorque
Definition: TorqueControl.h:21