# 3pi Spinning Line Follower example

**URL:** https://forum.pololu.com/t/3pi-spinning-line-follower-example/1156
**Category:** Robots
**Created:** [February 26, 2009, 11:45pm UTC](https://forum.pololu.com/t/3pi-spinning-line-follower-example/1156 "2009-02-26T23:45:57Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Byon](https://forum.pololu.com/letter_avatar_proxy/v4/letter/b/e9c0ed/32.png) [@Byon](https://forum.pololu.com/u/Byon)
#### Post date: [February 26, 2009, 11:45pm UTC](https://forum.pololu.com/t/3pi-spinning-line-follower-example/1156/1 "2009-02-26T23:45:57Z")

</div>

I’ve just uploaded a YouTube video of my 3pi robot I call Fourtophi (4^phi is about 3pi, don’t you see) running my SpinLineFollow code. It is basically a line follower that is constant spinning in circles, while it is following a black line. It is not a very fast line follower, but won the “Best in Show” award at our last LVBots Challenge robot competition.

If there is interest, I’d be happy to clean up my code and share it. It’s only about a page’s worth.

Byon

---

<div class="post-metadata">

### Author: ![Ben](https://forum.pololu.com/user_avatar/forum.pololu.com/ben/32/30_2.png) [@Ben](https://forum.pololu.com/u/Ben)
#### Post date: [February 27, 2009, 6:36am UTC](https://forum.pololu.com/t/3pi-spinning-line-follower-example/1156/2 "2009-02-27T06:36:34Z")

</div>

Hello, Byon.

Thank you for sharing your project with us; that’s a very cool variant on line following! I would definitely be interested in seeing the code.

- Ben

---

<div class="post-metadata">

### Author: ![Byon](https://forum.pololu.com/letter_avatar_proxy/v4/letter/b/e9c0ed/32.png) [@Byon](https://forum.pololu.com/u/Byon)
#### Post date: [March 3, 2009, 5:31am UTC](https://forum.pololu.com/t/3pi-spinning-line-follower-example/1156/3 "2009-03-03T05:31:53Z")

</div>

OK, Here it is. I just call this after the normal sensor calibration code.

Byon

```cpp
// Spinning Line Following code for the Pololu 3pi -- Byon Garrabrant - byon @ byon . com
// Ramp each wheel from MIN_SPEED to MAX_SPEED and back as it spins so it will translate along the line.
// Assumes the sensors have already been calibrated.

#include <pololu/3pi.h>

#define	FORWARD_OFFSET	0xA0 // Offset (0..255) of forward from the the front line
#define	MAX_SPEED 255 // Maximum speed the wheels will go
#define	MIN_SPEED 200 // Minimum speed the wheels will go

void Spinning_Line_Follow( void )
{
	unsigned short phase_start = get_ms();	// Start time of this rotation
	unsigned short last_phase_len = 100;	// Duration of the last rotation
	char last_line_side = 0; // which side was the line on?
	char line_count = 0; // Is this the front or back line?
	char led_duration = 0; // How much longer should the LED be on

	while ( 1 ) {
		unsigned short cur_time = get_ms();	// Grab the current time in ms
		unsigned int sensors[5]; // Is the line left or right?
		char line_side = (read_line(sensors,IR_EMITTERS_ON) < 2000);	
		left_led( 0 ); // Turn off the "FRONT" LED
		if (line_side & !last_line_side) {	// If it just changed, 
			if ( ++line_count & 1 ) { // and if this is the front line
				left_led( 1 ); // Turn on "FRONT" LED
				last_phase_len = cur_time - phase_start;// save the last rotation duration
				phase_start = cur_time; // and start counting this rotation
			}
		}
		last_line_side = line_side; // Remember where the line was

		unsigned short cur_phase = cur_time - phase_start;	// How far are we into the curent rotation?
		cur_phase <<= 8; // Multipy by 256
		cur_phase /= last_phase_len; // based on the last rotation duration
		cur_phase += FORWARD_OFFSET; // offset by which direction is "FORWARD"
		short left = cur_phase & 0xFF; // Wrap back to 0 .. 255
		if ( left >= 128 ) { // Convert to 0 .. 127 .. 0
			left = 256 - left;
		}
		left = (((left * (MAX_SPEED - MIN_SPEED))>>7) + MIN_SPEED);	// Scale the wheel speed to be MIN at 0, MAX at 127
		short right = MAX_SPEED + MIN_SPEED - left;	// the right is 180 degress out of phase from the left
		set_motors(left, -right); // and the right goes backwards
	}
}

```

---

<div class="post-metadata">

### Author: ![Ben](https://forum.pololu.com/user_avatar/forum.pololu.com/ben/32/30_2.png) [@Ben](https://forum.pololu.com/u/Ben)
#### Post date: [March 4, 2009, 5:15pm UTC](https://forum.pololu.com/t/3pi-spinning-line-follower-example/1156/4 "2009-03-04T17:15:02Z")

</div>

Thanks for the code, Byon! It’s continues to amaze me that such complex behaviors can come from such small, straight-forward programs.

- Ben

---

<div class="post-metadata">

### Author: ![Andre\_Luis](https://forum.pololu.com/letter_avatar_proxy/v4/letter/a/d6d6ee/32.png) [@Andre\_Luis](https://forum.pololu.com/u/Andre_Luis)
#### Post date: [March 20, 2009, 2:07am UTC](https://forum.pololu.com/t/3pi-spinning-line-follower-example/1156/5 "2009-03-20T02:07:14Z")

</div>

Hello,  
I´d like know how many library used in the program, because I can´t compiler this program in my 3PI… thank you and sorry because my english is not very well…

---

<div class="post-metadata">

### Author: ![Ben](https://forum.pololu.com/user_avatar/forum.pololu.com/ben/32/30_2.png) [@Ben](https://forum.pololu.com/u/Ben)
#### Post date: [March 22, 2009, 2:02am UTC](https://forum.pololu.com/t/3pi-spinning-line-follower-example/1156/6 "2009-03-22T02:02:41Z")

</div>

Hello.

You should only need the Pololu AVR library. You can download the library and find installation instructions in its [user’s guide](https://www.pololu.com/docs/0J20). Good luck getting it working. If you continue to have problems, please post the errors you’re getting.

- Ben

---

<div class="post-metadata">

### Author: ![epokh](https://forum.pololu.com/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@epokh](https://forum.pololu.com/u/epokh)
#### Post date: [March 23, 2009, 9:05pm UTC](https://forum.pololu.com/t/3pi-spinning-line-follower-example/1156/7 "2009-03-23T21:05:45Z")

</div>

In the attachment there’s the code and makefile to compile and program  
Untar the file and do make or make program from the bash.  
Cheers.  
[4picode.tar](https://forum.pololu.com/uploads/default/original/1X/6065d6913213d55000c8fa879cdb2cf659fa9b4c.tar) (100 KB)

---

<div class="post-metadata">

### Author: ![Andre\_Luis](https://forum.pololu.com/letter_avatar_proxy/v4/letter/a/d6d6ee/32.png) [@Andre\_Luis](https://forum.pololu.com/u/Andre_Luis)
#### Post date: [March 24, 2009, 8:15pm UTC](https://forum.pololu.com/t/3pi-spinning-line-follower-example/1156/8 "2009-03-24T20:15:26Z")

</div>

Hello!  
Thank you everybody for the answers! I can compile the program!  
Thank you!  
Obrigado!

Andre Luis

---

<div class="post-metadata">

### Author: ![shitizgoel](https://forum.pololu.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@shitizgoel](https://forum.pololu.com/u/shitizgoel)
#### Post date: [April 18, 2013, 7:43pm UTC](https://forum.pololu.com/t/3pi-spinning-line-follower-example/1156/9 "2013-04-18T19:43:58Z")

</div>

Could you please write me up a code that makes the 3pi bot to follow a 2.5cm line  
and take turns as in this video

PLEASE PLEASE for god sake help me…  
☹☹😒  
please send me the code on [shitizgoel@yahoo.co.in](mailto:shitizgoel@yahoo.co.in)
