Howdy y’all,
My team has recently looked into improving our autonomous programs by using threads and a color sorter, and I’ve been having trouble wrapping my head around threads. I’m using PROS 3.8.3, but i haven’t figured out threads properly. Any advice would be appreciated, as would any alternatives!
thanks,
Luke
16756A BRICCC
This is how my team writes tasks/threads in PROS. You’ll probably end up creating the task/thread in the autonomous()
section of the program.
The code that is run within the task is in its own thread. This thread is represented by the second pair of brackets in the task declaration. (i.e., all code that is inside of the {}
will be run in a thread.)
Task task_name{[=] {
//This is the new thread
function();
}};
The example above creates a thread that runs function()
. You can think of this function as a background process. Usually this function will look something like:
void function() {
while (true) { // loop
// do something like check the color sensor and react accordingly
pros::delay(10);
}
}
There are many different things you can do with tasks. BTW you don’t have to give the task a name . You could even tell the thread to delay before calling the function.
Task {[=] {pros::delay(time); function(); }};
I hope that helps!
Thank you, that helps tremendously. However, is it possible for the function/background process to interact with the main code while it runs?
for example:
intake.move_voltage(12000);
/*
color sort task running in background,
if blue is detected then conveyor will eject ring
*/
//theoretical function code here, only executes in function
while(true)
{
if(optical.color() == BLUE)
{
pros::delay(100);
intake.move_voltage(0);
pros::delay(100);
intake.move_voltage(12000);
}
}
i understand that the function would of course not be defined in the auton function, but would this type of interfacing be possible while the main program is executing?
Thanks in advance!
Yes, the thread can interact with the intake at the same time as the opcontrol & autonomous.
Please be careful when using two threads to control the intake.
Also please make sure that there is a delay in the while(ture) loop! If there is no delay the processor will be stuck in a loop, using most of its processing power. There is also no need to run the loop with a smaller delay than 10ms as none of the sensors can update quicker than every 10ms.
I personally just call a task that intakes/color sorts while a button is being pressed or for a certain period of time. For example in driver control:
if (master.get_digital_new_press(DIGITAL_R2))
{
// create a task that calls the color sorting function
}
The color sorting function that is called during driver control when the btn is pressed:
void color_sorting_driver() {
intake.move_voltage(1200);
while (master.get_digital(DIGITAL_R2)) // intake while the btn is held down
{
// color sorting (probably make it a function so it can be used later)
// delay to ensure the brain can manage other things
pros::delay(10);
}
intake.move_voltage(0);
}
I would then write a different function to manage the color sorting during autonomous. You could then call this function in a Task during the autonomous period.
void color_sort_for(uint32_t intake_duration) {
intake.move_voltage(1200);
uint32_t start_clk = pros::millis();
while (pros::millis() - start_clk < intake_duration)
{
// color sorting (probably the same function as is used in driver control)
// delay to ensure the brain can manage other things
pros::delay(10);
}
intake.move_voltage(0);
}
This is just one of the ways to do color sorting. There is probably a better way.
I don’t believe that optical.get_color()
exists in PROS. Instead you would use optical.get_heu()
PROS optical sensor documentation.
I hope that answered your question.
JB 2393S Valor
2 Likes
Is there a way to make this work for the JAR Template? We’ve played around a little bit, and can’t figure it out.