How to make VEX IQ robot drive straight with VEXcode blocks
Mini Series – How to make a reliable VEX IQ programming skills with VEXcode blocks
This is the part 1 of the mini series.
Robot skills challenge in VEX IQ Challenge (VIQC) consist of Driving Skills Matches, which will be entirely driver controlled, and Programming Skills Matches, which will be autonomous with limited human interaction. In the programming skills matches, beginner teams have always struggled with unreliable autonomous routine. I have heard the same lines over and over again
This worked yesterday.
It worked at my home!
Kids are very frustrated with the unreliable autonomous routine, and choose to give up on autonomous and simply focus on Driving Skills Matches, because it’s more fun and under their control. 80%+ of VEX IQ teams in worlds don’t have or don’t know how to have a reliable autonomous program.
Latest VEX IQ Middle School World Skills Standings leader board, some of the top 50 teams in world (total 618 teams on the board) still suffer from unreliable programming skills code.
We have decided to publish a mini series of tutorials to help the teams to improve their Programming Skills Matches score.
839 coding tutorial
Drive straight code snippet for VEXcode Blocks.
Create a my block for driveStraight. We are implementing a simple PID controller (technically, this is only a P controller). PID (proportional–integral–derivative) controller is a control loop mechanism employing feedback that is widely used in industrial control systems and a variety of other applications requiring continuously modulated control. If you would like to know more PID controller’s algorithm, you can start with the Wikipedia page. Matlab has a great video tutorial published to explain PID control on YouTube.
Calling driveStraight my block
At the beginning of the program, make sure you set the motor brake mode to brake and calibrate the gyro for 2 seconds. Then you can simply call driveStraight we created earlier.
- Distance: 500 – how far you want your robot to travel in degrees
- Heading: 0 – target heading of the robot
- Velocity: 60 – how fast you want your robot to travel, we recommend to start slow like 50 – 70 before you go full speed at 100. But it also depends on your robot’s gear ratio.
- Kp: 2 – proportional control value, the bigger the number, the faster the robot correct itself, but if the value is too big, correction will overshoot, and you will see robot zigzag. Kp = 0 means there is no correction applied.
How to make a reliable VEX IQ programming skills continues with part 2 – Precise turn with VEX IQ gyro
Join Our Champion Team to Start Your STEM Journey Today
Become Part of Caution Tape to Dream the Impossible.
Wow! This helps a lot with my program! Thanks!
You are very welcome. Feel free to ask us questions if you have any.
Thanks for this wonderful tutorial. My question is how do you set up your drive? It is not using the standard set up? Also does this work with 2 and 4 motor drive?
You will not set up drivetrain in this case. Instead, simply set up motor individually, you will have the full control of the robot, not limited by VEXcode provides.
Do you recommend to use only proportional control from PID, or the whole proportional, integral, derivative system from PID? My team is using the Razorbill robot from Ben Lipper’s channel.
It really depends your team’s programming skills level and robot design. I would recommend start with P controller first, let the kids fully understand it before they add Ki and Kd. Tuning Kp itself is already a big task for the whole autonomous route, adding Ki and Kd will only make kids more confused. Hope this helps.
That really helps, thank you so much! We were just wondering because we saw your turns for autonomous at the start of a Vex IQ Rise Above match in this video: https://www.youtube.com/watch?v=cjxkLA5We4k at 0:34. We were wondering how we could do that. Do you have any suggestions on how to make that work?
That’s a 2 wheel turn, all you need to do is have left and right wheel turning at different speed, say right wheel is 50%, left wheel is 100%, you will get a 2 wheel turn, and still can stop at certain angle. But the hard part is to know where the robot ending position is, that’s just trial and error for IQ students. Trigonometry is a bit too early for them.
How would you run both motors at the same time? Also, do you recommend to use Vexcode or RobotC for programming?
Just turn them on in sequential, they will run at the same time. RobotC’s debugging capability is much better, but limited platform support. Given it’s old software, I doubt it will be maintained as well. That’s a hard choice to make. Our best team still uses RobotC because that’s what they are familiar with and have existing code from previous years.
That really helps, thank you! I was trying to get both motors to run at the same time, but they would only run one at a time. Attached is an image of something similar to what I was trying. So I ran my left and right motors sequentially like that, but only ran one at a time and not both at the same time. Do you have any suggestions on how to get that to work?
https://www.google.com/search?q=vexcode+two+motors+drivetrain&tbm=isch&ved=2ahUKEwiSvOXR57TuAhXMK6wKHWf5D9MQ2-cCegQIABAA&oq=vexcode+two+motors+drivetrain&gs_lcp=CgNpbWcQA1D2pQdYrMMHYNDEB2gAcAB4AIAB5wGIAZwLkgEFNS4zLjOYAQCgAQGqAQtnd3Mtd2l6LWltZ8ABAQ&sclient=img&ei=HYgNYNK5M8zXsAXn8r-YDQ&bih=665&biw=1366&safe=active&ssui=on#imgrc=hLwMogztf_H6AM
Are you using set motor position or spin motor [direction]? First one is always blocking call, which won’t allow the code to go to next line until it’s finished. But if you use spin motor with “and don’t wait” option, you will be able to move to the next line, which allows you to run 2 motors at the same time.
Okay, I have tried it and it worked! I want to really thank you for your help because our team was having so much trouble with programming and just gave up to focus on driving. We can now start programming again and the sky’s the limit. Thank you!
That’s why we created the post at first place. You are very welcome.
I inputted the image attached. sometimes it goes straight and stops. Usually it goes straight and then does 360 turns forever. Help
Typically, if that happens. Reverse the – and + in the formula should solve the issue.
Thank you, it now goes straight and stops.
Next question, how do I get it to back up, NOT turn and go forward?
ThankYou!
Going backward? Simply put negative velocity and negative distance should do it.
Thank you. It works. I think I tried every combination but that one last night!!!!!!!!
Not a problem. I’m glad it helped.
Great article, thanks! So we’ve looked at this code sample and we understand it at a high level. We got it to run and it appears to do a good job. Two questions.
1. When trying to find a KP value for your robot, what are the usually increments you test in? And what is a general range to test between? So if we start with KP of 2, should we test in increments of .25, .5, 1,ect? Also, what is the general range? 0-2, 0-5, 0-10, 0-100, ect? Just trying to set a baseline for how and what to test.
2. As the velocity value increases, will the KP value change as well? Or your KP value will always be the same regardless of velocity? In our test, it seemed more accurate at 50% velocity with a KP value as 1, but when increasing the velocity to 80%, it didn’t feel like it was doing much. So trying to understand if velocity increases, is there a correlation to the KP value going up or down.
general rule, the higher the velocity, the less the kp. kp varies from robot to robot, but kp=2 is a good starting point, and decimal works.
Thanks Daniel!
one more question. As velocity increases, does the distance value decrease as well? I tried to look up ‘motor clicks’ as it was mentioned in the video to see how that corelates to the distance, but couldn’t find anything. We ask as we up our velocity, it seems like the distance increase a little bit. Thanks?!
Can you use motor time outs with this code?
Probably not motor timeout. But you can implement similar logic.
Hi just reading this but how to go backwards. Our team tried using negative velocity but it went on forever and would not stop.
Have you tried negative distance going backward?
I have tried but it did not work
Also it stopped going on forever, I am not sure what the error is
Is this for your controller or auton? Also how do you tell the robot to go straight, go backward when you need it?
This is for auton.
How do I make it go backwards, I tried making the velocity and distance negative but it would just spin in 2 circles? Please I really need this for my upcoming comp.
Negative velocity and distance. You may also need negative Kp too.