Is this Autonomous code correct?


#1
void autonomous( void ) {
while(Sonar1.distance(distanceUnits::in) <8) 
{
Arm1.spin(vex::directionType::fwd,200, vex::velocityUnits::pct);


}
while(Sonar1.distance(distanceUnits::in)>8)
{
Frontleftmotor.spin(vex::directionType::fwd,200,vex::velocityUnits::pct);
Backleftmotor.spin(vex::directionType::fwd,200, vex::velocityUnits::pct);

Backrightmotor.spin(vex::directionType::fwd,200, vex::velocityUnits::pct);
Frontrightmotor.spin(vex::directionType::fwd,200, vex::velocityUnits::pct);
}
Frontleftmotor.stop();
Frontrightmotor.stop(); 
Backleftmotor.stop();
Backrightmotor.stop();



#2

I need help with it


#3

The only problem I see is that you are trying to use 200% power for your motors. Are you trying to use pct units(-100 to 100) or RPM units(-200 to 200)? Did you test it on your robot? What is it suppose to do?


#4

I thought 200 was the max amount of power you can have.

So if a cap is less than 8 inches then it needs to move forward and flip it. Do you think it will work? Other people say it won’t.


#5

When you use the pct units (vex::velocityUnits::pct), the range is from -100 to +100. The range for using RPM is dependent on what gear config your motors have (-MaxRPM to +MaxRPM). Where is the sonar attached? The code tells the arm to raise for as long as the sonar reads less than 8 inches. Then it move forward for as long as the sonar reads more than 8 inches.


#6

Yes that is what I meant. With 200, all of the motors on my robot worked.


#7

I think you need to switch the order of the two while loops. I’m not sure why the flip is before the drive.


#8

The only downside with the sonars is that they have a cone shaped measuring to them which adds error to their readings. For my team, we use the encoders to move forward a certain distance and then we flip the cap with our flipper.
It would look something like this.


void autonomous( void )
{
  Frontleftmotor.rotateFor(1500,vex::rotationUnits::deg,200,vex::velocityUnits::rpm,false); 
  Backleftmotor.rotateFor(1500,vex::rotationUnits::deg,200,vex::velocityUnits::rpm,false); 
  Backrightmotor.rotateFor(1500,vex::rotationUnits::deg,200,vex::velocityUnits::rpm,false);
  Frontrightmotor.rotateFor(1500,vex::rotationUnits::deg,200,vex::velocityUnits::rpm);

  Arm1.rotateFor(500,vex::rotationUnits::deg,200, vex::velocityUnits::rpm);
}


#9

Nice. I was also trying to do something similar to this but i was not able to figure out the rotation Units and all that.


#10
void autonomous( void ) {
while(Sonar1.distance(distanceUnits::in)>8)
{
Frontleftmotor.spin(vex::directionType::fwd,200,vex::velocityUnits::pct);
Backleftmotor.spin(vex::directionType::fwd,200, vex::velocityUnits::pct);

Backrightmotor.spin(vex::directionType::fwd,200, vex::velocityUnits::pct);
Frontrightmotor.spin(vex::directionType::fwd,200, vex::velocityUnits::pct);
}



while(Sonar1.distance(distanceUnits::in) <8) 
{
Arm1.spin(vex::directionType::fwd,200, vex::velocityUnits::pct);


}


Frontleftmotor.stop();
Frontrightmotor.stop(); 
Backleftmotor.stop();
Backrightmotor.stop();

Would this be correct then


#11

I still recommend using only encoders, but if you want to use a sonar then it would look like this:


void autonomous( void )
{
  while(Sonar1.distance(distanceUnits::in)>8)
  {
    Frontleftmotor.spin(vex::directionType::fwd,200,vex::velocityUnits::rpm);
    Backleftmotor.spin(vex::directionType::fwd,200, vex::velocityUnits::rpm);
    Backrightmotor.spin(vex::directionType::fwd,200, vex::velocityUnits::rpm);
    Frontrightmotor.spin(vex::directionType::fwd,200, vex::velocityUnits::rpm);
  }
  Frontleftmotor.stop(vex::brakeType::coast);
  Backleftmotor.stop(vex::brakeType::coast);
  Backrightmotor.stop(vex::brakeType::coast);
  Frontrightmotor.stop(vex::brakeType::coast);

  while(Sonar1.distance(distanceUnits::in) <8) 
  {
    Arm1.spin(vex::directionType::fwd,200, vex::velocityUnits::rpm);
  }
  Arm1.stop(vex::brakeType::coast);
}

The “vex::brakeType::coast” param locks the motor in place when stopped. This will move forward and when the sonar detects less than 8 inches, the arm move up and the base stops.


#12

No, it doesn’t. It allows motor’s axle holder (and whichever inner parts are need to accomplish this) to spin nearly freely. Most likely it disconnects the wires on the coils from anything else, which would mean it’s exactly the opposite of locking the motor in place. The result of using vex::brakeType::coast is that the robot will continue rolling until friction stops it, which probably won’t be so far.

vex::brakeType::brake tries to slow down the rotation to reach a stop but doesn’t try to specifically hold a position.

vex::brakeType::hold tries to keep the motor locked in place. I suspect you were suggesting this.


#13

Oh yea sorry, I got mixed up. :stuck_out_tongue: