3pi-Linefollower - Demo Code For The Pololu 3pi Robot
3pi-Linefollower - Demo Code For The Pololu 3pi Robot
3pi-Linefollower - Demo Code For The Pololu 3pi Robot
print_from_program_space(demo_name_line2);
delay_ms(1000);
// Display battery voltage and wait for button press
while(!button_is_pressed(BUTTON_B))
{
int bat = read_battery_millivolts();
clear();
print_long(bat);
print("mV");
lcd_goto_xy(0,1);
print("Press B");
delay_ms(100);
}
// Always wait for the button to be released so that 3pi doesn't
// start moving until your hand is away from it.
wait_for_button_release(BUTTON_B);
delay_ms(1000);
// Auto-calibration: turn right and left while calibrating the
// sensors.
for(counter=0;counter<80;counter++)
{
if(counter < 20 || counter >= 60)
set_motors(40,-40);
else
set_motors(-40,40);
// This function records a set of sensor readings and keeps
// track of the minimum and maximum values encountered. The
// IR_EMITTERS_ON argument means that the IR LEDs will be
// turned on during the reading, which is usually what you
// want.
calibrate_line_sensors(IR_EMITTERS_ON);
// Since our counter runs to 80, the total delay will be
// 80*20 = 1600 ms.
delay_ms(20);
}
set_motors(0,0);
// Display calibrated values as a bar graph.
while(!button_is_pressed(BUTTON_B))
{
// Read the sensor values and get the position measurement.
unsigned int position = read_line(sensors,IR_EMITTERS_ON);
// Display the position measurement, which will go from 0
// (when the leftmost sensor is over the line) to 4000 (when
// the rightmost sensor is over the line) on the 3pi, along
// with a bar graph of the sensor readings. This allows you
// to make sure the robot is ready to go.
clear();
print_long(position);
lcd_goto_xy(0,1);
display_readings(sensors);
delay_ms(100);
}
wait_for_button_release(BUTTON_B);
clear();
print("Go!");
// Play music and wait for it to finish before we start driving.
play_from_program_space(go);
while(is_playing());
} // akhir inisialisasi
//cari arah
char cariarah(int kiri,int kanan,int lurus)
{
if(kanan) { return 'R'; }
else if(lurus) { return 'S'; }
else if(kiri) { return 'L'; }
else { return 'B'; }
}
void belok(char arah)
{
//unsigned int sensors[5];
unsigned int delay = 200;
//unsigned int position = read_line(sensors,IR_EMITTERS_ON);
//read_line(sensors,IR_EMITTERS_ON);
if(arah == 'L') {
//Belok kiri
set_motors(-max,max);
delay_ms(delay);
} else if(arah == 'R') {
// Belok kanan
set_motors(max,-max);
delay_ms(delay);
} else if(arah == 'S') {
//lurus aja
} else if(arah == 'B') {
//mbalik
set_motors(max,-max);
delay_ms(2*delay);
}
}
void susur() {
int last_proportional = 0;
long integral=0;
while(1)
{
// Normally, we will be following a line. The code below is
// similar to the 3pi-linefollower-pid example, but the maximum
// speed is turned down to 60 for reliability.
// Get the position of the line.
unsigned int sensors[5];
unsigned int position = read_line(sensors,IR_EMITTERS_ON);
// The "proportional" term should be 0 when we are on the line.
if(sensors[1] < 100 && sensors[2] < 100 && sensors[3] < 100)
{
// There is no line visible ahead, and we didn't see any
// intersection. Must be a dead end.
return;
}
else if(sensors[0] > 200 || sensors[4] > 200)
{
// Found an intersection.
return;
}
}
}
void sederhanakan()
{
switch(arahprint[counterjalur-2])
{
case 'L':
switch(arahprint[counterjalur])
{
case 'L':
arahprint[counterjalur] = '\0';
arahprint[counterjalur-1] = '\0';
counterjalur-=2;
arahprint[counterjalur] =
break;
case 'R':
arahprint[counterjalur] =
arahprint[counterjalur-1]
counterjalur-=2;
arahprint[counterjalur] =
break;
case 'S':
arahprint[counterjalur] =
arahprint[counterjalur-1]
counterjalur-=2;
arahprint[counterjalur] =
break;
}
break;
case 'R':
switch(arahprint[counterjalur])
{
case 'L':
arahprint[counterjalur] =
arahprint[counterjalur-1]
counterjalur-=2;
arahprint[counterjalur] =
break;
case 'R':
arahprint[counterjalur] =
arahprint[counterjalur-1]
counterjalur-=2;
arahprint[counterjalur] =
break;
case 'S':
arahprint[counterjalur] =
arahprint[counterjalur-1]
counterjalur-=2;
arahprint[counterjalur] =
break;
}
break;
case 'S':
switch(arahprint[counterjalur])
{
case 'L':
arahprint[counterjalur] =
arahprint[counterjalur-1]
counterjalur-=2;
arahprint[counterjalur] =
break;
case 'R':
arahprint[counterjalur] =
arahprint[counterjalur-1]
counterjalur-=2;
arahprint[counterjalur] =
break;
case 'S':
arahprint[counterjalur] =
arahprint[counterjalur-1]
counterjalur-=2;
arahprint[counterjalur] =
break;
'S';
'\0';
= '\0';
'B';
'\0';
= '\0';
'R';
'\0';
= '\0';
'B';
'\0';
= '\0';
'S';
'\0';
= '\0';
'L';
'\0';
= '\0';
'R';
'\0';
= '\0';
'L';
'\0';
= '\0';
'B';
}
break;
}
}
char jalurpulang(char arahprint[255]) {
for(int i=0; arahprint[i] != 0; i++) {
switch(arahprint[i]) {
case 'L' :
arahprint[i] = 'R';
break;
case 'R' :
arahprint[i] = 'L';
break;
}
}
return arahprint[255];
}
// This is the main function, where the code starts. All C programs
// must have a main() function defined somewhere.
int main()
{
// set up the 3pi
initialize();
while(1) {
unsigned int sensors[5];
unsigned int kiri=0;
unsigned int kanan=0;
unsigned int lurus=0;
left_led(0);
right_led(0);
susur();
set_motors(50,50);
delay_ms(50);
read_line(sensors,IR_EMITTERS_ON);
if(sensors[0] > 100)
{
// Belok kiri.
kiri=1;
left_led(1);
}
if(sensors[4] > 100)
{
//Belok kanan
kanan=1;
right_led(1);
}
//maju sithik
set_motors(40,40);
delay_ms(200);
read_line(sensors,IR_EMITTERS_ON);
while(1) {
// Beep to show that we finished the maze.
set_motors(0,0);
play(">>a32");
// Wait for the user to press a button, while displaying
// the solution.
while(!button_is_pressed(BUTTON_B))
{
if(get_ms() % 2000 < 1000)
{
clear();
print("Jadi!");
lcd_goto_xy(0,1);
print("Press B");
}
else
clear();
arahprint[jumlahcounter+1] = 0;
print(arahprint);
if(jumlahcounter > 8)
{
lcd_goto_xy(0,1);
print(arahprint+8);
}
delay_ms(30);
}
while(button_is_pressed(BUTTON_B));
delay_ms(1000);
for(int i=0;i<jumlahcounter;i++){
susur();
set_motors(50,50);
delay_ms(50);
set_motors(40,40);
delay_ms(200);
belok(arahprint[i]);
}
susur();
}
}