View unanswered posts | View active topics It is currently Thu Sep 18, 2014 12:41 am






Reply to topic  [ 9 posts ] 
Robot Sumo Code Help? 
Author Message
Rookie

Joined: Mon Jun 03, 2013 1:43 pm
Posts: 4
Post Robot Sumo Code Help?
Hello!

I'm working on a program for a class assignment which will let the robot play Robot Sumo. Basically, two robots get put on a 4' white circle that has a black border. The goal is to push the opponent off the circle.

Some of the criteria:
-The program must use multitasking
-The touch sensor can't be activated for more than five continuous seconds at a time (ramming the opponent is bad, I guess)
-The maximum amount of time the robots can be in a standstill collision is five seconds

Here's my code so far:

Code:
#pragma config(Sensor, S1,     touch,          sensorTouch)
#pragma config(Sensor, S2,     sonar,          sensorSONAR)
#pragma config(Sensor, S3,     light,          sensorLightActive)
#pragma config(Motor,  motorA,          left,          tmotorNormal, PIDControl, encoder)
#pragma config(Motor,  motorB,          right,         tmotorNormal, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

int Calibrate()                 
{
  int threshold;
  threshold = SensorValue[light]*.8;
  return threshold;
}

task lookForLine()           
{
  int thresh = Calibrate();                         //Calibrates the light sensor to find the black border
  while(true)
  {
    if (SensorValue[light]<thresh)
    {
      motor[left] = -50;
      motor[right] = -50;
      wait10Msec(200);
    }
      wait1Msec(10);
  }
}

task searchForOpponent()
{
  while(true)
  {
    if (SensorValue[sonar]<50)
    {
      motor[left] = 50;
      motor[right] = 50;
    }

    else
    {
      motor[left] = 50;                                      //Turns in place while it searches for the opponent
      motor[right] = -50;
     }
     wait1Msec(10);
    }
  }


task touchTimer()
{
   while(true)
   {
      ClearTimer(T1);
      while (SensorValue[touch] == 1)
      {
         if (time10(T1)>500)
         {
            StopTask(lookForLine);
            StopTask(searchForOpponent);
            motor[left] = -50;
            motor[right] = -50;
            wait10Msec(10);
            StartTask(lookForLine);
            StartTask(searchForOpponent);
            ClearTimer(T1);
         }
       }
         wait1Msec(10);
      }
}
 
task main()
{
  while(true)
  {
    StartTask(lookForLine);
    StartTask(searchForOpponent);
    StartTask(touchTimer);
    wait1Msec(10);
  }
}


Whenever I try to run this code the robot just moves forward indefinitely no matter what else I do. I've never done multitasking before so I think that's probably where I messed up.

Can anyone spot the stuff I did wrong?

Thank you!


Mon Jun 03, 2013 1:54 pm
Profile
Senior Roboticist
User avatar

Joined: Wed Sep 28, 2011 10:13 pm
Posts: 630
Location: If I told you, I'd have to kill you.
Post Re: Robot Sumo Code Help?
And why, exactly, are such easy goals being overly complicated with multitasking? This doesn't need multitasking. All these tasks will be conflicting with one another, controlling the motors as they are. This could be done much, much more cleanly with an infinite loop in just the one main task. I myself haven't had much experience with multitasking, because I've never had the need to use it extensively. :P I'd ask your teacher why in the world would he require such a simple task to be so much more complicated by involving multitasking.

Rule #1 of programming with RobotC: Don't multitask unless it's absolutely necessary. viewtopic.php?f=1&t=3342

_________________
A.K.A. inxt-generation
Self-proclaimed genius, and future world dominator.
My Brickshelf Folder
"Don't they teach recreational mathematics anymore?" - The Tenth Doctor
Bow down to Nikola Tesla, King of the Geek Gods.


Mon Jun 03, 2013 2:08 pm
Profile WWW
Rookie

Joined: Mon Jun 03, 2013 1:43 pm
Posts: 4
Post Re: Robot Sumo Code Help?
I read the sticky and yes, I understand that multitasking is overcomplicating the program, but it's part of the criteria for a school assignment. If I don't multitask I'll lose points, so unfortunately I don't really have a choice.


Mon Jun 03, 2013 2:12 pm
Profile
Senior Roboticist
User avatar

Joined: Wed Sep 28, 2011 10:13 pm
Posts: 630
Location: If I told you, I'd have to kill you.
Post Re: Robot Sumo Code Help?
Would you lose points if you just had two tasks?

_________________
A.K.A. inxt-generation
Self-proclaimed genius, and future world dominator.
My Brickshelf Folder
"Don't they teach recreational mathematics anymore?" - The Tenth Doctor
Bow down to Nikola Tesla, King of the Geek Gods.


Mon Jun 03, 2013 2:22 pm
Profile WWW
Rookie

Joined: Mon Jun 03, 2013 1:43 pm
Posts: 4
Post Re: Robot Sumo Code Help?
I think the idea is to have every process be its own task, but I don't think I would lose points.


Mon Jun 03, 2013 2:28 pm
Profile
Senior Roboticist
User avatar

Joined: Wed Sep 28, 2011 10:13 pm
Posts: 630
Location: If I told you, I'd have to kill you.
Post Re: Robot Sumo Code Help?
Well having every process have a task is very difficult, it'd be bread and butter to just have the main task an another separate task just doing everything. :wink: So decide what you want to do, and then try it again.

_________________
A.K.A. inxt-generation
Self-proclaimed genius, and future world dominator.
My Brickshelf Folder
"Don't they teach recreational mathematics anymore?" - The Tenth Doctor
Bow down to Nikola Tesla, King of the Geek Gods.


Mon Jun 03, 2013 2:33 pm
Profile WWW
Rookie

Joined: Fri Mar 11, 2011 9:30 pm
Posts: 12
Post Re: Robot Sumo Code Help?
We do a lot of multi-tasking in our FTC code.

A typical multi-threaded autonomous program would have
- 1 thread which reads sensors (and populates global variables) so all the sensor reading code is in one loop.
- 1 task that displays stuff, autonomous mission number, delay timer, sensor values, other debugging info, etc. to the screen
- 1 task that applies some time based error correction to the gyro (this task only started if significant drift is taking place)
- 1 task, the main thread, that based on sensor readings and mission selected, would actually do the autonomous code, i.e. this would be your main loop evaluating conditions and combinations of conditions (reads global variables from other tasks as inputs) and taking action.

we would optionally spin off threads that would do things like, move a manipulator out and in while the main task does something with the drive train.

we like this style of programming because we can divide up the code into logical chunks and work on them pretty much independently.

_________________
Jamie Diamond
Cougar Robotics Team (Ring It Up!, Bowled Over!, Get Over It! #4251)
(Smart Moves #127, Climate Connections #437, Power Puzzle #1470)
coach@cougarrobot.com
http://www.cougarrobot.com
https://www.facebook.com/CougarRoboticsTeam


Mon Jun 03, 2013 2:52 pm
Profile
Rookie

Joined: Mon Jun 03, 2013 1:43 pm
Posts: 4
Post Re: Robot Sumo Code Help?
Thanks for the help! I took your advice and consolidated my program into two tasks. I'm still having the same problem, though... From debugging it seems like my non-main task won't run at all. Even when I mess with the sensors (hold down the touch sensor for an extended period, put black paper in front of the robot, etc.) it doesn't seem like the global variables that hold these values are getting changed by the other task. Any ideas on why this is the case?

This is my new code:

Code:
#pragma config(Sensor, S1,     touch,          sensorTouch)
#pragma config(Sensor, S2,     sonar,          sensorSONAR)
#pragma config(Sensor, S3,     light,          sensorLightActive)
#pragma config(Motor,  motorA,          left,          tmotorNormal, PIDControl, encoder)
#pragma config(Motor,  motorB,          right,         tmotorNormal, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

bool opponentSeen;
bool touched;
bool onBlack;


int Calibrate()
{
  int threshold;
  threshold = SensorValue[light]*.7;
  return threshold;
}

int thresh = Calibrate();

task readSensors()
{
  while(true)
  {
     if (SensorValue[light]<thresh)
     {
       onBlack = true;
     }
     else
     {
       onBlack = false;
     }
     if (SensorValue[sonar]<50)
     {
       opponentSeen = true;
     }
     else
     {
       opponentSeen = false;
     }
     if (SensorValue[touch]==1)
     {
       touched = true;
     }
     else
     {
       touched = false;
     }
     wait10Msec(10);
   }
}

task main()
{
 while(true)
 {
   StartTask(readSensors);
   if (onBlack)
   {
     motor[left] = -100;
     motor[right] = -100;
     wait10Msec(100);
   }
   while (opponentSeen)
   {
     motor[left] = 100;
     motor[right] = 100;
   }
   while (touched)
   {
     ClearTimer(T1);
     while (time1(T1)<=5000)
     {
       motor[left] = 100;
       motor[right] = 100;
     }
     if (time1(T1)>5000)
     {
       motor[left] = -100;
       motor[right] = -100;
       wait1Msec(1000);
       ClearTimer(T1);
     }
   }
   while (onBlack!=true && opponentSeen!=true && touched!=true)
   {
     motor[left] = 100;
     motor[right] = 75;
     wait10Msec(10);
   }
   wait10Msec(10);
 }
}


Mon Jun 03, 2013 5:34 pm
Profile
Rookie

Joined: Fri Mar 11, 2011 9:30 pm
Posts: 12
Post Re: Robot Sumo Code Help?
Only had time for a quick look at your code so far, but it appears you are starting the readSensors task over and over... Start the task once and let it go.

Using all bools I think you will be safe but depending on what kind of global variables you use you may need a hogCPU() and a releaseCPU()

Then I recommend you start debugging this thing one loop or task at a time. First, run just the sensor task, run it, all by itself and monitor the variables in the debugger, or by displaying them to the screen, or by using the debug stream (we tend to like using the debug stream). Trip your different sensors by hand and watch the variables change value.

Then run just the big loop in the main all by itself. Populate the global variables by hardcoding values in a separate initialization procedure. See if the main does what you want based on different combinations of variables.

Then, when both are working separately combine them.

_________________
Jamie Diamond
Cougar Robotics Team (Ring It Up!, Bowled Over!, Get Over It! #4251)
(Smart Moves #127, Climate Connections #437, Power Puzzle #1470)
coach@cougarrobot.com
http://www.cougarrobot.com
https://www.facebook.com/CougarRoboticsTeam


Mon Jun 03, 2013 10:03 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 9 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:  



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