3pi robot moving around the table without falling off

hello.
im ask to program the 4 QTR-1RC sensors (IR sensors) that is already build in the 3pi robot to move around the table without falling off. but i happen to have codes related to it but isn’t in AVRStudio format. can you help me to convert them to AVRStudio,please?

// Movement Functions
void movement(int movementFrm, int movementTo, unsigned int delayTime, unsigned int wheel)
{
	int i = movementFrm;
	while (i != movementTo) // Acceleralte slowly to stop Backlash
    {
		if (i > movementTo) { i--; }
		else { i++; }
		
		if (wheel == BOTH) { set_motors(i, i); }
		else if (wheel == RIGHT_ONLY) { set_motors(movementFrm, i); }
		else if (wheel == LEFT_ONLY) { set_motors(i, movementFrm); }
		
		delay_ms(delayTime);
    }
	
	if (wheel == BOTH) { set_motors(movementTo, movementTo); }
	else if (wheel == RIGHT_ONLY) { set_motors(movementFrm, movementTo); }
	else if (wheel == LEFT_ONLY) { set_motors(movementTo, movementFrm); }
}
void back_off(int timeForBackOff, int turnValue, int reversalTime)
{
	movement(0, negSpeed, 2, BOTH);
	delay_ms(reversalTime);
	set_motors(negSpeed, (negSpeed + turnValue));
	delay_ms(timeForBackOff);
	movement(negSpeed, 0, 2, BOTH);
}



void initialize()
{
	pololu_3pi_init(2000);
	
//	PololuQTRSensorsRC(pins, 5, 4000, 255);
	
	menu(PRI_MENU);
	
	print("Go!");

}

int main()
{
		
	play_from_program_space(startup);
	initialize();
	
	read_line(sensors,IR_EMITTERS_ON);	// Read the sensor values into the 'sensors' array.
	unsigned int allSensors = (sensors[0] + sensors[1] + sensors[2] + sensors[3] + sensors[4]);
	unsigned int leftSensors = (sensors[0] + sensors[1]);
	unsigned int rightSensors = (sensors[3] + sensors[4]);
	unsigned int turningValue = 0;
	
	if (allSensors <= 0)
	{
		movement(0, speed, 10, BOTH);
		red_led(0);
		green_led(1);
	}
	
	while(1)
	{
		read_line(sensors,IR_EMITTERS_ON);	// Read the sensor values into the 'sensors' array.
		allSensors = (sensors[0] + sensors[1] + sensors[2] + sensors[3] + sensors[4]);
		leftSensors = (sensors[0] + sensors[1]);
		rightSensors = (sensors[3] + sensors[4]);
		turningValue = 0;
     }


// If there is a void
		if (allSensors > 0)
		{
			movement(speed, 0, 2, BOTH);
			
			int a = (largest_sensor_value()); // Outside left sensor is 1, inside is 2
			
			if (mute != TRUE) 
			{
				play_cliff_alert();
			}
			red_led(1);
			green_led(0);
			clear();
			print("Correct.");
			lcd_goto_xy(0, 1);
	
			// If the void's on the left
			if (a < 2)
			{
				a = (a + 1);
				int b = 0;
				if (a == 1) { a = 2; }			// Sets sensor 1 to have a value of 2 and vice versa
				else if (a == 2) { a = 1; }		// for easier manipulation
				
				if (a == 1) { b = (sensors[1]);	} // Increases turningValue if outside sensor has a value
						
				turningValue = ((a * 100) + b);
				back_off(80, turningValue, 100);
				print_long(turningValue);
				set_motors(0, 0);
			}
			
			// If the voids on the right
			else if (a > 2)
			{
				a = (((a - 4) * -1) + 1); // Outside right sensor is 1, inside is 2
				int b = 0;
//				if (a == 1) { b = (sensors[1] * 5);	} // Increases turningValue if inside sensor has a value
								
				turningValue = ((50 * a) + b);
				back_off(100, turningValue, 0);
				print_long(turningValue);
				set_motors(0, 0);
			}

codes that need to convert.docx (18.3 KB)

Hello.

I think you are fundamentally misunderstanding something when you say you need to convert this code to “AVR Studio format”. The only thing that is keeping you from compiling this code in AVR Studio is that it is incomplete. Note that your code is using the Pololu AVR library, so you will need to have this properly installed. The 3pi user’s guide explains how to do this, as does the Pololu AVR library user’s guide. Where did you get this code fragment?

- Ben

those codes are taken from one of the user who post similiar problems about ‘3pi-Additional QTR-1RC sensors help’. in order to read IR sensors value on the Lcd screen, does it program the same way as sharp distance sensors(digital sensors)?

No, the sensors have different output formats.

thanks alot for helping me. lastly i would like to ask if you can tell me a simple program codes where the IR sensor sense the edge of the table that causes it to stop.?

What you’re asking is not necessarily simple, and it sounds like you are basically looking for someone to do your assignment for you. I’m happy to help you if you get stuck or are confused about something, but that requires you to put some effort into this as well. I suggest you start by trying to understand the sample 3pi programs that are part of the Pololu AVR library. If you have specific questions, please let me know.

To help get you started, when the sensors are over the table, they will see more reflected IR then when they are not over the edge, so you should drive very slowly until the reflected IR detected by your sensors drops sharply, at which point you are probably at the edge of the table should stop. Another option might be to put a black border around the rim of the table and detect when the robot is over the border (by the same mechanism: the reflected IR should be much lower when the sensors are over the black).

By the way, since it is unlikely your program will work the first time you test it, please be ready to catch the 3pi before it falls off the table (or put a lot of pillows/cushions below the edge of the table)!

- Ben

oke, can :slight_smile: thanks alot for explaining to me! i’ll keep trying! really thanks alot!~ :smiley:

I have tried this and would like to share some experience.

  • Calibration: the 3pi has to learn the difference between the surface and the edge. Instead of turn left and right as in the line following, I let it go back from the edge (all the sensors are off the edge) and go ahead back to the original position. It is better if your surface has a bright colour.
  • Read the sensors and react. Before turning, it should back off about 1 inch to make sure the wheels do not cross the edge when it turns. For the same reason, make a sharp turn, i.e. set_motors(-20,20) to turn left.
  • However, I haven’t solved 2 issues:
  1. The distance between the wheels are bigger than the distance between the far left and far right sensors. When the 3pi approach the edge in a small angle (close to parallel with the edge), the wheel fall off before the sensor can see the edge. To be safe, I do not try physical edges, just use some pieces of paper on a dark table.
  2. No sensor in the rear: when moving backward (because it sees an ‘edge’ ahead) it may ‘fall’ to the back. This more likely to happens when it goes to the corners, where 2 edges are close to each other. It may be solve by turning around to ‘look over the soulder’ before reversing. But it does not look very nice :slight_smile: .

Thanks for sharing your experiences. I think there really isn’t much of a way to get around the limitations you mention without adding sensors that extend past the wheels.

- Ben

I made a demo video: http://www.youtube.com/watch?v=T3pS6sMEOFg
I used white paper to simulate the ‘table’ on a dark surface.