I am trying to create code that will slow down the robot as it gets closer to an object.
So far I have been able to write code that will stop the robot using an “if” statement with the conditional being the range as measured by sonar.
if(sonar.getRangeInches() < 40) {
motor.stop();
However, when I modify the code such that I want to change the speed on approach, I get “robotDrive…output not updated enough”. Any ideas? I’ve gotten the code to run by disabling the motor safety, but the sensitivity doesn’t change at all.
You can also do something like this. It starts a linear deceleration of the magnitude for mecanum drive. Just change out the numbers and the drive base and it will be fine.
If you’re using CammandBased java, you should take a look at Gearsbot. It uses PID control to move to a certain distance away from an object and stop, which seems to be exactly what you’re trying here.
You might be better off overriding the set method of your motor controller. If you do it there, you only have to do it once, and not worry about it anywhere else.
You may also want to only reduce the throttle if the applied voltage will push you towards the wall, but allow full throttle to push you away from the wall.