1. 5 weeks ago

    I am new to EDR, but I do have experience with VEX IQ and Graphical RobotC. We have begun using VCS on our V5 and I want to help my students use the Ultrasonic Range Finder for calculating distance. The example code that I have found only shows how to display the distance data on the brain. Can someone please post some sample code for VCS that would show how to use the sonar distance data to control movement? For example:
    Move forward until sonar distance is less than 100mm

    Thanks for any help!

  2. last week

    I got mine to work in VCS and have been using it for autonomous codes. I too found very little help and learned this from trial and error. I'm using the c++ pro to write this....

    1. Insert the sonar call command from your options in the congig file, set the port to the firts of the two sequential ports you have the sonar pugged into. I chose E and F.

    2. Set up a line on your LCD to print, and then insert the sonar get distance command inside the print function. Make sure this is inside a while loop and not just after the int main. If it's not, it won't continuaously update on the screen.

    3. Now you should be able to see the sonar distance on your screen as you run your code to make sure it's working before trying to control stuff.

    4. Set up any control function, like an if statement, and say something like if (sonar.distance(vex::distance::mm)>10)
    (
    Make motor do something.........
    )
    Else
    (
    Motor.stop()

    Of course my syntax is a little off cuz I'm not behind my computer, but the structure is how I made mine work.

    If this doesn't make sense I can actually.post the code.later for reference.

  3. This code is operating a bot with a V5 brain and 393 motors for the drive, but the sonar commands wouldn't make a difference...... This should be a great start......

    in config.h----------

    vex::sonar Sonar = vex::sonar(Brain.ThreeWirePort.E); //E is the output of sonar, F is the input

    in main.cpp---------------

    int main()
    {
    while (1)
    {
    Brain.Screen.print("Sonar Distance is %f CM ",Sonar.distance(vex::distanceUnits::cm));

    if((Sonar.distance(vex::distanceUnits::cm)>10))
    {
    LeftMotor.setVelocity(100,vex::percentUnits::pct);
    RightMotor.setVelocity(100,vex::percentUnits::pct);
    LeftMotor.spin(vex::directionType::rev);
    RightMotor.spin(vex::directionType::fwd);
    ArmMotor.spin(vex::directionType::fwd);
    vex::task::sleep(500);
    ArmMotor.spin(vex::directionType::rev);
    vex::task::sleep(500);
    }
    else
    {
    LeftMotor.setVelocity(100,vex::percentUnits::pct);
    RightMotor.setVelocity(100,vex::percentUnits::pct);
    LeftMotor.spin(vex::directionType::fwd);
    RightMotor.spin(vex::directionType::rev);
    vex::task::sleep(3000);
    LeftMotor.spin(vex::directionType::fwd);
    RightMotor.spin(vex::directionType::fwd);
    vex::task::sleep(1000);
    ClawMotor.spin(vex::directionType::rev);
    vex::task::sleep(1000);
    }
    }
    }

 

or Sign Up to reply!