Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: 3pi_Example_2 3pi_Lab1_Task2_Example1 3pi_Lab2_Task1_Example1 3pi_Line_Follow ... more
Diff: m3pi.cpp
- Revision:
- 3:5015bc2d1cf8
- Parent:
- 2:26bf14f4dc84
- Child:
- 4:0abe81f5d9fd
--- a/m3pi.cpp Fri Apr 07 18:22:35 2017 +0000 +++ b/m3pi.cpp Thu May 25 10:18:43 2017 +0000 @@ -6,41 +6,25 @@ m3pi::m3pi() { _serial = new Serial(p9,p10); - _reset = new DigitalOut(p23); - _button = new DigitalIn(p21); - _leds = new BusOut(p20,p19,p18,p17,p16,p15,p14,p13); - + _reset = new DigitalOut(p8); _last_line_position = 0.0; - } m3pi::~m3pi() { delete _serial; delete _reset; - delete _button; - delete _leds; + } /////////////////////////////// public methods ///////////////////////////////// -void m3pi::write_leds(int val) -{ - // check within limits - val = val > 255 ? 255 : val; - val = val < 0 ? 0 : val; - - _leds->write(val); -} - void m3pi::init() { _serial->baud(115200); reset(); // hard rest of 3pi stop(); // stop motors lcd_clear(); // clear LCD - write_leds(0); // turn off LEDs - _button->mode(PullUp); // turn pull-up on } /////////////////////////////// serial slave commands //////////////////////////////// @@ -122,73 +106,12 @@ void m3pi::calibrate() { - reset_calibration(); - - lcd_goto_xy(0,0); - lcd_print("Place on",8); - lcd_goto_xy(0,1); - lcd_print(" line ",8); - - wait(0.5); - - lcd_clear(); - lcd_goto_xy(0,0); - lcd_print(" Press ",8); - lcd_goto_xy(0,1); - lcd_print("to begin",8); - - while( read_button() ) { - // loop while waiting for button to be press - } - - wait(0.5); - - lcd_clear(); - lcd_goto_xy(0,0); - lcd_print("Reading ",8); - lcd_goto_xy(0,1); - lcd_print("Sensors ",8); - - spin_right(0.2); - - char led_val = 0; - Timer timer; - timer.start(); - - while (timer.read() < 5.0) { - - write_leds(led_val++); - - if (led_val > 255) { - led_val = 0; - } - - _serial->putc(0xB4); - wait_ms(25); - } - - timer.stop(); - - write_leds(255); - stop(); - - lcd_clear(); - lcd_goto_xy(0,0); - lcd_print(" Done ",8); - - while( read_button() ) { - // loop while waiting for button to be press - } - - lcd_clear(); - write_leds(0); - wait(0.5); + _serial->putc(0xB4); } void m3pi::reset_calibration() { _serial->putc(0xB5); - wait_ms(50); } float m3pi::get_line_position() @@ -225,26 +148,21 @@ _serial->putc(0xB9); _serial->putc(x); _serial->putc(y); - } void m3pi::auto_calibrate() { - reset_calibration(); // clear previous calibration - _serial->putc(0xBA); - write_leds(0xFF); // LEDs on - while(1) { + + while(1) { // wait for serial response if (_serial->readable()) { break; } } - write_leds(0); // LEDs off } /////////////////////////////// motor methods //////////////////////////////// - void m3pi::left_motor(float speed) { // check within bounds @@ -288,7 +206,6 @@ right_motor(right_speed); } - void m3pi::stop() { left_motor(0.0); @@ -335,12 +252,6 @@ //////////////////////////////////////////////////////////////////////////////// - -int m3pi::read_button() -{ - return _button->read(); -} - void m3pi::display_battery_voltage(int x,int y) { float voltage = get_battery_voltage(); @@ -381,7 +292,6 @@ unsigned int m3pi::get_sensor_array_value(unsigned int values[]) { - unsigned int value = 0; // loop through each bit, starting from PC4 @@ -411,13 +321,11 @@ float position = (int(value) - 2000)/2000.0; float is_on_line = false; - write_leds(0x0); // loop through and check if any sensor reading is above the threshold for (int i = 0; i<5; i++) { if (values[i] > 500) { is_on_line = true; - write_leds(0xFF); } } @@ -435,8 +343,8 @@ void m3pi::reset() { // pulse the reset line (active-high) + _reset->write(1); + wait_ms(100); _reset->write(0); wait_ms(100); - _reset->write(1); - wait_ms(100); } \ No newline at end of file