[PROSV5] Using Degrees?

Hi everyone,

I’m trying to switch from using voltage and time to move in autonomous to using degrees. However, I tried to look at the API, and it seemed to be beyond my understanding. Does anyone have any advice?

Have you tried looking at the tutorials?

This one shows how to command the drive using the built in sensors and PID…
https://pros.cs.purdue.edu/v5/tutorials/walkthrough/clawbot.html#simple-autonomous

Okay I have it set to where I can get motors to use degrees, but it seems that the last option I put overrides the first. For example, if I were to put:

#define LEFT_MOTOR 1
#define RIGHT_MOTOR 2

void chassis_move(const double position, const int32_t velocity){
motor_move_relative(LEFT_MOTOR, position, velocity);
motor_move_relative(RIGHT_MOTOR, position, velocity);
}

void opcontrol(){
chassis_move(507, 200);
delay(3);
chassis_move(868, 100);
}

Only the last line in opcontrol would be applied. Probably a noob question, but what am I doing wrong here?

You have:

  1. Tell motors to spin to 507 units (don’t wait for motors to get there)
  2. Wait 3msec
  3. Tell motors to spin to 868 units (don’t wait for motors to get there)

Do you see why only the last statement would be observed?

1 Like

Just to follow along with what Elliot wrote, check out the docs on that motor move_relative function.

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

Okay, so I see that a while loop needs to be created in order to block program execution until the movement is finished, like so (pulled straight from the PROS 3 API reference):

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

  motor_move_relative(1, 100, 100); // Also moves 100    units forward
  while (!((motor_get_position(1) < 205) && (motor_get_position(1) > 195))) {
    delay(2);
  }
}   

However, I find that when I attempt to emulate this within my program with multiple motors using a function, the program abruptly stops after the motors have gone 100 degrees.

void chassis_move(const double position, const int32_t velocity){
  motor_move_relative(LEFT_MOTOR, 100, velocity);
  motor_move_relative(RIGHT_MOTOR, 100, velocity);

  while (!(motor_get_position(LEFT_MOTOR) < (position + 5)) && (motor_get_position(LEFT_MOTOR) > (position - 5))) {
    delay(2);
  }
}

I guess it’s because I am telling multiple motors to run with the loop, but shouldn’t it all be the same if the motors are moving to the same position?

First off, I’m not sure that you ever told the motor encoders to report values in degrees. Click here for information on how to do that.

2 Likes

Yes, I have the following in initialize.c:

void initialize() {   
  motor_set_encoder_units(LEFT_MOTOR, E_MOTOR_ENCODER_DEGREES);
  motor_set_encoder_units(RIGHT_MOTOR, E_MOTOR_ENCODER_DEGREES);
}

So I think what is confusing you is the “relative” part of the move relative. If you want to check if you have reached the target you probably need

goalPosition = startPosition + motorPosition

Also your ! in the function doesn’t seem to do what you want. Might I suggest just to simplify matters

abs(goalPosition - motor_get_position(LEFT_MOTOR)) > 5

So taking your suggestions into account, I now have the following:

void chassis_move(const double position, const int32_t velocity){
  int goal_position = position + motor_get_position(LEFT_MOTOR));
  motor_move_relative(LEFT_MOTOR, 100, velocity);
  motor_move_relative(RIGHT_MOTOR, 100, velocity);
  while (abs(goal_position - motor_get_position(LEFT_MOTOR)) > 5){
    delay(2);
  }
}

Unfortunately, the result is still that all of the motors only go forward 100 degrees. Any other suggestions?

I mean… that’s what your code says to do

That’s telling the motor to move 100 units, whatever the motor units that you have set. You may want to change that line and the following line, to

motor_move_relative(LEFT_MOTOR, position, velocity);
motor_move_relative(RIGHT_MOTOR, position, velocity);

Okay, so I have applied the changes suggested by jcgrif, and the robot is now moving forward to the desired positition. However, after each movement, the robot stops abruptly, causing the back wheels to lift into the air and cause the robot to go slightly off course. I tried to use the delay function as well as set the speed to 0 with motor_move (which weirdly usually results in a softer braking), but the issue still occurs. Any advice?

motor_move_{relative, absolute, velocity} all follow the brake mode set for the motors.

motor_move{_voltage} do not respect the brake mode, which likely explains your observation of “softer braking.”

Since you are using motor_move_relative, aside from playing with different brake modes, you can try either setting a lower maximum velocity for the movement, or splitting the movement into a few parts that allow for a smoother deceleration near the end.

What is the default brake setting for motor_move_relative?