10
10
* Note that this could scale the forces *down* if they exceed the physical capabilities
11
11
* of the robot (ex. wheel slip).
12
12
*
13
- * @param wheels Wheels to compute the maximum torque scaling from
13
+ * @param force_wheels Force wheels to compute the maximum torque scaling from
14
14
* @param wheel_forces Forces to apply to each wheel
15
15
* @param battery_voltage The current battery voltage
16
16
*
17
17
* @return The amount by which to scale the force on each wheel to get the maximum
18
18
* torque possible while maintaining the same torque ratio between wheels
19
19
*/
20
- float app_control_getMaximalTorqueScaling (const Wheel_t * wheels [4 ],
20
+ float app_control_getMaximalTorqueScaling (const ForceWheel_t * force_wheels [4 ],
21
21
const float wheel_forces [4 ],
22
22
float battery_voltage )
23
23
{
@@ -26,12 +26,12 @@ float app_control_getMaximalTorqueScaling(const Wheel_t* wheels[4],
26
26
27
27
for (long i = 0 ; i < 4 ; i ++ )
28
28
{
29
- const Wheel_t * wheel = wheels [i ];
30
- const WheelConstants_t constants = app_wheel_getWheelConstants (wheel );
31
- float force = wheel_forces [i ];
29
+ const ForceWheel_t * wheel = force_wheels [i ];
30
+ const ForceWheelConstants_t constants = app_force_wheel_getWheelConstants (wheel );
31
+ float force = wheel_forces [i ];
32
32
float motor_torque =
33
33
force * constants .wheel_radius * constants .wheel_rotations_per_motor_rotation ;
34
- float curr_motor_rpm = app_wheel_getMotorSpeedRPM (wheel );
34
+ float curr_motor_rpm = app_force_wheel_getMotorSpeedRPM (wheel );
35
35
36
36
float resistive_voltage_loss = motor_torque *
37
37
constants .motor_current_per_unit_torque *
@@ -62,20 +62,22 @@ float app_control_getMaximalTorqueScaling(const Wheel_t* wheels[4],
62
62
* Note that this could scale the accelerations *down* if the given forces exceed the
63
63
* physical capabilities of the robot (ex. wheel slip).
64
64
*
65
- * @param robot The robot to compute the acceleration scaling constant for
65
+ * @param robot_constants The robot constants representing the robot
66
+ * @param battery_voltage The robot's battery voltage
67
+ * @param force_wheels The robot's force wheels
66
68
* @param linear_accel_x [m/s^2] The linear x acceleration to scale
67
69
* @param linear_accel_y [m/s^2] The linear y acceleration to scale
68
70
* @param angular_accel [rad/s^2] The angular acceleration to scale
69
71
*
70
72
* @return The scaling constant to multiply the all acceleration values by in order to
71
73
* accelerate the robot as fast as physically possible
72
74
*/
73
- float app_control_getMaximalAccelScaling (const FirmwareRobot_t * robot ,
75
+ float app_control_getMaximalAccelScaling (const RobotConstants_t robot_constants ,
76
+ float battery_voltage ,
77
+ const ForceWheel_t * force_wheels [4 ],
74
78
const float linear_accel_x ,
75
79
const float linear_accel_y , float angular_accel )
76
80
{
77
- const RobotConstants_t robot_constants = app_firmware_robot_getRobotConstants (robot );
78
-
79
81
// first convert accelerations into consistent units
80
82
// choose units of Force (N)
81
83
float normed_force [3 ];
@@ -87,25 +89,25 @@ float app_control_getMaximalAccelScaling(const FirmwareRobot_t* robot,
87
89
float wheel_forces [4 ];
88
90
force3_to_force4 (normed_force , wheel_forces );
89
91
90
- const Wheel_t * wheels [4 ];
91
- wheels [0 ] = app_firmware_robot_getFrontLeftWheel (robot );
92
- wheels [1 ] = app_firmware_robot_getBackLeftWheel (robot );
93
- wheels [2 ] = app_firmware_robot_getBackRightWheel (robot );
94
- wheels [3 ] = app_firmware_robot_getFrontRightWheel (robot );
95
-
96
- float battery_voltage = app_firmware_robot_getBatteryVoltage (robot );
97
-
98
- return app_control_getMaximalTorqueScaling (wheels , wheel_forces , battery_voltage );
92
+ return app_control_getMaximalTorqueScaling (force_wheels , wheel_forces ,
93
+ battery_voltage );
99
94
}
100
95
101
- void app_control_applyAccel (const FirmwareRobot_t * robot , float linear_accel_x ,
96
+ void app_control_applyAccel (RobotConstants_t robot_constants ,
97
+ ControllerState_t * controller_state , float battery_voltage ,
98
+ ForceWheel_t * force_wheels [4 ], float linear_accel_x ,
102
99
float linear_accel_y , float angular_accel )
103
100
{
104
- const RobotConstants_t robot_constants = app_firmware_robot_getRobotConstants (robot );
101
+ const ForceWheel_t * wheels [4 ];
102
+ wheels [0 ] = force_wheels [0 ];
103
+ wheels [1 ] = force_wheels [1 ];
104
+ wheels [2 ] = force_wheels [2 ];
105
+ wheels [3 ] = force_wheels [3 ];
105
106
106
107
// check for max acceleration in direction of the vel difference
107
- float scaling = app_control_getMaximalAccelScaling (robot , linear_accel_x ,
108
- linear_accel_y , angular_accel );
108
+ float scaling =
109
+ app_control_getMaximalAccelScaling (robot_constants , battery_voltage , wheels ,
110
+ linear_accel_x , linear_accel_y , angular_accel );
109
111
110
112
// if the (very naive) 1 tick acceleration violates the physical limits of the robot
111
113
// scale it to maximum
@@ -117,10 +119,9 @@ void app_control_applyAccel(const FirmwareRobot_t* robot, float linear_accel_x,
117
119
angular_accel *= scaling ;
118
120
}
119
121
120
- ControllerState_t * controller_state = app_firmware_robot_getControllerState (robot );
121
- float prev_linear_accel_x = controller_state -> last_applied_acceleration_x ;
122
- float prev_linear_accel_y = controller_state -> last_applied_acceleration_y ;
123
- float prev_angular_accel = controller_state -> last_applied_acceleration_angular ;
122
+ float prev_linear_accel_x = controller_state -> last_applied_acceleration_x ;
123
+ float prev_linear_accel_y = controller_state -> last_applied_acceleration_y ;
124
+ float prev_angular_accel = controller_state -> last_applied_acceleration_angular ;
124
125
125
126
float linear_diff_x = linear_accel_x - prev_linear_accel_x ;
126
127
float linear_diff_y = linear_accel_y - prev_linear_accel_y ;
@@ -151,41 +152,8 @@ void app_control_applyAccel(const FirmwareRobot_t* robot, float linear_accel_x,
151
152
float wheel_force [4 ];
152
153
speed3_to_speed4 (robot_force , wheel_force ); // Convert to wheel coordinate system
153
154
154
- app_wheel_applyForce (app_firmware_robot_getFrontLeftWheel (robot ), wheel_force [0 ]);
155
- app_wheel_applyForce (app_firmware_robot_getFrontRightWheel (robot ), wheel_force [3 ]);
156
- app_wheel_applyForce (app_firmware_robot_getBackLeftWheel (robot ), wheel_force [1 ]);
157
- app_wheel_applyForce (app_firmware_robot_getBackRightWheel (robot ), wheel_force [2 ]);
158
- }
159
-
160
- void app_control_trackVelocityInRobotFrame (FirmwareRobot_t * robot ,
161
- float linear_velocity_x ,
162
- float linear_velocity_y ,
163
- float angular_velocity )
164
- {
165
- float current_vx = app_firmware_robot_getVelocityX (robot );
166
- float current_vy = app_firmware_robot_getVelocityY (robot );
167
- float current_angular_velocity = app_firmware_robot_getVelocityAngular (robot );
168
- float current_orientation = app_firmware_robot_getOrientation (robot );
169
-
170
- // Rotate the current_velocity vector from the world frame to the robot frame
171
- float current_velocity [2 ];
172
- current_velocity [0 ] = current_vx ;
173
- current_velocity [1 ] = current_vy ;
174
- rotate (current_velocity , - current_orientation );
175
-
176
- // This is the "P" term in a PID controller. We essentially do proportional
177
- // control of our acceleration based on velocity error
178
- static const float VELOCITY_ERROR_GAIN = 10.0f ;
179
-
180
- float desired_acceleration [2 ];
181
- desired_acceleration [0 ] =
182
- (linear_velocity_x - current_velocity [0 ]) * VELOCITY_ERROR_GAIN ;
183
- desired_acceleration [1 ] =
184
- (linear_velocity_y - current_velocity [1 ]) * VELOCITY_ERROR_GAIN ;
185
-
186
- float angular_acceleration =
187
- (angular_velocity - current_angular_velocity ) * VELOCITY_ERROR_GAIN ;
188
-
189
- app_control_applyAccel (robot , desired_acceleration [0 ], desired_acceleration [1 ],
190
- angular_acceleration );
155
+ app_force_wheel_applyForce (force_wheels [0 ], wheel_force [0 ]);
156
+ app_force_wheel_applyForce (force_wheels [3 ], wheel_force [3 ]);
157
+ app_force_wheel_applyForce (force_wheels [1 ], wheel_force [1 ]);
158
+ app_force_wheel_applyForce (force_wheels [2 ], wheel_force [2 ]);
191
159
}
0 commit comments