Help with NQC code

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
aleynov
Posts: 7
Joined: 22 Feb 2013, 14:27

Help with NQC code

Post by aleynov »

Hi,
I am new here so I apologize in advance if I am posting in the wrong section.
I am working on a code for a tankbot with a light sensor and a touch sensor. It has to follow a black line, and make turns at t intersections. It will bump into a small object, back up and turn. It has to stop at a green pad at the end of a track. This is my code so far:

Code: Select all

#define THRESHOLD 39

int x=0;

task main()
{
SetUserDisplay(x,0);
SetSensor(SENSOR_1,SENSOR_TOUCH);
SetSensor(SENSOR_2,SENSOR_LIGHT);
start turn;
OnFwd(OUT_A+OUT_C);
while(true)
{
if(SENSOR_2 > THRESHOLD)
{OnRev(OUT_C);
OnFwd(OUT_A);
until(SENSOR_2 <=THRESHOLD);}

}
}
task turn()
{
if(x == 0 && SENSOR_1 == 1) //first turn
{
until(SENSOR_1 == 0);
x=x+1;
OnRev(OUT_A+OUT_C);
Wait(20);
OnFwd(OUT_A);
OnRev(OUT_C);
until(SENSOR_2 <= THRESHOLD);}
if(x == 1 && SENSOR_1 == 1) //second turn
{
until(SENSOR_1 == 0);
x=x+1;
OnRev(OUT_A+OUT_C);
Wait(20);
OnFwd(OUT_C);
OnRev(OUT_A);
until(SENSOR_2 <= THRESHOLD);
}



if(x == 2 && SENSOR_1 == 1) //third turn
{until(SENSOR_1 == 0);
x=x+1;
OnRev(OUT_A+OUT_C);
Wait(20);
OnFwd(OUT_A);
OnRev(OUT_C);
until(SENSOR_2 <= THRESHOLD);
}
}

Can somebody help me? It is not working correctly and I have no idea how to improve it.

Here is the link to the track

http://i.imgur.com/XPIRBD3.jpg

thanks!
Last edited by aleynov on 24 Feb 2013, 21:21, edited 1 time in total.
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: Help with NQC code

Post by afanofosc »

Can you post your code in a code block ([ followed by "code" followed by ] then your code followed by the [] with /code inside). That will keep it indented properly, which will make it easier to read.

I don't really think you need a separate turn task. You may need 2 light sensors, one to keep you on the path and another to check for which side the path turns toward. You know to turn whenever the light sensor on the path detects the red spot and the direction to turn is right if the light sensor on the right side of your robot detected black before you reached the red spot or left if the light sensor on the right side of your robot did not detect black before you reached the red spot. When you reach the red spot you may need to backup a bit, depending on the size of your robot and the position of the light sensor that sits above the path. You may need a rotation sensor to count how far back you backup.

If your light sensor is 4 inches ahead of the center of your robot and you can turn in place then you would not need to have a second light sensor at all and you would not need to backup at all. Just rotate clockwise 90 degrees to check for a line to the right and if not found rotate 180 degrees clockwise to check for a line to the left, ignoring the first line found.

Steve Hassenplug can probably give you a lot better advice than I can, though.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
hassenplug
Posts: 346
Joined: 27 Sep 2010, 03:05
Contact:

Re: Help with NQC code

Post by hassenplug »

I almost skipped this post, when I saw the "NQC" title, and a reply from John...

This program is not going to do anything good. It will tell the robot to go straight, until it sees the line, then it will turn (forever). That's all.

I would ask you to think through what you want the robot to do. Write out the steps. Then, we can figure out how to convert that to a program.

Steve
---> Link to lots of MINDSTORMS stuff under my picture --->
aleynov
Posts: 7
Joined: 22 Feb 2013, 14:27

Re: Help with NQC code

Post by aleynov »

Steve,
I need the robot to follow the line until it bumps into the obstacle (touch sensor), then back up about 4 inches, and then pick up the line that it needs to follow without going back in the direction it came from. This code did exactly what you said, it bumped and then turned without picking up any lines.

Thank you for any help.
aleynov
Posts: 7
Joined: 22 Feb 2013, 14:27

Re: Help with NQC code

Post by aleynov »

Also,
I only have 1 touch sensor and 1 bump sensor. The robot is a tankbot with a bumper on the front for bumping into the obstacles. I used a timer in the code to tell it when to turn right or left. So when x == 0 and sensor 1 == 1, that is the first turn and so on. This is a very basic engineering class but this one project has been giving me troubles.
hassenplug
Posts: 346
Joined: 27 Sep 2010, 03:05
Contact:

Re: Help with NQC code

Post by hassenplug »

aleynov wrote:Steve,
I need the robot to follow the line until it bumps into the obstacle (touch sensor), then back up about 4 inches, and then pick up the line that it needs to follow without going back in the direction it came from.
That's a good start. Now, you need to expand on that a bit.
1) How does the robot follow the line?
2) What exactly should it do after bumping into an obstacle?

The more you can break down each of these steps, the easier it will be to write the program.

Steve
---> Link to lots of MINDSTORMS stuff under my picture --->
aleynov
Posts: 7
Joined: 22 Feb 2013, 14:27

Re: Help with NQC code

Post by aleynov »

edit
Last edited by aleynov on 25 Feb 2013, 20:32, edited 1 time in total.
aleynov
Posts: 7
Joined: 22 Feb 2013, 14:27

Re: Help with NQC code

Post by aleynov »

hassenplug wrote: That's a good start. Now, you need to expand on that a bit.
1) How does the robot follow the line?
2) What exactly should it do after bumping into an obstacle?

The more you can break down each of these steps, the easier it will be to write the program.

Steve
The robot is a tankbot with a front mounted light sensor facing down to follow the line, and a touch sensor with an attached bumper to bump into the red obstacles. When the robot bumps the red cube, it has to back up and make the correct turn according to the track. It has to repeat this process until it reaches the green pad where it has to stop.


Thanks again for any help.
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: Help with NQC code

Post by afanofosc »

Does having a light sensor pointing downward enable a robot to follow a line? Do you need to think about and maybe write down what you want the motors on your tank bot to do based on the sensor values that you read from the light sensor? Maybe something like

while the light sensor reading tells me that it sees a black line set both motors to turn forward at the same speed.
while the light sensor reading tells me that it sees a white surface then turn clockwise.

Document using words the algorithm that you want your robot to perform. I am not a line following algorithm expert so my example above is probably not a good plan. Once you have it described in a fair amount of detail in words then it will be lot easier for you to translate that plan into NQC or any other programming language.

Here's an example using 2 sensors over a white line on a black surface:

http://www.societyofrobots.com/programming_mobot.shtml

Another example found via Google that uses one sensor:

http://roborugby.ucd.ie/line-follow.html

A third example, which seems decent:

http://connectankush.blogspot.com/2010/ ... lower.html

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
aleynov
Posts: 7
Joined: 22 Feb 2013, 14:27

Re: Help with NQC code

Post by aleynov »

Here's the best I can think of with what I want the robot to do.

the threshold I am using is 39, which is what it reads when it is on the black line. I am using variable x to keep count of the bumps in order to tell it which way to rotate. So on the first bump,

x=x+1 will tell it to turn right so it picks up the correct line.


code in words

Code: Select all

while the light sensor is at 39 or below, power motors A and C;
when it bumps the first obstacle and x == 0, rotate clockwise until light sensor is at 39 or below, back to powering motors A and C;
when it bumps the second obstacle and x == 1 (having already hit an obstacle making x == 1), rotate counter-clockwise until light sensor is at 39 or below, back to powering motors A and C;
when it bumps the third obstacle and x == 2 (hitting the last obstacle made x == 2), rotate counter-clockwise until light sensor is at 39 or below. back to powering motors A and C;
when it bumps the fourth obstacle and x == 3, rotate clockwise until light sensor is at 39 or below, and power motors A and C;
when it bumps the fifth obstacle and x == 4, rotate clockwise until light sensor is at 39 or below and power motors A and C;
when the light sensor reads the green pad, (i believe it read 36) terminate the program.
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 25 guests