View unanswered posts | View active topics It is currently Sat Oct 25, 2014 3:31 pm






Reply to topic  [ 11 posts ] 
IR sensor questions 
Author Message
Rookie

Joined: Mon Jan 03, 2011 10:52 pm
Posts: 22
Post IR sensor questions
So, I am trying to get the IR sensor working on one of our lego bots before I get it going on our FTC bot. The following is my code:

Code:
#pragma config(Sensor, S1,     InfraredSensor,                    sensorHiTechnicIRSeeker1200)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

task main()
{
  while(SensorRaw[InfraredSensor] = 5);
  {
    nMotorEncoder[motorA] = 0;
    nMotorEncoderTarget[motorA] = 1000;
    motor[motorA] = -50;
    motor[motorB] = -50;
   
  }
  while(SensorRaw[InfraredSensor] < 5);
  {
    nMotorEncoder[motorA] = 0;
    nMotorEncoderTarget[motorA] = 1000;
    motor[motorA] = 50;
    motor[motorB] = -50;
  }
  while(SensorRaw[InfraredSensor] > 5);
  {
    nMotorEncoder[motorA] = 0;
    nMotorEncoderTarget[motorA] = 1000;
    motor[motorA] = 50;
    motor[motorB] = -50;
  }
}


Basically if I'm understanding this correctly, what it should do is:

If the beacon is in front of it (in the 5 position) then drive straight forward, if the beacon is to the left turn left, if the beacon is to the right, turn right. I just tossed the target in there so you know what I am looking for. Basically it would figure out where it is, turn a certain distance, check again,drive a certain distance, check again, etc.. until I finally want it to do that last task which is separate from this.


Mon Jan 31, 2011 10:07 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: IR sensor questions
This thread may help you.
http://robotc.net/forums/viewtopic.php? ... acy#p11686

BTW, just spot an issue with your code. You have an assignment in your while condition.
Code:
  while(SensorRaw[InfraredSensor] == 5)
  {
    nMotorEncoder[motorA] = 0;
    nMotorEncoderTarget[motorA] = 1000;
    motor[motorA] = -50;
    motor[motorB] = -50;
   
  }

The "= 5" should have been "== 5".
And also, you probably need another while loop enclosing all these while loops or it won't work. For example, let's say you run for a while and the infrared sensor register < 5. So you move on to the next while loop, turning the robot to correct the angle. But when you are back == 5, you can't go back to the first while loop to go forward and the program ends.


Last edited by MHTS on Mon Jan 31, 2011 11:12 pm, edited 1 time in total.



Mon Jan 31, 2011 10:13 pm
Profile
Rookie

Joined: Mon Jan 03, 2011 10:52 pm
Posts: 22
Post Re: IR sensor questions
Thanks,

I'll have to tweak the code I'm sure for our robot. But I tried this code (not sure if I looped it correctly though).

Code:
#pragma config(Sensor, S1,     InfraredSensor,                    sensorHiTechnicIRSeeker1200)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

task main()
{
  while(true);
  {
    while(SensorRaw[InfraredSensor] == 5);
    {
      motor[motorA] = -50;
      motor[motorB] = -50;
    }
    while(SensorRaw[InfraredSensor] < 5);
    {
      motor[motorA] = 50;
      motor[motorB] = -50;
    }
    while(SensorRaw[InfraredSensor] > 5);
    {
      motor[motorA] = 50;
      motor[motorB] = -50;
    }
  }
}


To be honest I know almost nothing about coding, I'm lucky to have made it this far. Now it doesn't work yet... I think it might be because of the while(true); statement but I have no idea. It loads just fine but no reaction from the motors even with moving the beacon around. Also curious if I could have it stop this loop and exit it after a certain time or distance traveled.


Mon Jan 31, 2011 10:31 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: IR sensor questions
You may want to do something like this:
Code:
#pragma config(Sensor, S1,     InfraredSensor,                    sensorHiTechnicIRSeeker1200)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

task main()
{
  while(true)
  {
    if (SensorRaw[InfraredSensor] < 5)
    {
      motor[motorA] = 50;
      motor[motorB] = -50;
    }
    else if (SensorRaw[InfraredSensor] > 5)
    {
      motor[motorA] = -50;
      motor[motorB] = 50;
    }
    else
    {
      motor[motorA] = -50;
      motor[motorB] = -50;
    }
  }
}

Wait, the <5 and >5 motor values are the same? I have changed it to be different but depending on what direction the motors should go, you may need to correct the sign of the values.


Last edited by MHTS on Mon Jan 31, 2011 10:59 pm, edited 4 times in total.



Mon Jan 31, 2011 10:37 pm
Profile
Rookie

Joined: Mon Jan 03, 2011 10:52 pm
Posts: 22
Post Re: IR sensor questions
I never can use else statements... I always get the following error:

Code:
**Error**:Unexpected 'else'. Ignored.
**Error**:Unexpected scanner token-> '{'
**Error**:Unexpected scanner token-> '}'


Mon Jan 31, 2011 10:38 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: IR sensor questions
Sorry, I missed deleting a ';' off of the second else if. I will edit the previous post to correct it.


Mon Jan 31, 2011 10:41 pm
Profile
Rookie

Joined: Mon Jan 03, 2011 10:52 pm
Posts: 22
Post Re: IR sensor questions
Sorry I'm just punching in the code as I go. But now it doesn't even move... hrmmm. I'll have to look into and i'll let you know what I can find.


Mon Jan 31, 2011 10:52 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: IR sensor questions
Oh, I found another ';' on the while (true). I will correct the previous post.


Mon Jan 31, 2011 10:59 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: IR sensor questions
In general, ';' is used to terminate a statement. If the statement is not done yet, don't put a ';' there. For example, the while statement consists of a keyword 'while', a condition within a pair of parenthesis, then a single statement or statement block. If you put a ';' at the end of the while statement like you did, it is equivalent to having a NULL statement:
Code:
while(SensorRaw[InfraredSensor] == 5);
is equivalent to:
while (SensorRaw[InfraredSensor] == 5)
{
}

And so is:
while (true);
is equivalent to:
while (true)  //this will loop forever doing nothing.
{
}

This means your code will loop and do nothing.


Mon Jan 31, 2011 11:04 pm
Profile
Rookie

Joined: Mon Jan 03, 2011 10:52 pm
Posts: 22
Post Re: IR sensor questions
Ok cool! Is there a way that I could make this loop checking every say 500 rotations of the shaftencoder? but stopping after it has checked like say 5 times? Or would using another sensors such as a touch sensor be the best thing to use..

i.e. my code would look like this I believe:

Code:
#pragma config(Sensor, S1,     InfraredSensor,                    sensorHiTechnicIRSeeker1200)
#pragma config(Sensor, S1,     TouchSensor,                    sensorTouchSensor)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

task main()
{
  while(sensor[touchsensor] == 0)
  {
    if (SensorRaw[InfraredSensor] < 4.75)
    {
      motor[motorA] = -50;
      motor[motorB] = 50;
    }
    else if (SensorRaw[InfraredSensor] > 5.25)
    {
      motor[motorA] = 50;
      motor[motorB] = -50;
    }
    else
    {
      motor[motorA] = 100;
      motor[motorB] = 100;
    }
  }
  motor[motorC] = 100;
  servo(servo1] = 127;
}


basically it drives until the touch sensor is pressed and then it does the next task... is this something that would work? and can you link a touch sensor and the IR into the same sensor port using the sensor bridge (think that is what it is called).


Mon Jan 31, 2011 11:20 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: IR sensor questions
First off, the SensorRaw values are integers so you cannot compare them to floating point numbers. Secondly, each sensor port takes only one sensor. If you have a SenosrMUX, you can connect the sensor MUX to one sensor ports and connect the IR seeker and touch sensor to two of the four ports on the SensorMUX. But the programming of sensors connecting to the SensorMUX needs to be different. You need to download Xander's (mightor) drivers and look at his sample code. Unless you are running out of ports, I suggest you stick with the NXT sensor ports. Thirdly, after the sensor touches, you are missing some code to stop the driving. So you need to set motorA and motorB to zero.
And finally, I don't understand what you meant by check every 500 rotations and stopping after it checked 5 times. I assume you mean you want to use the encoders to measure how far the distance you have gone and would like to stop it after it has gone a certain distance? If so, yes it can be done.
Code:
task main()
{
  int targetDistance = (nMotorEncoder[motorA] + nMotorEncoder[motorB])/2 + <whatever distance you want it to travel>;
  while((nMotorEncoder[motorA] + nMotorEncoder[motorB])/2 < targetDistance)
  {
    if (SensorRaw[InfraredSensor] < 5)
    {
      motor[motorA] = -50;
      motor[motorB] = 50;
    }
    else if (SensorRaw[InfraredSensor] > 5)
    {
      motor[motorA] = 50;
      motor[motorB] = -50;
    }
    else
    {
      motor[motorA] = 100;
      motor[motorB] = 100;
    }
  }
  motor[motorA] = 0;
  motor[motorB] = 0;

  motor[motorC] = 100;
  servo(servo1] = 127;



Mon Jan 31, 2011 11:36 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 11 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.