View unanswered posts | View active topics It is currently Tue Nov 25, 2014 4:49 pm






Reply to topic  [ 7 posts ] 
optimizing code for multiple sensors 
Author Message
Rookie

Joined: Mon Nov 09, 2009 10:51 am
Posts: 23
Post optimizing code for multiple sensors
I have a student making a basic bot with 4 ultrasonic sensors, each facing a different direction, forward, back, and to both sides. The idea is for the bot to stay put when nothing is detected and then drive away from any of the 4 sensors that detects something closer than 20cm.
The problem is the sensors often detect a close object but often don't and it's very inconsistent, sometimes not detecting anyting for several seconds. I'm wondering if the control structures could be written more efficiently.
The task main is below, with void functions for each sensor condition (forwards, backwards, right, left, standstill). She also tried a series of if conditions instead of using else ifs. The void functions each take 1 second to complete. The 'show' function displays the sensor values on different lines so she can see what the sensors are picking up and includes a wait time of 200 msec.
She tried increasing the threshold to 30 and the sensors were more consistently responsive but still seems it could be better.
Code:
task main()
{
  wait1Msec(500); //initialize sensors
  while(true)
    {
  show();
    if(SensorValue[sonarSensor1] < 20)
    {
       backwards(); //both motors reverse
     }
    else if(SensorValue[sonarSensor2] < 20)
    {
      forwards(); //both motors forward
    }
    else if(SensorValue[sonarSensor3] < 20)
    {
      leftturn(); //point turn, drive forward
    }
    else if(SensorValue[sonarSensor4] < 20)
    {
      rightturn(); //point turn, drive forward
    }
    else
    {
        standstill(); //both motors at 0 power, tried both no wait time and 100 msec wait time.
    }
  }
}

Thanks for any suggestions.


Tue Jan 03, 2012 11:03 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3289
Location: Rotterdam, The Netherlands
Post Re: optimizing code for multiple sensors
The trouble with multiple ultrasound sensors is that they may interfere with each other. They work by means of echo location, so you can imagine with four of them it can be a little bit like trying to have a conversation in a very noisy room. Mind you, this isn't always the case and depends heavily on the placement of the sensors, the distance between the sensors and the targets, etc, etc.
In any case, it would really help if you could show us -all- of your program not just a snipped of what you might think is relevant.

Regards,
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]


Wed Jan 04, 2012 2:51 am
Profile WWW
Rookie

Joined: Mon Nov 09, 2009 10:51 am
Posts: 23
Post Re: optimizing code for multiple sensors
Thanks, the sensors are all facing away from each other, like in a north, south, east, west orientation. I may suggest moving one farther out than it is. Here is the whole program, sorry I truncated it:
Code:
#pragma config(Sensor, S1, sonarSensor1, sensorSONAR)
#pragma config(Sensor, S2, sonarSensor2, sensorSONAR)
#pragma config(Sensor, S3, sonarSensor3, sensorSONAR)
#pragma config(Sensor, S4, sonarSensor4, sensorSONAR)

void show()
{
      nxtDisplayTextLine(2,"Sonar1: %d", SensorValue[sonarSensor1]);
      nxtDisplayTextLine(3,"Sonar2: %d", SensorValue[sonarSensor2]);
      nxtDisplayTextLine(4,"Sonar3: %d", SensorValue[sonarSensor3]);
      nxtDisplayTextLine(5,"Sonar4: %d", SensorValue[sonarSensor4]);
      wait1Msec(200);
}

void standstill()
    {
      nxtDisplayTextLine(1,"still");
      motor[motorB] = 0;
      motor[motorC] = 0;
    }

void backwards()
  {
      nxtDisplayTextLine(1,"back");
      motor[motorB] = -50;
      motor[motorC] = -50;
      wait1Msec(1000);
    }

void forwards()
{
      nxtDisplayTextLine(1,"forwards");
      motor[motorB] = 50;
      motor[motorC] = 50;
      wait1Msec(1000);
  }

void leftturn()
{
       nxtDisplayTextLine(1,"left");
     motor[motorB] = -50;
      motor[motorC] = 50;
      wait1Msec(500);
      motor[motorB] = 50;
      motor[motorC] = 50;
      wait1Msec(500);
  }

void rightturn()
{
      nxtDisplayTextLine(1,"right");
      motor[motorB] = 50;
      motor[motorC] = -50;
      wait1Msec(500);
      motor[motorB] = 50;
      motor[motorC] = 50;
      wait1Msec(500);
  }

task main()
{
  wait1Msec(500);
  while(true)
    {
    show();
    if(SensorValue[sonarSensor1] < 30)
      {
          backwards();
      }
    else if(SensorValue[sonarSensor2] < 30)
    {
      forwards();
    }
    else if(SensorValue[sonarSensor3] < 30)
    {
      leftturn();
    }
    else if(SensorValue[sonarSensor4] < 30)
    {
      rightturn();
    }
    else
    {
        standstill();
    }
  }
}


Wed Jan 04, 2012 7:23 am
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: optimizing code for multiple sensors
Assuming you park your robot in a center of a large room where it should not see any obstacles in any direction, you said you occassionally see the readings of some ultrasonic sensors showing a close object. In order to prove if it is due to interference or not, note which sensor gives you erroneous reading most often. Unplug all the other sensors, just leaving that one and run your program again and see if you no longer get erroneous readings from that sensor. If it is really from the interference of other sensors, one way to avoid it is to use a single sonar sensor but put it on a rotating platform so you can have it "look around". I wrote a "radar" module to experiment with wall following. The radar module allows me to define "sampling points" in terms of the platform angle. So I can define 4 sampling points (0, 90, 180, 270). It will sweep back and forth at the speed I defined between the two extreme angles and stop briefly on each sampling point and take a reading.
Alternatively, there is also a forum thread talking about using "single shot" mode to avoid interference of multiple sonar sensors.
viewtopic.php?f=1&t=3723&p=16024&hilit=single#p16024
I think Xander might have added that support to his drivers. Xander?


Wed Jan 04, 2012 3:51 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3289
Location: Rotterdam, The Netherlands
Post Re: optimizing code for multiple sensors
I did add it but I didn't test it properly I think. I'll have to look at it again but I am not sure I'll get to it before I go on vacation this coming Saturday.

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


Thu Jan 05, 2012 2:59 am
Profile WWW
Novice

Joined: Thu Apr 17, 2008 6:04 am
Posts: 70
Location: Plymouth, Indiana, USA
Post Re: optimizing code for multiple sensors
I suggest assigning a variable to each sensor's reading and using that variable in the if and the DisplayText statments. It's essential to have some amount of wait time between the readings. The wait time give the echos a chance to die away (to reduce interference). The idea of using a variable is that the text lines can then show what the values were when the if statements were active.

For example:
frontVar = SensorValue[sonarSensor1];
wait1Msec(100);
backVar = SensorValue[sonarSensor2];
wait1Msec(100);
and so on.

You may need to play with the wait times. I did a similar thing with two ultra sonic sensors (90 degrees apart) a while back but can't remember the time between soundings.

Dave

_________________
Dad (Dave) and Adam


Thu Jan 05, 2012 6:13 am
Profile
Rookie

Joined: Mon Nov 09, 2009 10:51 am
Posts: 23
Post Re: optimizing code for multiple sensors
Thanks for all the suggestions!
What she ended up doing was testing different wait times between sensor readings in the task main and landing on 50 Msec's as the optimal time. For some reason that allows interference to die down while not making it wait too long. It actually responds pretty consistently within a fraction of a second now on all 4 sensors! The resulting program is follows:
Code:
#pragma config(Sensor, S1, sonarSensor1, sensorSONAR)
#pragma config(Sensor, S2, sonarSensor2, sensorSONAR)
#pragma config(Sensor, S3, sonarSensor3, sensorSONAR)
#pragma config(Sensor, S4, sonarSensor4, sensorSONAR)

void backwards()
  {
    motor[motorB] = -50;
      motor[motorC] = -50;
      wait1Msec(1500);
    }

void forwards()
{
  motor[motorB] = 100;
      motor[motorC] = 100;
      wait1Msec(1500);
  }

void backwardleftturn()
{

      motor[motorB] = 50;
      motor[motorC] = -50;
      wait1Msec(500);
      motor[motorB] = -50;
      motor[motorC] = -50;
      wait1Msec(1500);
  }

void backwardrightturn()
 {

      motor[motorB] = -50;
      motor[motorC] = 50;
      wait1Msec(500);
      motor[motorB] = -50;
      motor[motorC] = -50;
      wait1Msec(1500);
}

  void standstill()
  {
    motor[motorB] = 0;
    motor[motorC] = 0;
 }

task main()
{
  while(true)
    {
      standstill();

    wait1Msec(50);
    if(SensorValue[sonarSensor1] < 30)
{
    backwards();
 }
     wait1Msec(50);
    if(SensorValue[sonarSensor2] < 30)
    {
      forwards();
    }
     wait1Msec(50);
    if(SensorValue[sonarSensor3] < 30)
    {
      backwardleftturn();
    }
     wait1Msec(50);
    if(SensorValue[sonarSensor4] < 30)
    {
      backwardrightturn();
    }
  }
}


Tue Jan 17, 2012 10:06 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.