Library for Pololu m3pi line-following robot. Implements the serial slave commands.
Dependents: 3pi_Example_2 3pi_Lab1_Task2_Example1 3pi_Lab2_Task1_Example1 3pi_Line_Follow ... more
Diff: m3pi.cpp
- Revision:
- 1:5523d6d1feec
- Parent:
- 0:56320ef879a6
- Child:
- 2:26bf14f4dc84
--- a/m3pi.cpp Tue Apr 04 08:08:16 2017 +0000 +++ b/m3pi.cpp Wed Apr 05 10:25:42 2017 +0000 @@ -48,27 +48,35 @@ void m3pi::get_raw_values(unsigned int *values) { - _serial->putc(0x86); + char vals[10]; // array to receive 10 byte return message + _serial->putc(0x86); // send command - for (int i=0; i<5; i++) { - char lsb = _serial->getc(); - char msb = _serial->getc(); - unsigned int value = (msb<<8 | lsb); - values[i]=value; + int n=0; + while ( _serial->readable() ) { // keep looping while data on rx line + vals[n] = _serial->getc(); // read into array + n++; // increment index + } + + for(int i=0; i<5; i++) { // construct the 2-byte values + values[i] = (vals[2*i+1] << 8) | vals[2*i]; } } void m3pi::get_calibrated_values(unsigned int *values) { - _serial->putc(0x87); + char vals[10]; // array to receive 10 byte return message + _serial->putc(0x87); // send command - for (int i=0; i<5; i++) { - char lsb = _serial->getc(); - char msb = _serial->getc(); - unsigned int value = (msb<<8 | lsb); - values[i]=value; + int n=0; + while ( _serial->readable() ) { // keep looping while data on rx line + vals[n] = _serial->getc(); // read into array + n++; // increment index } + for(int i=0; i<5; i++) { // construct the 2-byte values + values[i] = (vals[2*i+1] << 8) | vals[2*i]; + } + } float m3pi::get_trimpot_value() @@ -135,7 +143,7 @@ lcd_goto_xy(0,1); lcd_print("Sensors ",8); - spin_right(0.1); + spin_right(0.2); char led_val = 0; Timer timer; @@ -149,7 +157,7 @@ } _serial->putc(0xB4); - wait_ms(5); + wait_ms(25); } timer.stop(); @@ -159,9 +167,7 @@ lcd_clear(); lcd_goto_xy(0,0); - lcd_print("Place on",8); - lcd_goto_xy(0,1); - lcd_print(" line ",8); + lcd_print(" Done ",8); while( read_button() ) { // loop while waiting for button to be press