View unanswered posts | View active topics It is currently Fri Oct 24, 2014 10:49 am






Reply to topic  [ 5 posts ] 
Switching sensors in a program 
Author Message
Rookie

Joined: Tue Jun 18, 2013 6:04 pm
Posts: 29
Post Switching sensors in a program
I have the bot following a line using the light sensor. When it reaches the end of the line, I would like the sonar sensor to take over. I have the bot in a while loop to follow the line, but when it reaches the end of the line, it just goes in circles. How can I get the light sensor program to end and the sonar sensor take over? I can't post the program because it's on a students computer at school.


Mon Jul 15, 2013 6:57 pm
Profile
Moderator
Moderator

Joined: Mon Oct 04, 2010 2:18 pm
Posts: 196
Post Re: Switching sensors in a program
What is the condition in your while loop? For example, look at the code below. The condition in the while loop is an encoder count for the right motor. As long as the encoder count is less than 1800, the robot will do what is contained inside of the while loop.

Code:
#pragma config(Sensor, S1,     touchSensor,              sensorTouch)
#pragma config(Sensor, S2,     compassSensor,            sensorVirtualCompass)
#pragma config(Sensor, S3,     lightSensor,              sensorLightActive)
#pragma config(Sensor, S4,     sonarSensor,              sensorSONAR)
#pragma config(Motor,  motorA,          gripperMotor,       tmotorNormal, PIDControl, encoder)
#pragma config(Motor,  motorB,          rightMotor,         tmotorNormal, PIDControl, encoder)
#pragma config(Motor,  motorC,          leftMotor,          tmotorNormal, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

/*+++++++++++++++++++++++++++++++++++++++++++++| Notes |++++++++++++++++++++++++++++++++++++++++++++++
Line Track for Distance
This program uses 1 NXT Line Follower Sensor to track a black line on a light(er) surface
for 5 rotations of the encoders. There is a two second pause at the beginning of the program.

The threshold variable defined in this program may need to be changed for the line/surface your
robot is tracking and the lighting conditions.

Robot Model(s): NXT REMBOT

[I/O Port]          [Name]              [Type]                [Description]
Motor Port 2        rightMotor          NXT Motor             Right side motor
Motor Port 3        leftMotor           NXT Motor             Left side motor
Sensor Port S3      lightSensor         NXT Line Follower     Front-center, facing down

----------------------------------------------------------------------------------------------------*/

//+++++++++++++++++++++++++++++++++++++++++++++| MAIN |+++++++++++++++++++++++++++++++++++++++++++++++
task main()
{
  wait1Msec(2000);          // The program waits for 2000 milliseconds before continuing.

  int threshold = 45;       /* found by taking a reading on both DARK and LIGHT    */
                            /* surfaces, adding them together, then dividing by 2. */

  nMotorEncoder[rightMotor] = 0;  // Reset the right motor encoder to 0.

  //Line Track for 5 rotations...
  while(nMotorEncoder(rightMotor) < 1800)
  {
    // sensor sees light:
    if(SensorValue(lightSensor) < threshold)
    {
      // counter-steer left:
      motor[leftMotor]  = 55;
      motor[rightMotor] = 25;
    }
    // sensor sees dark:
    else
    {
      // counter-steer right:
      motor[leftMotor]  = 25;
      motor[rightMotor] = 55;
    }
  }
}


Mon Jul 15, 2013 8:01 pm
Profile
Rookie

Joined: Tue Jun 18, 2013 6:04 pm
Posts: 29
Post Re: Switching sensors in a program
Hi,
Thanks for responding. I'm at school, so here is the program.

#pragma config(Sensor, S1, sonarSensor, sensorSONAR)
#pragma config(Sensor, S2, lightSensor, sensorLightActive)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

task main()
{
while(SensorValue(sonarSensor) > 30)
{
while(SensorValue(lightSensor) < 62.5)
{
motor[motorB] = 50;
motor[motorC] = 0;

}
while(SensorValue(lightSensor) > 62.5)
{
motor[motorB] = 0;
motor[motorC] = 50;

}
}
motor [motorB] = 0;
motor [motorC] = 0;
wait1Msec (2000);
}


Tue Jul 16, 2013 8:05 am
Profile
Moderator
Moderator

Joined: Mon Oct 04, 2010 2:18 pm
Posts: 196
Post Re: Switching sensors in a program
Hi Chris,

Everything you need can be found in this video:

http://www.education.rec.ri.cmu.edu/previews/robot_c_products/teaching_rc_tetrix_preview/sensing/linetrack/videos/linetrack2.html

Let me know if you have any questions after viewing the video.


Tue Jul 16, 2013 12:58 pm
Profile
Rookie

Joined: Tue Jun 18, 2013 6:04 pm
Posts: 29
Post Re: Switching sensors in a program
Parkway,
I used the encoders, as you suggested, and it worked out very well.
Thank you for your help.


Wed Jul 17, 2013 8:43 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 5 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.