View unanswered posts | View active topics It is currently Sat Nov 01, 2014 7:06 am






Reply to topic  [ 4 posts ] 
SonarSensor programming issue 
Author Message
Rookie

Joined: Tue Oct 04, 2011 6:22 am
Posts: 7
Post SonarSensor programming issue
All,

after I started a while ago to program my Lego Mindstorms motors I want continue now with programming the sensors. Using the examples at the IDE help the compilation worked with no errors but if I want download the executable to the robot and start it I always get an error:

Quote:
Byte Code Interpreter Exception:

Program Slot: 0, Task ID: main[0]
Error at PC: Task main + 0x007D
TaskPC: Task: main+0077
TaskState: 'Exception'
Exception Type: 'Task Number out of range(23)'


I don't know what is wrong. See below my code (sorry for the german remarks;-))

Code:
#pragma config(Sensor, S4,     sonarSensor,         sensorSONAR)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

/* Deklarationen */

int int_sonorval = 0;

/* Task zur Ermittlung des vom Sonar gemessenen Wertes */
task SonorChk()
{
  int_sonorval = SensorValue[sonarSensor];
  eraseDisplay();
  nxtDisplayString (4," Gemessene Distanz:");
  nxtDisplayString (5,"");
  nxtDisplayString (6," %i cm", int_sonorval);
}

task main()
{
/* Hauptschleife - nach 1 Minute Programm auf alle Faelle beenden */
  int int_now;
  int int_end;
  ClearTimer(T1);
  int_now = time10[T1];
  int_end = int_now + 7000;
  while(int_now < int_end) {
    motor[motorC] = 50;
    motor[motorA] = 50;
/*    int_now = nClockMinutes;*/
    int_now = time10[T1];
    StartTask(SonorChk, 1);
    if (int_sonorval < 5 ) // Wenn die Distanz zu einem Objekt < 5 cm dann Stop
    {
      motor[motorC] = 0;
      motor[motorA] = 0;
      int_now = int_now + 7000; // while-Schleife nicht nochmal durchlaufen
    }
  }
  StopAllTasks();
}


Can you tell me where I think maybe wrong and what is the issue here?

Thanks and regards

Stefan


Wed Mar 07, 2012 5:25 am
Profile
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post Re: SonarSensor programming issue
There is no reason for you to be using your own tasks here. Just replace the task with a function:
Code:
void SonorChk()
{
//code
}

and use just "SonarChk();" in your main task instead of using StartTask. Also, you do not need StopAllTasks at the end of your program, as it will end when the main task ends. You should look at the sticky thread on multitasking to learn when and when not to use tasks.

_________________
sudo rm -rf /


Wed Mar 07, 2012 9:22 am
Profile
Rookie

Joined: Tue Oct 04, 2011 6:22 am
Posts: 7
Post Re: SonarSensor programming issue
Hi and thanks for this, it worked. Just it is shown now always a distance of 250 cm even I hold my hand in front. What I wanted is that the loop check always the distance and if it get less than lets say 10 cm the robot should stop.

By the way the examples - that with the task too - I had from the function descriptions which is included in the RobotC IDE.

Cheers

Stefan


Sun Mar 18, 2012 10:08 am
Profile
Rookie

Joined: Tue Oct 04, 2011 6:22 am
Posts: 7
Post Re: SonarSensor programming issue
Hi guys,

got it! First I did a mistake and connected the sensor cable wrong. Then I found out that in the while loop the 5 cm I used is to less. I changed it to 20 cm and it works really cool.

\w/ Rock on guys

Stefan


Fri Apr 06, 2012 4:45 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 4 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.