View unanswered posts | View active topics It is currently Fri Mar 22, 2019 12:44 am






Reply to topic  [ 11 posts ] 
EV3 Multi Tasking Issues, help needed!!! 
Author Message
Rookie

Joined: Wed Feb 11, 2015 12:58 pm
Posts: 6
Post EV3 Multi Tasking Issues, help needed!!!
I have been writing some behaviour control, navigation code which I have taken some from a project I did in NXT which worked. The model I'm writing for is the EV3 using RC 10.27.
This code is using multi tasking as each task runs the sensors, behaviours and motors, but for some reason this code will run the sensor/ behaviour tasks but not the motors task. The motor task has been tested by itself and works but not within this code structure, it doesn't even hit a break point in the task.
The only way this code work is when everything is converted to functions but I'd rather be able to multi task as I need realtime data from the sensors for navigation.
I'm at my wits end and cannot see why it will not work and there are no errors??? Any help would be appreciated. :?:

Code:
#pragma config(Sensor, S2,     gyro2,          sensorEV3_Gyro)
#pragma config(Sensor, S3,     colour3,        sensorEV3_Color, modeEV3Color_Color)
#pragma config(Sensor, S4,     sonar4,         sensorEV3_Ultrasonic)
#pragma config(Motor,  motorB,          motorLeft,     tmotorEV3_Large, PIDControl, encoder)
#pragma config(Motor,  motorC,          motorRight,    tmotorEV3_Large, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

//GLOBALS
#define HALT 0
#define FORWARD 1
#define REVERSE 2
#define LTURN 3
#define RTURN 4

const int TICK = 500; //ms
const int PROX = 15; //cm

//Motor speed variables
const int FSPEED = 50;
const int RSPEED = 30;
const int TSPEED = 20;


int motor_input = HALT;

//void motors();

task motors()
{// This task controls movement
   while(true)
   {
      if(motor_input == HALT)
      {
         setMotorSpeed(motorLeft, 0);
         setMotorSpeed(motorRight, 0);

      }

      else if(motor_input == FORWARD)
      {
         //straight line code
         setMotorSyncEncoder(motorLeft, motorRight,0, 720, FSPEED);
      }

      else if(motor_input == LTURN)
      {
         setMotorReversed(motorLeft, true);
         setMotorSpeed(motorLeft, TSPEED);
         setMotorSpeed(motorRight, TSPEED);

         return;
      }

      else if(motor_input == RTURN)
      {
         setMotorSpeed(motorLeft, TSPEED);
         setMotorReversed(motorRight, true);
         setMotorSpeed(motorRight, TSPEED);

         return;
      }

      else if(motor_input == REVERSE)
      {
         setMotorReversed(motorLeft, true);
         setMotorReversed(motorRight, true);

         setMotorSpeed(motorLeft, RSPEED);
         setMotorSpeed(motorRight, RSPEED);

      }

   }
}


task cruise()
{
   while (true)
   {
      if(currDistance > PROX)
      {
         motor_input = FORWARD;
      }
      else
         setMotorSpeed(motorLeft, 0);
      setMotorSpeed(motorRight, 0);

   }
}

task main()
{

   startTask(cruise);
   startTask(motors);

}


Wed Feb 11, 2015 1:22 pm
Profile
Expert

Joined: Thu Dec 01, 2011 12:07 am
Posts: 151
Post Re: EV3 Multi Tasking Issues, help needed!!!
Add braces {} around the 2 lines after the 'else' statement in the 'cruise' task.


Wed Feb 11, 2015 1:38 pm
Profile
Rookie

Joined: Wed Feb 11, 2015 12:58 pm
Posts: 6
Post Re: EV3 Multi Tasking Issues, help needed!!!
I've tried that but no difference, I can remove the if statement all together and the code still doesnt work, only if I manually set motor_input from the main does it work and even thats stops working after I compile this version of the code.


Wed Feb 11, 2015 3:53 pm
Profile
Expert

Joined: Thu Dec 01, 2011 12:07 am
Posts: 151
Post Re: EV3 Multi Tasking Issues, help needed!!!
The program may never be executing the second task. Add a wait1Msec(10); to the main loop for each task to allow time for the other task to run.
currDistance does not appear to be defined anywhere as well.
You may also need a loop in the main task after the other tasks start.


Wed Feb 11, 2015 11:40 pm
Profile
Rookie

Joined: Wed Feb 11, 2015 12:58 pm
Posts: 6
Post Re: EV3 Multi Tasking Issues, help needed!!!
i've tried it and it doesn't make a difference I think theres something wrong with my ide as other multi task code doesn't appear to be working correctly either


Sun Feb 22, 2015 8:40 am
Profile
Site Admin
Site Admin
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
Post Re: EV3 Multi Tasking Issues, help needed!!!
It doesn't work because you have two tasks trying to control the motor. Why do you set the motor speeds in the cruise task? Isn't that what the HALT command is for to be assigned to motor_input ?

= Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Sun Feb 22, 2015 10:19 am
Profile WWW
Rookie

Joined: Wed Feb 11, 2015 12:58 pm
Posts: 6
Post Re: EV3 Multi Tasking Issues, help needed!!!
Yes I understand what your saying and I have already tried the code without the if statement in the cruise task, where cruise just sets the motor input to forward and it still does not work, the problem is the motor task is not even being run at all no matter what the cruise task says


Sun Feb 22, 2015 11:24 am
Profile
Site Admin
Site Admin
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
Post Re: EV3 Multi Tasking Issues, help needed!!!
ok, this little program works, and will show you what you need to do with your main task:


Code:
task beep()
{
  while (true)
  {
    playTone(500, 10);
    sleep(1500);
  }
}

task blink()
{
  while(true)
  {
    setLEDColor(ledGreen);
    sleep(200);
    setLEDColor(ledRed);
    sleep(200);
  }
}

task main()
{
  startTask(beep);
  startTask(blink);

  // You need a sleep here, the program will exit when main exits.
  while(true)
  {
    sleep(100);
  }
}

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Sun Feb 22, 2015 12:44 pm
Profile WWW
Rookie

Joined: Wed Feb 11, 2015 12:58 pm
Posts: 6
Post Re: EV3 Multi Tasking Issues, help needed!!!
thankyou, I will give it a try


Sun Feb 22, 2015 12:53 pm
Profile
Rookie

Joined: Wed Feb 11, 2015 12:58 pm
Posts: 6
Post Re: EV3 Multi Tasking Issues, help needed!!!
ok I have finally got it working and found the problem, it appears there needs to be a while true loop in the main task (even a empty one works!), possibly because it's a task and not it function, so have added a while true after all the tasks and it now runs, when plugged into the computer only but it's progress. Its interesting theres no documention about the while loop needing to be there, its in the example but doesnt say it's neccessary to run multiple tasks. Thanks for the help guys.


Sun Feb 22, 2015 4:19 pm
Profile
Site Admin
Site Admin
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
Post Re: EV3 Multi Tasking Issues, help needed!!!
When the main task exits, the program exits. This is fairly common on all programming platforms. I put the loop in my example program, so you would see that you needed the loop.

= Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Sun Feb 22, 2015 4:22 pm
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 11 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.