1+ #include "u_tc.h"
2+ #include "u_dti.h"
3+ #include <stdint.h>
4+
5+ #define MIN (a , b ) ((a) < (b) ? (a) : (b))
6+ #define MAX (a , b ) ((a) > (b) ? (a) : (b))
7+
8+ #define MAX_SLIP_ERROR .06f
9+ #define INCHES_TO_MILES 63360.0f
10+ #define SECONDS_TO_HOURS 1.0f / 3600.0f
11+
12+ static float _calc_slip (float omega_rl , float omega_rr , float omega_fl ,
13+ float omega_fr );
14+ static float _calc_vehicle_speed (float omega_fl , float omega_fr );
15+ static float _calc_rear_wheel_speed (float omega_rl , float omega_rr );
16+ static float _rads_to_mph (float omega , float r );
17+ static float _clamp_slip (float slip );
18+
19+ /**
20+ * @brief Calculates the slip error based on the wheel speeds and vehicle speed.
21+ *
22+ * @param omega_rl Rear left wheel speed in radians per second
23+ * @param omega_rr Rear right wheel speed in radians per second
24+ * @param omega_fl Front left wheel speed in radians per second
25+ * @param omega_fr Front right wheel speed in radians per second
26+ * @return float
27+ */
28+ static float _calc_slip (float omega_rl , float omega_rr , float omega_fl ,
29+ float omega_fr ) {
30+ // Use min of left/right since the slower wheel will always have less slip
31+ float omega_rear = MIN (omega_rl , omega_rr );
32+ float vehicle_speed = _calc_vehicle_speed (omega_fl , omega_fr );
33+ float rear_wheel_ground_speed = _calc_rear_wheel_speed (omega_rl , omega_rr );
34+ // Vehicle speed can be 0, so add small number to denominator to prevent
35+ // divide by 0 (though TC should not even be active at 0 mph)
36+ float slip =
37+ (rear_wheel_ground_speed - vehicle_speed ) / MAX (vehicle_speed , 1e-6f );
38+ return _clamp_slip (slip );
39+ }
40+
41+ /**
42+ * @brief Clamps the slip value from [0, 1]
43+ *
44+ * @param slip The slip ratio
45+ * @return float The slip ratio clamped to [0, 1]
46+ */
47+ static float _clamp_slip (float slip ) {
48+ if slip
49+ < 0 { return 0 ; }
50+ else if (slip > 1 ) {
51+ return 1 ;
52+ } else {
53+ return slip ;
54+ }
55+ }
56+
57+ /**
58+ * @brief Gets a better esitmate of vehicle speed by averaging the speed
59+ * calculated from the front wheels with the speed calculated from the rear
60+ * wheels.
61+ *
62+ * @param omega_fl Front left wheel speed in radians per second
63+ * @param omega_fr Front right wheel speed in radians per second
64+ * @return float Vehicle speed in miles per hour
65+ */
66+ static float _calc_vehicle_speed (float omega_fl , float omega_fr ) {
67+ // Use min of left/right since the slower wheel will always have less slip
68+ float omega_front = MIN (omega_fl , omega_fr );
69+ return (_rads_to_mph (omega_front , TIRE_DIAMETER / 2 ) + dti_get_mph ()) / 2 ;
70+ }
71+
72+ /**
73+ * @brief Calculates the rear wheel ground speed.
74+ *
75+ * @param omega_rl Rear left wheel speed in radians per second
76+ * @param omega_rr Rear right wheel speed in radians per second
77+ * @return float The rear wheel ground speed in miles per hour
78+ */
79+ static float _calc_rear_wheel_speed (float omega_rl , float omega_rr ) {
80+ // Use min of left/right since the slower wheel will always have less slip
81+ float omega_rear = MIN (omega_rl , omega_rr );
82+ return _rads_to_mph (omega_rear , TIRE_DIAMETER / 2 );
83+ }
84+
85+ /**
86+ * @brief Converts rad/s and inches to MPH ground speed.
87+ *
88+ * @param omega Angular velocity in radians per second
89+ * @param r Radius in inches
90+ * @return float The ground speed in MPH
91+ */
92+ static float _rads_to_mph (float omega , float r ) {
93+ // Convert radians per second to miles per hour
94+ float wheel_speed_ips = omega * r ; // Inches per second
95+ float wheel_speed_mph =
96+ wheel_speed_ips * SECONDS_TO_HOURS * INCHES_TO_MILES ; // Miles per hour
97+ return wheel_speed_mph ;
98+ }
0 commit comments