Skip to content

Commit f0dd36d

Browse files
authored
Bug fix: Limited the max motor command values
Fixed a problem where the values sent to the Arduino were not capped. The robot turns violently causing the motors to stop working temporarily. Reduced the cap for manual driving to 120 from 255 and applied the cap to autonomous driving where it wasn't applied before. Tested in two physical rovers and in the sim.
1 parent 1c2d30d commit f0dd36d

File tree

1 file changed

+28
-27
lines changed

1 file changed

+28
-27
lines changed

src/abridge/src/abridge.cpp

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -161,40 +161,41 @@ void driveCommandHandler(const geometry_msgs::Twist::ConstPtr& message) {
161161
float left = (message->linear.x); //target linear velocity in meters per second
162162
float right = (message->angular.z); //angular error in radians
163163

164-
if (currentMode == 1) //manual control
164+
// Cap motor commands at 120. Experimentally determined that high values (tested 180 and 255) can cause
165+
// the hardware to fail when the robot moves itself too violently.
166+
int max_motor_cmd = 120;
167+
168+
// Assumes left and right are always between -1 and 1
169+
float linear = left * max_motor_cmd;
170+
float angular = right * max_motor_cmd;
171+
172+
left = linear - angular;
173+
right = linear + angular;
174+
175+
if (left > max_motor_cmd)
176+
{
177+
left = max_motor_cmd;
178+
}
179+
else if (left < -max_motor_cmd)
165180
{
181+
left = - max_motor_cmd;
182+
}
166183

167-
float linear = left * 255;
168-
float angular = right * 255; //scale values between -255 and 255;
169-
170-
left = linear - angular;
171-
right = linear + angular;
172-
173-
if (left >255)
174-
{
175-
left = 255;
176-
}
177-
else if (left < -255)
178-
{
179-
left = -255;
180-
}
181-
182-
if (right >255)
183-
{
184-
right = 255;
185-
}
186-
else if (right < -255)
187-
{
188-
right = -255;
189-
}
184+
if (right > max_motor_cmd)
185+
{
186+
right = max_motor_cmd;
187+
}
188+
else if (right < -max_motor_cmd)
189+
{
190+
right = -max_motor_cmd;
190191
}
191192

192193
int leftInt = left;
193194
int rightInt = right;
194195

195-
sprintf(moveCmd, "v,%d,%d\n", leftInt, rightInt); //format data for arduino into c string
196-
usb.sendData(moveCmd); //send movement command to arduino over usb
197-
memset(&moveCmd, '\0', sizeof (moveCmd)); //clear the movement command string
196+
sprintf(moveCmd, "v,%d,%d\n", leftInt, rightInt); //format data for arduino into c string
197+
usb.sendData(moveCmd); //send movement command to arduino over usb
198+
memset(&moveCmd, '\0', sizeof (moveCmd)); //clear the movement command string
198199
}
199200

200201

0 commit comments

Comments
 (0)