Aligning Against Walls

Hello! I’m currently working on my team’s skills program. We align to walls multiple times in the program, and we use a limit switch so that when it’s pressed, we stop our drive motors and continue the program. However on occasion we hit the wall the wrong way, and are unable to complete the action. How can I make it time out after a time (say one second)? This is the original void

void align(int speed) {
stopAllMotors();
while(!Align.pressing()) {
LeftDrive1.spin(directionType::rev, speed ,velocityUnits::pct);
LeftDrive2.spin(directionType::rev, speed ,velocityUnits::pct);
RightDrive1.spin(directionType::rev, speed ,velocityUnits::pct);
RightDrive2.spin(directionType::rev, speed ,velocityUnits::pct);
}
}

This is the timeout ability I tried. Motor.setTimeout(); was not working, nor was this:

void align(int speed, int time, int timepassed = 0) {
stopAllMotors();
while(!Align.pressing()) {
while(time > timepassed) {
LeftDrive1.spin(directionType::rev, speed ,velocityUnits::pct);
LeftDrive2.spin(directionType::rev, speed ,velocityUnits::pct);
RightDrive1.spin(directionType::rev, speed ,velocityUnits::pct);
RightDrive2.spin(directionType::rev, speed ,velocityUnits::pct);
}
stopAllMotors();
task::yield();
}
}

I don’t think the timeout works with the spin method. Try it with startRotateFor for some distance that you would know would be far too long to be achievable.

For the second example, it looks like there is nothing to increment timepassed, so it will always be 0, and never get out of the while loop.

Also, you may also want to add a sleep inside the loop so this function doesn’t hog the CPU and cause other weirdness.

My code is a little bit extra, but I do that for skills also. Here’s my code:

void skillsbutton(int speed=100, int sleeptime=500, int watchdog=4000){
    Brain.resetTimer();
    while(TouchSensor.pressing()==0 && Brain.timer(msec)<(watchdog - sleeptime) ){
    FrontLeft.spin(forward,speed,percent);
    BackLeft.spin(forward,speed,percent);    
    FrontRight.spin(forward,speed,percent);
    BackRight.spin(forward,speed,percent);  }
    FrontLeft.stop();
    BackLeft.stop(); 
    FrontRight.stop();
    BackRight.stop(); 
    task::sleep(sleeptime);
}

The brain has a built in timer, so I rest the timer, and just tell the motors to spin until either the button is pressed, or until a certain amount of time passes. I reset a timer before running the program, and that way, it is guaranteed that the function will end at at most 4 seconds. Tell me if you have any questions. You seemed to have the right idea, but you never actually counted up with the timepassed the first time.

Thank you both! I figured out a way to program it though:

void align(int speed, int time) {
int timepassed=0;
stopAllMotors();
while(!Align.pressing() && (timepassed<time)) {
while(time > timepassed) {
LeftDrive1.spin(directionType::rev, speed ,velocityUnits::pct);
LeftDrive2.spin(directionType::rev, speed ,velocityUnits::pct);
RightDrive1.spin(directionType::rev, speed ,velocityUnits::pct);
RightDrive2.spin(directionType::rev, speed ,velocityUnits::pct);
task::sleep(2000);
timepassed++;
}
stopAllMotors();
} }

Alright, so for a while I was doing the same thing until I realized that using a time function instead of encoders is perfect for aligning with the walls.

1 Like

You could put one limit switch on each side of your robot and only drive the side(s) that haven’t been triggerd

We did some moves to align with the wall a couple times in our autonomous programs as well. The girls talked about adding a switch but it never got done. One thing we did experience is that we often got out of alignment or slightly climbed the wall when backing into it. Because of this, their exit from the wall sometimes was out of alignment. They found that this was minimized when the sequence of turning on and off motors was changed.

The girls had a 6 motor drive on a 4 wheel base, thus the code, for whatever reason, was written to turn on all 3 left motors, then all 3 right motors. When this happened, the robot would start one side SLIGHTLY before the other. Sometimes it worked good, sometimes it didn’t. The code was changed so that a left and right pair was turned on together and this seemed to help.

You could also turn on multiple motors with one command too - something like:
motor[1] = motor[2] = motorspeed;

(my apologies - I don’t have robotC in front of me right now, nor do I know if VCS can do the same thing)

2 Likes

If you had a gyro on your bot you could use the wall to re callebrate it and then use the gyro to drive streight