View unanswered posts | View active topics It is currently Thu Apr 17, 2014 9:20 pm






Reply to topic  [ 7 posts ] 
Curious problem with sonar sensor 
Author Message
Rookie

Joined: Wed Jul 23, 2008 12:48 am
Posts: 19
Location: Germany
Post Curious problem with sonar sensor
Hello,
first of all I'm new to RobotC, although I'm not not new to C-programming/programming in general. I really like most of this language so far and I'm thinking of purchasing it. Up to now I was using RobotC 1.10 and yesterday for testing also 1.40 public RC1, both under Vista. Irrespective of the version I discovered a strange bug which made me almost crazy. Here is the problem:

I was writing my first program for my first self-build tribot-like robot. So far I managed to let him travel and rotate and then I wanted him to measure also the distance via the sonar sensor. After checking the examples I included the appropriate code into my program. Unfortunately it didn't read any useful values from the sensor. Hence I first tried the sample program itself and found it was running like a charm (Displaying Sonar Value to LCD.c). Because I was not able to find the mistake, I put the sample code into my program. This way only the sample code was executed, because of the infinite loop in it. But my program was still generating wrong values. In particular it is generating values between 900 and something above 1000, without showing real distance dependence. When disconnecting the sensor it says something about 100. Then I started to comment out everything which was not needed. But this didn't work. So I deleted almost everything in the code to make it equal with the sample program, but this also didn't change the situation. Then I deleted also all comments from both programs. When compiling and downloading the sample code, suddenly the following code appeared in the beginning of the program:
Code:
//*!!Sensor,    S4,          sonarSensor, sensorSONAR,      ,                    !!*//
//*!!                                                                            !!*//
//*!!Start automatically generated configuration code.                           !!*//


After deleting these lines and compiling/downloading again, another similar line appeared. So far the code was working. After deleting these lines again it showed the same strange behavior as found for my own program. In the end I'm really confused now. Has anybody any clue what is happening here? If there is any way to attach files, I would like to attach my program files, since I don't think posting them makes sense, because both programs are in principle the same. Anyway, here is the code:

The original program (not working):
Code:
//const int one_turn = 700; // round about 700 degree motor rotation for one complete turn
//const TSynchedMotors syncMotors = synchBC; // synchronized motors
//const tMotor primM = motorB; // primary motor
const tSensors sonarSensor = (tSensors) S4; // port of sonar sensor

//int dist_min = 300; // contains minimum distance measured
//int pos_min = 10000; // position of minimum


// search for minimum distance within about one full turn
//void search_min360()
//{
//   int x;

//  nMotorEncoder[primM] = 0;
//  nSyncedTurnRatio = -100;
  //nMotorEncoderTarget[primM] = one_turn;
//  motor[motorB] = 10;
//  while (nMotorEncoder[primM] < one_turn)
//  {
//     x= SensorValue[sonarS];
//     nxtDisplayTextLine(1,"Sonar:%d   ",x);
//     if (x < dist_min)
//     {
//        dist_min = x;
//        pos_min = nMotorEncoder[primM];
//        PlaySound(soundShortBlip);
//     }
//     wait1Msec(1000);
//  }
//  motor[primM] = 0;
//  PlaySound(soundBeepBeep);
//  wait1Msec(1000);
//}

// test routine to drive straight forward and backward
//void drive_straight()
//{
//   nSyncedTurnRatio = 100;
   //nMotorPIDSpeedCtrl[primM] = mtrEncoderReg;
//   motor[motorB] = 20;
//   wait1Msec(4000);
//   motor[motorB] = -20;
//   wait1Msec(4000);
//   motor[motorB] = 0;
//}

task main()
{
   int x=0;                                         //the variable "x" is declared as an integer, and initialized to equal zero

   while(true)                                      //an infinite while loop is delcared with true as its condition
   {
      x = SensorValue(sonarSensor);                 //the variable "x" is set to take the value of the sonar sensor
      nxtDisplayTextLine(1,"Sonar Reading:%d",x);   //"Sonar Reading:" followed by the value of the variable "x" will be displayed on line 1 of the screen of the NXT
      wait1Msec(1000);                              //the program waits 1000 milliseconds before running further code
      eraseDisplay();                               //the NXT display is cleared
   }
   //nSyncedMotors = syncMotors;
   //nPidUpdateInterval = 5;
   //search_min360();
   //drive_straight();
}


The sample code with deleted comments (not working after deleting the sudden comments on top):
Code:
const tSensors sonarSensor          = (tSensors) S4;

task main()
{
   int x=0;

   while(true)
   {
      x = SensorValue(sonarSensor);
      nxtDisplayTextLine(1, "Sonar Reading:%d", x);

      wait1Msec(100);
   }
}


Sample code with sudden comments (working):
Code:
//*!!Sensor,    S4,          sonarSensor, sensorSONAR,      ,                    !!*//
//*!!                                                                            !!*//
//*!!Start automatically generated configuration code.                           !!*//
const tSensors sonarSensor          = (tSensors) S4;

task main()
{
   int x=0;

   while(true)
   {
      x = SensorValue(sonarSensor);
      nxtDisplayTextLine(1, "Sonar Reading:%d", x);

      wait1Msec(100);
   }
}


My program condensed to the core parts of the sample program, which starts producing correct number after inserting the first comment-lines:
Code:
//*!!                                                                            !!*//
//*!!Start automatically generated configuration code.                           !!*//
const tSensors sonarSensor          = (tSensors) S4;

task main()
{
    int x=0;

   while(true)
   {
      x = SensorValue(sonarSensor);
      nxtDisplayTextLine(1, "Sonar Reading:%d", x);

      wait1Msec(100);
   }
}



I'm sorry for the lengthy post, but after all I'm really confused and don't know how to explain shortly. Thank you very much for reading (if you reached this point ;) ).[/code]


Wed Jul 23, 2008 1:26 am
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post 
Hi!
My advice:
don't use
Code:
//*!!                                                                            !!*//
//*!!Start automatically generated configuration code.                           !!*//
const tSensors sonarSensor          = (tSensors) S4;

at all, both the const... declaration and the automatically generated code appear sort of mystic, fancy, and totally useless to me .
Try to use
Code:
SensorType(...)(

at the first line of your task main() in order to specify your sensors and the ports they are attached to.
If it's still faulty, post it to the developers:
a) bug tracker: http://www.education.rec.ri.cmu.edu/too ... n_page.php
b) support: viewtopic.php?t=629
I'll appreciate if you'd tell us about any reply... :?
HTH!

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Last edited by Ford Prefect on Wed Jul 23, 2008 4:22 am, edited 4 times in total.



Wed Jul 23, 2008 4:08 am
Profile
Rookie
User avatar

Joined: Thu Jun 26, 2008 11:27 am
Posts: 21
Location: Johannesburg, South Africa
Post 
Hi

The strange comment lines
Code:
//*!!Sensor,    S4,          sonarSensor, sensorSONAR,      ,                    !!*//
//*!!                                                                            !!*//
//*!!Start automatically generated configuration code.                           !!*//

are generated by RobotC when you use the 'Motors and Sensors Setup' function on the 'Robot' menu. You can hide this under the View menu (Hide configuration code) if you want.
This tells the compiler that the sensor on port 4 (ie S4) is a sonar sensor. So if you delete this the compiler does not know what sensor is connected which is why you get meaningless readings.
You can also do this in your program thus:
Code:
SensorType[S4] = sensorSONAR;
as suggested by Ford Prefect (I also prefer to do it this way)
The line
Code:
const tSensors sonarSensor          = (tSensors) S4;
simple defines a new name for port S4 which makes your code more readable and also means if you change the sonar sensor to another port you only have to change this one line not every occurance of S4

Hope this helps


Wed Jul 23, 2008 4:18 am
Profile WWW
Rookie

Joined: Wed Jul 23, 2008 12:48 am
Posts: 19
Location: Germany
Post 
Hey, thank you very much. This sounds reasonable to me and I will test it as soon as I have time again to get my hands on my robot (can't wait for it :) ).
I find it strange the compiler is generating code which is suddenly printed in my program. Makes me feel there is something happening which I can't see and control. But I would not have complained if this would have worked automatically also for my program. The SensorType-Command was just missing in the examples I was looking at. So, thanks for pointing that out.


Wed Jul 23, 2008 5:12 am
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post 
zeron wrote:
I find it strange the compiler is generating code which is suddenly printed in my program. Makes me feel there is something happening which I can't see and control.

This is just the way I feel... :?

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Wed Jul 23, 2008 5:20 am
Profile
Expert
User avatar

Joined: Fri Nov 09, 2007 4:51 am
Posts: 121
Location: Hungary, Europe
Post 
zeron wrote:
Makes me feel there is something happening which I can't see and control.


Linux guy first meeting Windows :-)


Wed Jul 23, 2008 12:54 pm
Profile
Rookie

Joined: Wed Jul 23, 2008 12:48 am
Posts: 19
Location: Germany
Post 
elemes wrote:
Linux guy first meeting Windows :-)


:lol:

Now I have tested with explicit "SensorType" declaration and its all working smooth. It was just too confusing to have two programs which obviously contain essentially the same code, but are working different. Thank you again for your fast replies.


Thu Jul 24, 2008 2:03 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 7 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.