NXC: Need help to complete project

Discussion specific to projects ideas and support.
Post Reply
gravy1992
Posts: 2
Joined: 24 Jan 2012, 16:57

NXC: Need help to complete project

Post by gravy1992 »

Hello,

First of all, I have no idea whether I'm just too stupid to come to a conclusion regarding the problem, or it is just not possible to solve it without adding additional sensors/hardware...
Either way, time doesn't run slower and the deadline comes closer far too fast and I really hope that you are able to help me.

The task:
A Robot controlled by a single NXT-Brick has to follow a black line on white ground. Occasionally there are 3 white stripes on the black line indicating a platform next to the road. The roboter then has to leave the course, push a small ball off the platform, return to the line and continue it's journey.

Specs:
Line:
- length: 16m
- diameter: 3-6cm
Stripes:
- length: 2cm
- diameter: 3-6cm

To build the robot we are allowed to use 1 NXT-Brick, 3 motors, 2 light sensors, 2 touch sensors and all kinds of non-electrical LEGO-Bricks. The code has to be written in NXC.

As for now my group and I were able to code the program which lets the robot follow the black line.

Code: Select all

#define BLACK 30
#define _moveForward(motor, strength) \
   OnFwd(motor, strength);
#define _stop(motor) \
   Off(motor);

int turn;     //0=Right, 1=Left

sub rightCurve() {
   until(Sensor(IN_1)<= BLACK && Sensor(IN_2)<=BLACK){
      OnFwd(OUT_BC, 100);
      OnRev(OUT_A, 50);
   }
}

sub leftCurve() {
   until(Sensor(IN_1)<= BLACK && Sensor(IN_2)<=BLACK){
      OnFwd(OUT_AC, 100);
      OnRev(OUT_B, 50);
   }
}

sub straightOn() {
   OnFwd(OUT_ABC, 100);
}

task main() {
   SetSensorLight(IN_1);  //Right
   SetSensorLight(IN_2);  //Left
   
   PlayToneEx(220, 100, 100, 0);
   
   while(true) {
      _moveForward(OUT_ABC, 60);

      if(Sensor(IN_1)>BLACK && Sensor(IN_2)>BLACK) { //Both sensors white
         if(turn=0) {
            rightCurve();
         }
         if(turn=1) {
            leftCurve();
         }
      }
      
      if(Sensor(IN_1)<BLACK && Sensor(IN_2)<BLACK) { //Both sensors black
         straightOn();
      }

      if(Sensor(IN_1)>BLACK && Sensor(IN_2)<BLACK) { //Right: White, Left: Black
         turn = 1;
         leftCurve();
      }
      
      if(Sensor(IN_1)<BLACK && Sensor(IN_2)>BLACK) { //Right: Black, Left: White
         turn = 0;
         rightCurve();
      }
   }
}
But regarding the code for the stripes we are becoming frustrated more and more...

This is a video of an earlier stage of the robot:
You can see the stripes and the platform around 0:15 mins

The problem we see is, that there is absolutely no way to distinguish the stripes from the white ground.

I know that you all are much much more familiar with Mindstorms, NXT and NXC and I really hope there is someone able to help.

Hoping for fast answers,
gravy

P.s. I hope I added all the information needed. If something important misses please tell me.
tabbycatrobots
Posts: 100
Joined: 27 Dec 2010, 19:10

Re: NXC: Need help to complete project

Post by tabbycatrobots »

Maybe you can use some ideas from my project "Kemmel - Combining Line Following and Formula 1", described
in this forum, Mindstorms Projects. Your robot has 2 sub-tasks 1. follow the black line, and 2. watch for
a marker of some sort (in this case, some black / white stripes), and the robot has 2 light sensors. Structure
your software the same way. Are you familiar with multi-tasking (multi-threaded) programs? This is a
simple way to solve this problem. Use one task, in the program, to monitor one light sensor and thus to monitor
the left (or right) side of the black line. Use a second task in your program to monitor the 2nd light sensor
and count black white changes. The first task that is monitoring the line edge also controls the motors, i.e.
steering. When the 2nd task counts 3 black white changes, it writes a global variable that the 1st task is
checking periodically. (Through trial and error, you will have to determine the checking period.) When the
1st task reads a pre-determined value in this variable, it stops following the line, and does the ball pushing.
So, I suggest thinking about the software part as a multi-threaded program. Hope this helps. suggestions -
mount the light sensors such that when one is over the left side of the black line, the 2nd light sensor is over
the middle of the black line. Improve your line following code so the robot tracks the left side of the line better.
tabbycatrobots
Posts: 100
Joined: 27 Dec 2010, 19:10

Re: NXC: Need help to complete project

Post by tabbycatrobots »

In my "Kemmel" topic posts, see my last post for the R03 code zip file. And another suggestion,
in the thread that watches for stripes, have some code such that after it sees 6 - 10 cm of
black, it starts back at 0, counting the stripes. This should help handle the case of the robot wobbling
as it line follows, such that the 2nd sensor is not over the center of the black line, and the 2nd
sensor sees white background and starts counting stripes.
mcsummation
Posts: 220
Joined: 23 Jan 2012, 17:07
Location: Round Rock, TX

Re: NXC: Need help to complete project

Post by mcsummation »

The motor/wheels in the back seem to make it harder to turn sharply when the track changes quickly. Why not use a "free castering wheel" back there? Something like this http://www.nxtprograms.com/NXT2/castor_bot/index.html.
pepijndevos
Posts: 175
Joined: 28 Dec 2011, 13:07
Location: Gelderland, Netherlands
Contact:

Re: NXC: Need help to complete project

Post by pepijndevos »

It seems you are currently putting the light sensors outside of the track. While you could follow the line with one sensor using PID, another option is to put both sensor *within* the line. This way, you can distinguish driving off the line from the stripes, because normally, only one sensor will see white, whereas with the stripes, both will be white.

So in theory you don't even need to count them, just as soon as you get 2 white readings, do the pushing, if you get 1 white reading, turn.
-- Pepijn
http://studl.es Mindstorms Building Instructions
gravy1992
Posts: 2
Joined: 24 Jan 2012, 16:57

Re: NXC: Need help to complete project

Post by gravy1992 »

tabbycatrobots wrote:In my "Kemmel" topic posts, see my last post for the R03 code zip file
First of all I really have to thank you all, especially you tabbycatrobots.
Mostly because of your code we drastically changed our point of view about NXC-programming. Thank you very very much for supplying us with such a great example :)

The reason why I did not check this thread very frequently after reading the first answers is, because we finally got the good ideas to complete the code and therefore had been quite busy.
It might not be the perfect solution (in fact we just use the very basic methods and commands), but it is working perfectly.

What we did was basically adding quite a lot of "timers" checking for white stripes or plain white space, indicating a loss of the track.

The result:



Im sure that there are still lots of possibilities to enhance the code, but I am quite confident that everything will work out fine when we have to present our precious little robot ;)

So once again: Thank you all and keep on with what you do. Lots of the projects I've seen here are not only fantastic creations, but true art!
tabbycatrobots
Posts: 100
Joined: 27 Dec 2010, 19:10

Re: NXC: Need help to complete project

Post by tabbycatrobots »

I'm glad my ideas could help. I'm interested in the case in the video, where there is a very sharp corner
and if your robot doesn't turn enough on the first try, it travels in reverse for a short distance, then
tries to travel around the corner a second time, and always seems to be successful on the second try. If
you don't mind, would you upload your programs that shows this - maybe in a zip file. I'm thinking this
technique might help when my racing robots miss their pit entrance.
Thanks.
Howard
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests