Hi, I’m new to programming and I’m having trouble with setting the heading of my inertial sensor. Can anyone please help me? Thanks!
Welcome to the VEXForum @FYRE1010!
The error you are getting appears to be something to do with the configuration of your inertial sensor in your code. Could you post your devices configuration (either in the Smart Configuration or in the robot-config.cpp)?
Yes, sorry for the late reply. Here is my robot-config.cpp:
#include "vex.h"
using namespace vex;
using signature = vision::signature;
using code = vision::code;
// A global instance of brain used for printing to the V5 Brain screen
brain Brain;
// VEXcode device constructors
motor LeftBack = motor(PORT17, ratio18_1, false);
motor LeftFront = motor(PORT18, ratio18_1, false);
motor RightBack = motor(PORT20, ratio18_1, false);
motor RightFront = motor(PORT19, ratio18_1, false);
controller Controller1 = controller(primary);
motor LeftIntake = motor(PORT14, ratio6_1, false);
motor RightIntake = motor(PORT13, ratio6_1, false);
motor Rollers = motor(PORT16, ratio6_1, true);
motor Pooper = motor(PORT15, ratio6_1, true);
inertial Inertial1 = inertial(PORT11);
// VEXcode generated functions
// define variable for remote controller enable/disable
bool RemoteControlCodeEnabled = true;
/**
* Used to initialize code/tasks/devices added using tools in VEXcode Pro.
*
* This should be called at the start of your int main function.
*/
void vexcodeInit( void ) {
// nothing to initialize
}
Hmm. Your initialization looks ok.
Could you send your code, main.cpp? There isn’t much more we can do without seeing your code.
Use [code] ... [/code]
tags to format your code.
Your robot-config.cpp file formatted:
#include “vex.h”
using namespace vex;
using signature = vision::signature;
using code = vision::code;
// A global instance of brain used for printing to the V5 Brain screen
brain Brain;
// VEXcode device constructors
motor LeftBack = motor(PORT17, ratio18_1, false);
motor LeftFront = motor(PORT18, ratio18_1, false);
motor RightBack = motor(PORT20, ratio18_1, false);
motor RightFront = motor(PORT19, ratio18_1, false);
controller Controller1 = controller(primary);
motor LeftIntake = motor(PORT14, ratio6_1, false);
motor RightIntake = motor(PORT13, ratio6_1, false);
motor Rollers = motor(PORT16, ratio6_1, true);
motor Pooper = motor(PORT15, ratio6_1, true);
inertial Inertial1 = inertial(PORT11);
// VEXcode generated functions
// define variable for remote controller enable/disable
bool RemoteControlCodeEnabled = true;
/**
* Used to initialize code/tasks/devices added using tools in VEXcode Pro.
* This should be called at the start of your int main function.
*/
void vexcodeInit( void ) {
// nothing to initialize
}
Okay, got it. Here’s my main.cpp (Just scroll down to the DriveTo function and autonomous):
#include "vex.h"
// ---- START VEXCODE CONFIGURED DEVICES ----
// Robot Configuration:
// [Name] [Type] [Port(s)]
// LeftBack motor 17
// LeftFront motor 18
// RightBack motor 20
// RightFront motor 19
// Controller1 controller
// LeftIntake motor 14
// RightIntake motor 13
// Rollers motor 16
// Pooper motor 15
// Inertial1 inertial 11
// ---- END VEXCODE CONFIGURED DEVICES ----
using namespace vex;
//functions for motor operations
void Intakes(double speed, double length, bool skip = true) {
LeftIntake.setVelocity(speed, percent);
RightIntake.setVelocity(speed, percent);
LeftIntake.spinFor(fwd, length, degrees, false);
RightIntake.spinFor(fwd, -length, degrees, skip);
}
void Index(double speed, double length, bool skip = true) {
Rollers.setVelocity(speed, percent);
Rollers.spinFor(fwd, length, degrees, skip);
}
void Score(double speed, double length, bool skip = true) {
Rollers.setVelocity(speed, percent);
Pooper.setVelocity(speed, percent);
Rollers.spinFor(fwd, length, degrees, false);
Pooper.spinFor(reverse, length, degrees, skip);
}
void Poop(double speed, double length, bool skip = true) {
Pooper.setVelocity(speed, percent);
Pooper.spinFor(fwd, length, degrees, skip);
}
void Drive(double speed, double length, bool skip = true) {
LeftBack.setVelocity(speed, percent);
LeftFront.setVelocity(speed, percent);
RightBack.setVelocity(speed, percent);
RightFront.setVelocity(speed, percent);
LeftBack.setStopping(brake);
LeftFront.setStopping(brake);
RightBack.setStopping(brake);
RightFront.setStopping(brake);
LeftBack.spinFor(fwd, length, degrees, false);
LeftFront.spinFor(fwd, length, degrees, false);
RightFront.spinFor(reverse, length, degrees, false);
RightBack.spinFor(reverse, length, degrees, skip);
}
void Turn(double speed, double length, bool skip = true) {
LeftBack.setVelocity(speed, percent);
LeftFront.setVelocity(speed, percent);
RightBack.setVelocity(speed, percent);
RightFront.setVelocity(speed, percent);
LeftBack.setStopping(brake);
LeftFront.setStopping(brake);
RightBack.setStopping(brake);
RightFront.setStopping(brake);
LeftBack.spinFor(reverse, length, degrees, false);
LeftFront.spinFor(reverse, length, degrees, false);
RightFront.spinFor(reverse, length, degrees, false);
RightBack.spinFor(reverse, length, degrees, skip);
LeftBack.stop();
LeftFront.stop();
RightBack.stop();
RightFront.stop();
}
void Strafe(double speed, double length, bool skip = true) {
LeftBack.setVelocity(speed, percent);
LeftFront.setVelocity(speed, percent);
RightBack.setVelocity(speed, percent);
RightFront.setVelocity(speed, percent);
LeftBack.setStopping(brake);
LeftFront.setStopping(brake);
RightBack.setStopping(brake);
RightFront.setStopping(brake);
LeftBack.spinFor(reverse, length, degrees, false);
LeftFront.spinFor(fwd, length, degrees, false);
RightFront.spinFor(fwd, length, degrees, false);
RightBack.spinFor(reverse, length, degrees, skip);
LeftBack.stop();
LeftFront.stop();
RightBack.stop();
RightFront.stop();
}
void Turn() {
// code to be executed
}
void FlipOut(bool skip = true) {
LeftIntake.setVelocity(100, percent);
RightIntake.setVelocity(100, percent);
LeftIntake.spinFor(fwd, 1000, degrees, false);
RightIntake.spinFor(fwd, 1000, degrees, true);
Pooper.setStopping(hold);
Index(100, -100, false);
wait(0.500, sec);
Index(100, 100, skip);
}
void T_Auton() {
Strafe(50, 300, true);
LeftIntake.setVelocity(100, percent);
RightIntake.setVelocity(100, percent);
RightIntake.spinFor(fwd, 1000, degrees, false);
wait(0.125, sec);
LeftIntake.spinFor(fwd, 1000, degrees, false);
Index(100, -150, true);
Drive(50, 300, false);
Intakes(100, 1000, false);
Index(100, 300, false);
wait(1.000, sec);
Score(100, 900, true);
Drive(30, -300, true);
Intakes(100, -150, true);
Turn(30, 260, true);
Strafe(65, -1350, true);
Drive(50, 300, true);
Score(100, -150, true);
Score(100, 1150, true);
Drive(50, -500, true);
Strafe(65, -1000, true);
Turn(50, 250, true);
Intakes(100, 6000, false);
Drive(50, 1300, true);
Score(100, 2000, true);
Drive(100, -900, true);
/*
Pooper.setStopping(hold);
Index(100, -100, true);
LeftIntake.setVelocity(100, percent);
RightIntake.setVelocity(100, percent);
LeftIntake.spinFor(fwd, 1000, degrees, false);
RightIntake.spinFor(fwd, 1000, degrees, true);
wait(0.500, sec);
Index(100, 100, true);
Intakes(100, 20000, false);
Drive(30, 700, true);
Turn(75, 725, true);
Drive(30, 1050, true);
Score(100, -150, true);
Score(100, 3150, true);
Drive(100, -800, true);
*/
}
void S_Auton() {
Pooper.stop(vex::brakeType::hold);
Index(100, -100, true);
LeftIntake.setVelocity(100, percent);
RightIntake.setVelocity(100, percent);
LeftIntake.spinFor(fwd, 1000, degrees, false);
RightIntake.spinFor(fwd, 1000, degrees, true);
wait(1, sec);
Index(100, 100, true);
LeftBack.setStopping(brake);
LeftFront.setStopping(brake);
RightBack.setStopping(brake);
RightFront.setStopping(brake);
Pooper.spinFor(fwd, 25, degrees, false);
Intakes(100, 500, false);
LeftBack.setVelocity(30, percent);
LeftFront.setVelocity(30, percent);
RightBack.setVelocity(30, percent);
RightFront.setVelocity(30, percent);
LeftBack.spinFor(fwd, 700, degrees, false);
LeftFront.spinFor(fwd, 700, degrees, false);
RightFront.spinFor(reverse, 700, degrees, false);
RightBack.spinFor(reverse, 700, degrees, true);
LeftBack.stop();
LeftFront.stop();
RightBack.stop();
RightFront.stop();
LeftBack.setVelocity(75, percent);
LeftFront.setVelocity(75, percent);
RightBack.setVelocity(75, percent);
RightFront.setVelocity(75, percent);
LeftBack.spinFor(reverse, 725, degrees, false);
LeftFront.spinFor(reverse, 725, degrees, false);
RightFront.spinFor(reverse,725, degrees, false);
RightBack.spinFor(reverse, 725, degrees, true);
LeftBack.stop();
LeftFront.stop();
RightBack.stop();
RightFront.stop();
LeftBack.setVelocity(30, percent);
LeftFront.setVelocity(30, percent);
RightBack.setVelocity(30, percent);
RightFront.setVelocity(30, percent);
LeftBack.spinFor(fwd, 1050, degrees, false);
LeftFront.spinFor(fwd, 1050, degrees, false);
RightFront.spinFor(reverse, 1050, degrees, false);
RightBack.spinFor(reverse, 1050, degrees, true);
LeftBack.stop();
LeftFront.stop();
RightBack.stop();
RightFront.stop();
Score(100, -150, true);
Intakes(100, 3000, false);
Score(100, 3150, true);
LeftBack.setVelocity(75, percent);
LeftFront.setVelocity(75, percent);
RightBack.setVelocity(75, percent);
RightFront.setVelocity(75, percent);
LeftBack.spinFor(fwd, -800, degrees, false);
LeftFront.spinFor(fwd, -800, degrees, false);
RightFront.spinFor(reverse, -800, degrees, false);
RightBack.spinFor(reverse, -800, degrees, true);
LeftBack.stop();
LeftFront.stop();
RightBack.stop();
RightFront.stop();
}
#include "C:/Program Files (x86)/VEX Robotics/VEXcode Pro V5/sdk/vexv5/gcc/include/math.h"
//Inertial1.setHeading(0, degrees);
double x = 0;
double y = 0;
double heading1 = Inertial1.heading(degrees);
double DriveTo(double gox, double goy, double speed, bool skip = true) {
double move = 600*sqrt( pow(gox, 2) + pow(goy, 2));
LeftBack.setVelocity(30, percent);
LeftFront.setVelocity(30, percent);
RightBack.setVelocity(30, percent);
RightFront.setVelocity(30, percent);
if (gox >= 0) {
double theta = atan(goy/gox);
LeftBack.spin(forward);
LeftFront.spin(forward);
RightFront.spin(forward);
RightBack.spin(forward);
waitUntil(Inertial1.heading(degrees) >= theta);
}
else {
double theta = 180 - atan(goy/gox);
LeftBack.spin(reverse);
LeftFront.spin(reverse);
RightFront.spin(reverse);
RightBack.spin(reverse);
waitUntil(Inertial1.heading(degrees) == theta);
}
LeftBack.stop();
LeftFront.stop();
RightFront.stop();
RightBack.stop();
Drive(speed, move, skip);
x = x + gox;
y = y + goy;
heading1 = Inertial1.heading(degrees);
return x;
return y;
return heading1;
};
void usercontrol( void ) {
while(1) {
double a1 = Controller1.Axis1.value();
double axis1 = 100*pow(0.01*a1, 3);
//double axis1 = 0.0000363992*pow(a1, 3) + 0*pow(a1, 2) + 0.237365*a1;
double axis3 = 100*( pow( (0.01*Controller1.Axis3.value()), 3) );
double axis4 = 100*( pow( (0.01*Controller1.Axis4.value()), 3) );
double turnx = axis1;
double leftback = axis3 - axis4 + turnx;
double leftfront = axis3 + axis4 + turnx;
double rightback = axis3 + axis4 - turnx;
double rightfront = axis3 - axis4 - turnx;
LeftBack.spin(vex::directionType::fwd, leftback, vex::velocityUnits::pct);
LeftFront.spin(vex::directionType::fwd, leftfront, vex::velocityUnits::pct);
RightBack.spin(vex::directionType::fwd, -rightback, vex::velocityUnits::pct);
RightFront.spin(vex::directionType::fwd, -rightfront, vex::velocityUnits::pct);
if (Controller1.ButtonL1.pressing()) {
Rollers.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
Pooper.spin(vex::directionType::rev, 100, vex::velocityUnits::pct);
}
else if (Controller1.ButtonR2.pressing()) {
LeftIntake.spin(vex::directionType::rev, 100, vex::velocityUnits::pct);
RightIntake.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
Rollers.spin(vex::directionType::rev, 100, vex::velocityUnits::pct);
Pooper.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
}
else if (Controller1.ButtonL2.pressing()) {
LeftIntake.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
RightIntake.spin(vex::directionType::rev, 100, vex::velocityUnits::pct);
Rollers.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
Pooper.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
}
else if (Controller1.ButtonR1.pressing()) {
LeftIntake.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
RightIntake.spin(vex::directionType::rev, 100, vex::velocityUnits::pct);
Rollers.spin(vex::directionType::fwd, 80, vex::velocityUnits::pct);
}
else if (Controller1.ButtonA.pressing()) {
LeftIntake.spin(vex::directionType::rev, -100, vex::velocityUnits::pct);
RightIntake.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
Rollers.spin(vex::directionType::rev, 100, vex::velocityUnits::pct);
}
else {
LeftIntake.stop(vex::brakeType::brake);
RightIntake.stop(vex::brakeType::brake);
Rollers.stop(vex::brakeType::brake);
Pooper.stop(vex::brakeType::hold);
}
}
}
void autonomous( void ) {
Inertial1.calibrate();
wait(2, sec);
//FOR TOURNAMENT AUTONOMOUS:
//T_Auton();
//FOR PROGRAMMING SKILLS:
//S_Auton();
DriveTo(1, 1, 50, true);
}
int main() {
competition Competition;
Competition.drivercontrol(usercontrol);
Competition.autonomous(autonomous);
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
while(true){
wait(100,msec);
}
}
needs to be inside a function
Will that fix my error? My problem is that when I use the Inertial1.setheading(0, degrees); function it isn’t working. Other than that, it works.
you can only use Inertial1 to set heading etc. inside a function. so move this
Inertial1.setHeading(0, degrees);
inside main
Thank you! This solved my problem.