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/Systems/ControllerSystem.h"
#include "../../../Inc/Utils/Common.h"
#include <math.h>
Include dependency graph for TorqueControl.c:

Go to the source code of this file.

Functions

void initTorqueControl (TorqueControl *tc, Apps *apps, int hz, float maxTorque)
 Initializes the Torque Control Actuator with initial settings. More...
 
int startTorqueControl (TorqueControl *tc)
 Starts the Torque Control Actuator. More...
 
int setDesiredTorque (ControllerSystem *controller)
 Sets the desired torque for the Torque Control Actuator. More...
 

Function Documentation

◆ initTorqueControl()

void initTorqueControl ( TorqueControl tc,
Apps apps,
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 6 of file TorqueControl.c.

6 {
7 initControllerSystem(&tc->base, "Torque Control", hz, c_TORQUE, setDesiredTorque, tc);
8 tc->desiredTorque = 0;
9 tc->maxAllowedTorque = maxTorque;
10 tc->apps = apps;
11}
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
int setDesiredTorque(ControllerSystem *controller)
Sets the desired torque for the Torque Control Actuator.
Definition: TorqueControl.c:27
ControllerSystem base
Definition: TorqueControl.h:19
float maxAllowedTorque
Definition: TorqueControl.h:22
float desiredTorque
Definition: TorqueControl.h:21
Here is the call graph for this function:
Here is the caller graph for this function:

◆ setDesiredTorque()

int setDesiredTorque ( ControllerSystem controller)

Sets the desired torque for the Torque Control Actuator.

Parameters
controllerA pointer to the ControllerSystem structure.
Returns
_SUCCESS if the desired torque was set, _FAILURE otherwise.

Definition at line 27 of file TorqueControl.c.

27 {
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 _SUCCESS
Definition: Common.h:5
@ c_computed
ControllerState state
Here is the call graph for this function:
Here is the caller graph for this function:

◆ startTorqueControl()

int startTorqueControl ( TorqueControl tc)

Starts the Torque Control Actuator.

Parameters
tcA pointer to the TorqueControl structure.
Returns
_SUCCESS if the actuator was started, _FAILURE otherwise.

Definition at line 13 of file TorqueControl.c.

13 {
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}
#define _FAILURE
Definition: Common.h:6
@ c_idle
#define ENABLE(item_)
Definition: Updateable.h:10
int(* safety)(struct ControllerSystem *controller)
Here is the caller graph for this function: