Move_relative not working?

#include “main.h”

/**

  • A callback function for LLEMU’s center button.
  • When this callback is fired, it will toggle line 2 of the LCD text between
  • “I was pressed!” and nothing.
    */
    void on_center_button() {
    static bool pressed = false;
    pressed = !pressed;
    if (pressed) {
    pros::lcd::set_text(2, “I was pressed!”);
    } else {
    pros::lcd::clear_line(2);
    }
    }

/**

  • Runs initialization code. This occurs as soon as the program is started.

  • All other competition modes are blocked by initialize; it is recommended

  • to keep execution time for this mode under a few seconds.
    */
    void initialize() {
    pros::lcd::initialize();
    pros::lcd::set_text(1, “Hello PROS User!”);

    pros::lcd::register_btn1_cb(on_center_button);
    }

/**

  • Runs while the robot is in the disabled state of Field Management System or
  • the VEX Competition Switch, following either autonomous or opcontrol. When
  • the robot is enabled, this task will exit.
    */
    void disabled() {}

/**

  • Runs after initialize(), and before autonomous when connected to the Field
  • Management System or the VEX Competition Switch. This is intended for
  • competition-specific initialization routines, such as an autonomous selector
  • on the LCD.
  • This task will exit when the robot is enabled and autonomous or opcontrol
  • starts.
    */
    void competition_initialize() {}

/**

  • Runs the user autonomous code. This function will be started in its own task
  • with the default priority and stack size whenever the robot is enabled via
  • the Field Management System or the VEX Competition Switch in the autonomous
  • mode. Alternatively, this function may be called in initialize or opcontrol
  • for non-competition testing purposes.
  • If the robot is disabled or communications is lost, the autonomous task
  • will be stopped. Re-enabling the robot will restart the task, not re-start it
  • from where it left off.
    */
    void autonomous() {}

/**

  • Runs the operator control code. This function will be started in its own task
  • with the default priority and stack size whenever the robot is enabled via
  • the Field Management System or the VEX Competition Switch in the operator
  • control mode.
  • If no competition control is connected, this function will run immediately
  • following initialize().
  • If the robot is disabled or communications is lost, the
  • operator control task will be stopped. Re-enabling the robot will restart the
  • task, not resume it from where it left off.
    */
    void opcontrol() {
    pros::Motor left_mtr(20);
    left_mtr.move_relative(1000,100);
    }

if anyone could tell me how to put this in that box that would help. This simple code where it literally just moves 1000 units doesn’t work, in fact the motor doesn’t even make a sound, anyone find some issues with it?

Uh, been a while since I used PROS. My best guess is that the opcontrol void exits immediately after starting the move_relative and it just appears like it doesn’t run. Maybe put a wait command after it. You could also look at the PROS API here.
This is the code in their example (shortened for clarity).

void autonomous() {
  pros::Motor motor (1);
  motor.move_absolute(100, 100); // Moves 100 units forward
  while (!((motor.get_position() < 105) && (motor.get_position() > 95))) {
    // Continue running this loop as long as the motor is not within +-5 units of its goal
    pros::delay(2);
  }
  motor.tare_position();
}

You can apply the same logic to what you are trying to do. The while loop just runs while it is not within ±5 of the goal. The tare_position resets the motor encoder position to zero.

3 Likes

Oh and to do the good forum formatting, just wrap the code in ```cpp at the beginning and ``` at the end.

1 Like

Add a delay after the move. Like @trontech569 said, its probably exiting right after it calls the move function. Since your not doing anything else, you probably don’t need all the code he posted. A simple pros::delay(1000) after your move statement should work.

Edit: Also you should probably put auto code into the auto period. You can test it with either a competition switch or you can use the competition option on the controller.

2 Likes

Yes, both @trontech569 and @PortalStorm4000 are correct move_relative() is a non-blocking function. It just sets new target for the motor and returns control to the program.

https://pros.cs.purdue.edu/v5/api/cpp/motors.html#move-relative