Using the canned program it works to do what I want, however, when I move it into a competition template in Auton, the sensor is still reading but I lose use of the sensor
Code:
float distance;
distance=Distance3.objectDistance(mm);
// The robot will stop driving when the Range Finder is less than 300 mm away
// from an object.
if(distance > 300)
{
leftdrv.spin(forward);
rightdrv.spin(forward);
distance=Distance3.objectDistance(mm);
}
leftdrv.stop();
rightdrv.stop();
}
replace if with while and add delay
did as you said, (again) Either can be used if it is ran as a stand alone using Int Main, however, as soon as you place code in auton, it quits on the distance sensor
please show your whole code and use ``` to enclose your code. Do you use VEXnet Competition Switch?
Whole Code and yes on the vexnet
/----------------------------------------------------------------------------/
/* /
/ Module: main.cpp /
/ Author: VEX /
/ Created: Thu Sep 26 2019 /
/ Description: Competition Template /
/ /
/----------------------------------------------------------------------------*/
// ---- START VEXCODE CONFIGURED DEVICES ----
// Robot Configuration:
// [Name] [Type] [Port(s)]
// Inertial19 inertial 19
// Distance3 distance 3
// leftdrv motor 1
// rightdrv motor 10
// ---- END VEXCODE CONFIGURED DEVICES ----
#include “vex.h”
using namespace vex;
// A global instance of competition
competition Competition;
// define your global instances of motors and other devices here
/---------------------------------------------------------------------------/
/* Pre-Autonomous Functions /
/ /
/ You may want to perform some actions before the competition starts. /
/ Do them in the following function. You must return from this function /
/ or the autonomous and usercontrol tasks will not be started. This /
/ function is only called once after the V5 has been powered on and /
/ not every time that the robot is disabled. /
/---------------------------------------------------------------------------*/
void pre_auton(void) {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
float distance;
distance=Distance3.objectDistance(mm);
Inertial19.calibrate();
// waits for the Inertial Sensor to calibrate
while (Inertial19.isCalibrating()) {
wait(100, msec);
// All activities that occur before the competition starts
// Example: clearing encoders, setting servo positions, …
}
}
/---------------------------------------------------------------------------/
/* /
/ Autonomous Task /
/ /
/ This task is used to control your robot during the autonomous phase of /
/ a VEX Competition. /
/ /
/ You must modify the code to add your own robot specific commands here. /
/---------------------------------------------------------------------------*/
void autonomous(void)
{
// Turns the robot to the right
leftdrv.spin(forward);
rightdrv.spin(reverse);
// Waits until the motor reaches a 90 degree turn and stops the Left and
// Right Motors.
waitUntil((Inertial19.rotation(degrees) >= 90.0));
leftdrv.stop();
rightdrv.stop();
rotation(0);
wait(3, seconds);
float distance;
distance=Distance3.objectDistance(mm);
// The robot will stop driving when the Range Finder is less than 300 mm away
// from an object.
while(distance > 300)
{
leftdrv.spin(forward);
rightdrv.spin(forward);
wait(59,msec);
distance=Distance3.objectDistance(mm);
}
leftdrv.stop();
rightdrv.stop();
}
void usercontrol()
{}
//
// Main will set up the competition functions and callbacks.
//
int main() {
// Set up callbacks for autonomous and driver control periods.
Competition.autonomous(autonomous);
Competition.drivercontrol(usercontrol);
// Run the pre-autonomous function.
pre_auton();
// Prevent main from exiting with an infinite loop.
while (true) {
wait(100, msec);
}
}
Your code seems fine. What exactly happens when you run the autonomous.
Will make the 90 turn, but will not move after even though the device value on the brain is shown as above the threshold. Like I said, if I run this in int main, it works fine, as soon as I shift it to auton it craps out.
What is this mean? What is it for?
resets the inertial sensor, works. Only problem is with the distance sensor
I have never seen this before. Are you sure that this line of code is right? Please check the document for inertial API. I am not sure the code can be compiled. Could you check?
it compiles, it runs the inertial part of the code, it starts, turns 90 degrees, then stops and will not do anything else, I check the brain and the distance sensor shows above 300mm so it should drive forward. I can run this code if I take it out of the vex competition template and just run it in int main, but that will not help me for a competition robot.
You should check that distance is not 0, which in the current V5 vexos means there is no valid distance returned. We have changed this for EXP and IQ2 so that invalid distance returns 2500mm, but none of that code has been back ported to V5 yet.
I am really frustrated. I have checked the distance sensor and it is showing a value. Can we try a little something different. Could you show me a simple code that would drive fwd and stop 10" from a wall starting from any distance. This shouldnt be this hard. Thanks
Easy man, the problem may be something else instead of the code you posted. I am still not convinced about rotation(0).
two things you may want to do to debug:
- print out the value of the distance sensor
- add an indication flag to show which line of the code the autonomous stops. i.e
Brain.Screen.printAt(1,1 ,"check point 1");
// Turns the robot to the right
leftdrv.spin(forward);
rightdrv.spin(reverse);
// Waits until the motor reaches a 90 degree turn and stops the Left and
// Right Motors.
Brain.Screen.printAt(1,1 ,"check point 2");
waitUntil((Inertial19.rotation(degrees) >= 90.0));
Brain.Screen.printAt(1,1 ,"check point 3");
...
This works, think my brain may have been messed up.\
void autonomous(void) {
float howfar;
float point;
Inertial19.setHeading(0,degrees);
// Turns the robot to the right
LeftMotor.spin(forward);
RightMotor.spin(reverse);
// Waits until the motor reaches a 90 degree turn and stops the Left and
// Right Motors.
point=(Inertial19.heading(degrees));
while (point<10)
{
LeftMotor.spin(vex::directionType::rev, 20, vex::velocityUnits::pct);
RightMotor.spin(vex::directionType::rev, 20, vex::velocityUnits::pct);
point=(Inertial19.heading(degrees));
}
LeftMotor.stop();
RightMotor.stop();
wait(100,msec);
howfar=(Vision13.objectDistance(mm));
while(howfar>120)
{
LeftMotor.spin(forward);
RightMotor.spin(reverse);
howfar=(Vision13.objectDistance(mm));
}
LeftMotor.stop();
RightMotor.stop();
}
glad you solve the problem yourself. Good luck.