View unanswered posts | View active topics It is currently Sat Aug 02, 2014 6:10 am






Reply to topic  [ 3 posts ] 
Finding a Candle 
Author Message
Rookie

Joined: Sun Mar 11, 2012 11:15 pm
Posts: 3
Post Finding a Candle
Hey,

I am working on a project and I am used ROBOTC to program my NXT robot.

For this program I need to use the light senor to locate and go to the candle but I am having trouble getting it to work.

What I need for it to do is check for light and if it finds a strong enough light source to go to the source and just quit the entire program if it does not find a strong enough light source it needs to break out and back into the main function so it can continue on to the next room.

I have the light sensor attached to the robot almost like an arm pointing straight out infront.

If you have any ideas please let me know thanks!!


#pragma config(Sensor, S1, light, sensorLightInactive)
#pragma config(Sensor, S4, Sonar, sensorSONAR)
#pragma config(Motor, motorB, RightWheel, tmotorNormal, PIDControl, encoder)
#pragma config(Motor, motorC, LeftWheel, tmotorNormal, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

void checkLight();

task main ()
{
nMotorPIDSpeedCtrl[motorC] = mtrSpeedReg;
nMotorPIDSpeedCtrl[motorB] = mtrSpeedReg;

motor[motorC] = 100;
motor[motorB] = 100;
wait1Msec(1000);

checkLight();

motor[motorC] = 100;
motor[motorB] = 100;
wait1Msec(5000);

}

void checkLight()
{
if (SensorValue(light) > 60)
{
while (SensorValue(Sonar) > 15 )
{
motor[motorC] = 25;
motor[motorB] = 25;
}
}

else
motor[motorC] = -25;
motor[motorB] = 50;
return;
}


Sun Mar 11, 2012 11:22 pm
Profile
Rookie

Joined: Thu Mar 13, 2014 6:12 am
Posts: 18
Post Re: Finding a Candle
Normally functions are inserted at the beginning of a programme.


Fri Jul 18, 2014 3:46 am
Profile
Rookie

Joined: Wed Feb 24, 2010 11:51 pm
Posts: 12
Post Re: Finding a Candle
I'll play the part of the robot and "run" your program.
amullen98 wrote:
Code:
 task main ()
{
  nMotorPIDSpeedCtrl[motorC] = mtrSpeedReg;
  nMotorPIDSpeedCtrl[motorB] = mtrSpeedReg;

  motor[motorC] = 100;
  motor[motorB] = 100;
  wait1Msec(1000);
   
  checkLight();
 
  motor[motorC] = 100;
  motor[motorB] = 100;
  wait1Msec(5000);
 
}


The main function tells your robot to go straight forward at full speed for 1 second, check for light once, then go full speed forward again for 5 seconds.

amullen98 wrote:
Code:
void checkLight()
{
  if (SensorValue(light) > 60)
  {
    while (SensorValue(Sonar) > 15 )
      {
   motor[motorC] = 25;
   motor[motorB] = 25;
      }
    }

    else
         motor[motorC] = -25;
   motor[motorB] = 50;
              return;
 }

The checkLight function checks for a bright light once.
If there is a bright light, then move forward slowly until the sonar is less than 15. Then start turning in an arc. Then return back to main.
If there isn't a bright light, then start turning in an arc and return back to main.

There are a few problems:
1) In both cases, as soon as checkLight returns to main, the bot will immediately start going full speed straight forward. This covers up any attempt to turn in an arc. Probably not what you want.
2) in the else case in checkLight, there are no {} around the turn request, so only motor[motorC]=-25; is conditioned on the light value. motor[motorB]=50; will always execute before returning to main. This is why I said, "Then start turning in an arc" in both light and dark cases. If you have trouble spotting these kind of the problems, try using the RobotC "Fix Formatting" button to adjust the indenting of the code to make it more obvious.
3) In checkLight, if your intent is to turn slowly until a bright light is detected, you need keep looping and checking the light repeatedly instead of returning. But make sure to have some sort of timeout/max distance turned limit so you don't spin forever in a dark room.

Perhaps you should pair up with a friend/teammate and write down instructions on paper, using normal people words (not code), to describe the steps a robot-like person would have to take to solve the find-a-candle problem. Test the instructions with another person. Make sure they follow the instructions exactly and don't fill in rules that they "should just know". Once you've gotten those steps worked out, then translating them into RobotC should be easier.

Terry

_________________
Vex Coach and robotics instructor


Fri Jul 18, 2014 1:49 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 3 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.