I am trying to test out my new inertail sensor so my team can use it next year so I decided to test it with trying to make the robot turn 90 degrees. Easy right? No, it is driving in circles to infinity with the below code I based off the example tutorial. Is it that I have the sensor centered on my bot?
#include "vex.h"
using namespace vex;
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
Inertial20.calibrate();
// waits for the Inertial Sensor to calibrate
while (Inertial20.isCalibrating()) {
wait(100, msec);
}
// Turns the robot to the right]
motor FLD = motor(PORT1, ratio18_1, false);
motor FRD = motor(PORT10, ratio18_1, true);
motor MLD = motor(PORT11, ratio18_1, false);
motor MRD = motor(PORT20, ratio18_1, true);
motor RLD = motor(PORT12, ratio18_1, false);
motor RRD = motor(PORT19, ratio18_1, true);
FLD.spin(forward);
FRD.spin(reverse);
MLD.spin(forward);
MRD.spin(reverse);
RLD.spin(forward);
RRD.spin(reverse);
// Waits until the motor reaches a 90 degree turn and stops the Left and
// Right Motors.
waitUntil((Inertial20.rotation(degrees) >= 90.0));
FLD.stop();
FRD.stop();
MLD.stop();
MRD.stop();
RLD.stop();
RRD.stop();
wait(1, seconds);
}
No, it should work fine wherever you place the sensor. It will even work if you mount it sideways!
I’m guessing the problem lies in your usage of the rotation
method. I believe that rotation
returns the rotation in degrees since the time it was last reset. Try using setRotation
to zero the rotation before you make the turn?
Tried that and still same thing. I also added a set heading incase that would help. Current code:
‘’'#include “vex.h”
using namespace vex;
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
Inertial20.calibrate();
// waits for the Inertial Sensor to calibrate
while (Inertial20.isCalibrating()) {
wait(100, msec);
}
// Turns the robot to the right]
motor FLD = motor(PORT1, ratio18_1, false);
motor FRD = motor(PORT10, ratio18_1, true);
motor MLD = motor(PORT11, ratio18_1, false);
motor MRD = motor(PORT20, ratio18_1, true);
motor RLD = motor(PORT12, ratio18_1, false);
motor RRD = motor(PORT19, ratio18_1, true);
Inertial20.setHeading(0, degrees);
Inertial20.setRotation(0, degrees);
FLD.spin(forward);
FRD.spin(reverse);
MLD.spin(forward);
MRD.spin(reverse);
RLD.spin(forward);
RRD.spin(reverse);
// Waits until the motor reaches a 90 degree turn and stops the Left and
// Right Motors.
waitUntil((Inertial20.rotation(degrees) >= 90.0));
FLD.stop();
FRD.stop();
MLD.stop();
MRD.stop();
RLD.stop();
RRD.stop();
wait(1, seconds);
}
‘’’
Hello, so when you turn on the robot, does it just constantly spin in a circle around the inertial sensor? I’ve also attached sample code formatted for your robot so that it turns and slows down as it gets closer for more precision. This is how my team used to do rotations.
#include "vex.h"
using namespace vex;
double targetAngle = 90; // Target angle in degrees
double turningSpeed;
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
Inertial20.calibrate();
// waits for the Inertial Sensor to calibrate
while (Inertial20.isCalibrating()) {
wait(100, msec);
}
// Turns the robot to the right]
// Left Drive Motors
motor FLD = motor(PORT1, ratio18_1, false);
motor MLD = motor(PORT11, ratio18_1, false);
motor RLD = motor(PORT12, ratio18_1, false);
// Right Drive Motors
motor FRD = motor(PORT10, ratio18_1, true);
motor MRD = motor(PORT20, ratio18_1, true);
motor RRD = motor(PORT19, ratio18_1, true);
// Start Left Drive
FLD.spin(forward);
MLD.spin(forward);
RLD.spin(forward);
// Start Right Drive
FRD.spin(reverse);
MRD.spin(reverse);
RRD.spin(reverse);
// Wait until robot is rotated correctly
while (fabs(targetAngle - Inertial20.rotation(degrees)) >= 1.25) {
turningSpeed = targetAngle - Inertial20.rotation(degrees);
// Set Left Drive Turning Speed
FLD.setVelocity(turningSpeed, percent);
MLD.setVelocity(turningSpeed, percent);
RLD.setVelocity(turningSpeed, percent);
// Set Right Drive Turning Speed
FRD.setVelocity(turningSpeed, percent);
MRD.setVelocity(turningSpeed, percent);
RRD.setVelocity(turningSpeed, percent);
}
//Stop Left Drive
FLD.stop();
MLD.stop();
RLD.stop();
// Stop Right Drive
FRD.stop();
MRD.stop();
RRD.stop();
wait(1, seconds);
}
This code works on our robot, hope this helps!
I had to reverse a directions but it still accelerated to max speed and kept spinning in place.
Is your robot turning clockwise or counterclockwise? It could be that the direction is opposite of the one your inertial sensor is checking for. A solution/troubleshooting method for this is described in @EngineerMike’s post in this topic.
3 Likes
Alright sorry for not following up but life gets busy. The issue was not in my spin directions since apparently it did not matter before the while loop in my implementation. The problem was in the while loop I had added “*-1” to the motor spins on the wrong side. This led to the robot turning away from 90 degs causing it to accelerate. This did not stop when it came around do the inertial sensor keeping track of how many rotations you do and not just using mod to track degrees in a way that is 0-360. Once I switched the negative to the correct side it worked like a charm. Now I will keep messing with it to get more complicated autonomous designs written up. If anyany knows if the sensor is useful going in straight lines and not just for tracking turns let me know. Thanks to those who helped me figure out what was going on and hopefully new students and coaches can see this to learn how to use sensors are bring their bots to the next level. My current code is attached below.
#include “vex.h”
using namespace vex;
double targetAngle = 0; // Target angle in degrees
double turningSpeed;
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
Inertial20.calibrate();
// waits for the Inertial Sensor to calibrate
while (Inertial20.isCalibrating()) {
wait(100, msec);
}
// Turns the robot to the right]
// Left Drive Motors
motor FLD = motor(PORT1, ratio18_1, false);
motor MLD = motor(PORT11, ratio18_1, false);
motor RLD = motor(PORT12, ratio18_1, false);
// Right Drive Motors
motor FRD = motor(PORT10, ratio18_1, true);
motor MRD = motor(PORT20, ratio18_1, true);
motor RRD = motor(PORT19, ratio18_1, true);
for (int i = 0; i < 4; i++) {
targetAngle += 90;
FLD.spin(forward);
MLD.spin(forward);
RLD.spin(forward);
FRD.spin(reverse);
MRD.spin(reverse);
RRD.spin(reverse);
// Wait until robot is rotated correctly
while (targetAngle - Inertial20.rotation(degrees) >= 1.25) {
turningSpeed = (targetAngle - Inertial20.rotation(degrees)) / 2;
// Set Left Drive Turning Speed
FLD.setVelocity(turningSpeed*-1, percent);
MLD.setVelocity(turningSpeed*-1, percent);
RLD.setVelocity(turningSpeed*-1, percent);
// Set Right Drive Turning Speed
FRD.setVelocity(turningSpeed, percent);
MRD.setVelocity(turningSpeed, percent);
RRD.setVelocity(turningSpeed, percent);
}
}
//Stop Left Drive
FLD.stop();
MLD.stop();
RLD.stop();
// Stop Right Drive
FRD.stop();
MRD.stop();
RRD.stop();
wait(1, seconds);
}