M3pi coding project help

Hello everybody,

So my project is to use m3pi robots to solve a floor layout. The robot is required to follow a maze, go through every turn and intersection, and return to it’s original location. The trick is, the code has to be as SIMPLE as possible. Simple enough that a 9th grader with no coding experience can understand what’s going on. Therefore, the code that does the left, right, and turnaround turns are nothing but if/else if/else statements. This makes the robot “clunky” but it get’s the job done and goes through the maze. However, stopping does not work correctly. With my current code, since the turns aren’t precise, the m3pi sometimes ends up with all it’s sensors under the black line, which makes the motors stop. I’ve tried multiple things, such as the main function calling an outside function checking for stop conditions, but nothing has worked.

I was wondering if anybody can help or suggest some ideas. Please keep in mind it HAS to be simple. I know the problem can be solved using the sample code on the Pololu website, but that code isn’t simple for a kid with no experience to follow. I added the part of the code that needs help. I edited the line following code, so before this part the m3pi initializes and calibrates the sensors.

Thanks for any help.

-Arley

`unsigned int sensors[5];

unsigned int left_sensor = sensors[0];
unsigned int right_sensor = sensors[4];

unsigned int middle_sensor1 = sensors[1];
unsigned int middle_sensor2 = sensors[2];
unsigned int middle_sensor3 = sensors[3];


unsigned int position = read_line(sensors,IR_EMITTERS_ON);


// turn left 
if(left_sensor > 100){

    set_motors(0,50);	
}

//turn right
else if(right_sensor > 100)
    set_motors(50,0);

//go straight
else if(middle_sensor1 > 100 || middle_sensor2 > 100 || middle_sensor3 > 100){

//my attempt to try to stop the motors
	if (position == 2000){

		set_motors(0,0);
		return 0; 
		}
	
	else
		set_motors(50,50);
		}

//turn around
else {
set_motors(-50,50);	
}`

Hello, Arley.

It sounds like you want your m3pi to be able to distinguish between a line and the end point of the maze. What are you using to indicate the maze’s end point (e.g. a black circle)? Can you post a picture showing the maze layout? If the end point is larger than the maze line, you might try driving the m3pi straight a bit more and check that the sensors are still reading a position of 2000 before stopping.

With regards to the turning precision, can you post a video demonstrating that issue?

- Amanda

Hello Amanda,

Thank you for the quick response. Sorry I couldn’t get back to you quicker, I was out of the office for the weekend.

The m3pi will start right above the end spot, run it’s course, and return to the starting spot stopping once it hits the end spot. The end location is marked by large strips of black tape that form a large rectangle. However, I can change the end spot to be whatever I want it to be (a circle, square, etc). I will post a picture of the maze.

I tried posting the video but the file size was too large and the dropbox link was not working. Thank you for your help.

-Arley

When your robot gets to an intersection (or a turn), can you try driving the robot straight for a short period so that its wheels are lined up with the horizontal line before turning? It might be helpful to look at the code under “The Main Loop(s)” section of the 3pi Robot User’s Guide, in particular the third code segment and its comments, to get an idea of how to implement that into your code.

You mentioned having problems posting a video using the forum and Dropbox. Can you try using another video-sharing website like YouTube and post a link to the video here? If are still unable to post your video on the forum, you can try emailing the video to support@pololu.com, or you could mark on your picture to indicate where the bad behavior is happening (if it happens at the same spot every time) so that we can get a better scope of what is happening.

- Amanda

Thanks Amanda, I will try that out and let you know how it goes.

Furthermore, I emailed the video to support@pololu.com with the subject name “Arley Project Help Video”. Can you draw any conclusions from the video? What might be an issue with the code?

-Arley

We received your email and video. I copied the contents of your email (quoted below) for the benefit of others who might be having a similar issue or following this discussion.

From your video, it looks like the 3pi (not an m3pi) is just following the line through each turn. The jerky movements you are seeing are probably due to the 3pi trying to keep the itself centered on the line while making the sharp turn. You might try having the 3pi turn at a set speed for a certain amount of time as soon as the 3pi detects an intersection (or a sharp turn in your maze). You can look at the 3pi’s maze solving code to get an idea on how to implement something like that.

I looked at your code again and noticed that your stop condition is if (position == 2000). The position measurement returned by read_line() is an estimate of the robot’s position with respect to the line; essentially, with your partial code the 3pi will stop if it reads a line directly under sensor 2. To detect the end point of your maze, you would need to check if all three middle sensors are over a black surface. However, if your maze contained intersections (e.g. ‘T’ type and ‘+’ type), you would need to distinguish between an intersection and the end point of your maze, which can be done by driving the 3pi straight for a small amount of time and check the three middle sensors again to see if they are still over a black surface.

I strongly recommend that you look at the 3pi’s maze solving code and comments to get a better understanding of how the code is structured.

- Amanda