Using a color sensor to control a motor

Hi, I am using the REV Color Sensor V3 that came in this years kit of parts. I’m using the example code that was in the sensors documentation. I am trying to get the motor to stop when the sensor senses blue, but I think the code gets stuck in the while loop because when I press the button to execute this code the motor never stops. The updateColor() method just updates the color sensed by the sensor. The SmartDashboard is updating what color the sensor is seeing, but the motor never stops when it sees blue.

Do you have any suggestions on what is happening?

Thank you,
Rachel Kinnamon team 3465

Don’t use open-ended while loops in periodic functions. You block all the rest of the code from running. Instead save the information to need to keep over a longer period of time in a state variable at class level.

2 Likes

So, do you think I should use a for loop instead of the while loop?

No, err… unless you have a counter (@Peter_Johnson is that what you mean?) he means don’t loop at all. In periodic() it loops 50 hz, about . 002 seconds.

Also, how I recommend you do this is (This is for command based but bear with me) have the end condition for you motor running to be if some method of you color choose returns true. Since you are doing this iteratvley you can check if the color your seeing is the color you want and return true. Then when you call you motor on the next iteration have it end if the color book is true

I have no idea why that example code uses a while loop…

The TeleopPerodic function is already being called in a loop. The segment of code should be more like…

updatecolor(); 

if(controller2.getRawButton(1) && colorString != "Blue")
    m_spinnerMotor.set(-1.0);
else
   m_spinnerMotor.stopMotor();
3 Likes

It doesn’t. It was added later by OP.

@rachelcinn, @JohnFogarty and @Peter_Johnson have some good advice. teleopPeriodic() gets called over and over again, so you can think of it as your loop.

2 Likes

I agree with what the previous commentors have said. TeleopPeriodic runs every 20 milliseconds, so you don’t need a loop in the code. Replacing it with a if statement should solve your issue.

To the specific issue you mentioend

TeleopPeriodic forms the guts of what’s called “Superloop” architecture in embedded software.

The key to keep in mind is that programming for a robot requires a slightly different mentality than “usual” desktop PC programs. Inside of teleop periodic, teams get to provide a list of things to do every 20ms.

The while loop in your code currently says “Every 20ms, stop doing anything else until we detect blue” . What I’m strongly suspecting you actually want is “Every 20ms, if we see blue, stop. Otherwise, spin”.

Broader Discussion

The following is some psuedocode of how the robot is using the code you’ve written. It’s definitely inaccurate, but might be useful in forming a mental model of howteleopPeriodic() gets used.

public static void main(){

    robotInit();

    while(true){ //The proverbial "super loop"
        readInfoFromDS();

        if(dsSaysWeAreInTeleop()){
            teleopPeriodic();
        } 

        // Other else-if's for other modes here

        sendBatteryVoltageValueToDS();

        delayFor20ms();
    }

}

The key - the robot will already call teleopPeriodic inside of a loop. teleopPeriodic can’t “block” - it has to “do its thing” fast and get out of the way, because there’s other things the robot needs to do.

Note that this is just one solution to a much broader problem in computer science. The problem, simply stated, is “What do you do when you have fewer processors than things you want to calculate?”. Operating systems, like windows and Linux, have very specific and complex solutions to let multiple programs run “at once”. For embedded software, like a robot, we have the opportunity to be a bit more precise as to which pieces of code run when.

There are programming mechanisms called “threads” which can be used to allow pieces of code to run simultaneously (or, nearly simultaneously). However, I’d recommend staying away from these if you can. It’s easy to get code running in parallel, but gets very hard, very quickly, to have those chunks of code pass data back and forth.

2 Likes

Thank you for your responses! I finally got the code to work!

2 Likes

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.