SonarSensor programming issue
Page 1 of 1

Author:  Wolpi35 [ Wed Mar 07, 2012 5:25 am ]
Post subject:  SonarSensor programming issue


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:

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;-))

#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];
  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;
  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

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

Thanks and regards


Author:  magicode [ Wed Mar 07, 2012 9:22 am ]
Post subject:  Re: SonarSensor programming issue

There is no reason for you to be using your own tasks here. Just replace the task with a function:
void SonorChk()

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.

Author:  Wolpi35 [ Sun Mar 18, 2012 10:08 am ]
Post subject:  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.



Author:  Wolpi35 [ Fri Apr 06, 2012 4:45 pm ]
Post subject:  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


Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group