# Code for 2nd run of the maze

My 3pi has been working great but for some reason I cannot, for the life of me, get it to go faster for the second run after it solves it… basically i want it to run normal for the first run then after it has found the simplified path I want to increase the motors to a higher speed and get to the end as fast as possible… any help please???

Hello,

What are you doing to make it go faster? Does it still explore every path the second time, or does it go straight to the end? Can you ask a more specific question about what you are trying to do?

- Ryan

yeah it will solve the maze(go straight to the end) in the second phase just fine, but I want to increase the speed when it knows the shorter path. for example, for the maze I built the motor speed works great at 90, when its in the learning phase… but once it learns the maze for the simplified path I wish to increase it to… say 150 or so…
Do you put the code in the line follow-segment.c, maze_solve.c, main.c??? and what would the code look like?

The code in follow-segment.c is probably what you want to modify. The speed of the robot on the straightaways is controlled by the the ‘max’ variable in the follow_segment method. If you made the follow segment method take a parameter for the max speed, you could change the calls to follow_segment to have an appropriate max speed for that particular call. The method signature would be something like this:

``````void follow_segment(int max)
``````

As you increase the speed, you may have to tune the PID constants to keep your 3pi on the line. The PID constants are on this line in the code:

``````int power_difference = proportional/20 + integral/10000 + derivative*3/2;
``````

- Ryan

It does answer my question and thanks for the quick response… and it did work!!!

however, it did it for both phases, the learning phase and the simplified phase… I just want it to do this(code you gave me) on the simplified phase???

-Mike

In maze-solve.c there are three places where the follow_segment method is called:

``````// FIRST MAIN LOOP BODY
follow_segment();

// SECOND MAIN LOOP BODY
follow_segment();

// Follow the last segment up to the finish.
follow_segment();
``````

After making the changes I suggested in my previous post, I would change these lines to something like:

``````// FIRST MAIN LOOP BODY
follow_segment(60);

// SECOND MAIN LOOP BODY
follow_segment(80);

// Follow the last segment up to the finish.
follow_segment(80);
``````

- Ryan

once again thanks again for the quick response,
but when I tried what you suggested nothing different happened!?!?
any thoughts???

its remained the same speed in both phases…

here’s my code for the follow-segment( not all of it)

``````#include <pololu/3pi.h>

void follow_segment(int max)
{
int last_proportional = 0;
long integral=0;

while(1)
{
unsigned int sensors[5];

int proportional = ((int)position) - 2000;

int derivative = proportional - last_proportional;
integral += proportional;

last_proportional = proportional;

int power_difference = proportional/20 + integral/10000 + derivative*3/2;

const int max = 110;
if(power_difference > max)
power_difference = max;
if(power_difference < -max)
power_difference = -max;

if(power_difference < 0)
set_motors(max+power_difference,max);
else
set_motors(max,max-power_difference);

and heres my maze solve code( not all of it)
// MAIN LOOP BODY
follow_segment(60);

set_motors(50,50);
delay_ms(50);

// These record whether the robot has seen a line to the
// left, straight, or right, while learning the
// intersection it is at.
unsigned char found_left=0;
unsigned char found_straight=0;
unsigned char found_right=0;

// This reads the sensors and checks the intersection.
unsigned int sensors[5];

// Check for left and right tunrs.
if(sensors[0] > 100)
found_left = 1;
if(sensors[4] > 100)
found_right = 1;

// move forward straight(only just a little bit) - this lines up our
// wheels with the intersection.
set_motors(40,40);
delay_ms(200);
................
// SECOND MAIN LOOP
follow_segment(80);
// Drive straight as before.
set_motors(50,50);
delay_ms(50);
set_motors(40,40);
delay_ms(200);
// Make the turns according to the instructions stored.
turn(path[i]);
}
// Follow the last part to the finish.
follow_segment(80);
``````

The line `const int max = 110; `is shadowing your method variable.

Just comment out that line.

-Ryan

There is goes… its always something so simple… isn’t it???
Thanks you so much!!!
-Mike

so what was your PID for such fast speeds ?