Hello all
I want to program two buttons that when pressed spin the robot 180 degrees then stops. Currently when I press the two buttons the robot turns slightly to the right. Help Please
My code is based off of telling the arcade drive to turn for a set amount of time and then stop. Attached code is placed in Teleop.
It takes about 5 sec to spin the robot in 180degrees under operator control.
How would I make the robot turn 180 deg using encoders on the gear box programing wise?
Thanks in advance
I think your problem is that you are checking if 100 or 40 is greater than or equal to your loop count. When you enter the loop, that counter will be 0, so it will exit the loop right away. You want LESS than or equal to - or you just need to swap the wires going into the comparison blocks you already have.
Also, you don’t need to increment your loop counter - it increments itself every time the loop restarts.
I would place it in timed tasks for ease of use and roubostness
The problem is that teleop is operated sequentially with data communication. When you put the 5 second while loop in telop, no Driver Station communication occurs for those 5 seconds. There are various safety mechanisms that kick in and stop the robot if communication doesn’t occur. That is why your code is stopping.
If you have encoders on your left and right wheels, the difference in values between the two tells you something about the direction your robot is facing. The conversion between encoder counts and robot heading is a function of the wheel circumference and the distance between left and right wheels.
What you will need to do is something like this:
- Continuously compute the robot heading from the encoder values.
- While the buttons aren’t pressed, drive normally, but continuously save the current heading + 180 degrees as a target value.
- While the buttons are pressed, turn in whatever direction will make your current heading approach the target.
You’ll probably find that the robot overshoots the target and starts oscillating back and forth. That’s when you should start learning how to use PID control.
We did something similar to that last year in order for our robot to drive forward for an interval, then stop and actuate it’s bridge manipulator.
Anyone on Curie field that saw the big black robot crash full-speed into the bridge during autonomous (twice :ahh: ) saw how well that worked out.
The best we could tell, any variation that uses up processing speed would make the action take longer than anticipated, and we used a similar method to yours.
I would recommend using encoders