From dff0308b1b9d197f1a957d634b587131eb5164f1 Mon Sep 17 00:00:00 2001 From: Jacob Perron Date: Mon, 2 Sep 2019 12:26:09 -0700 Subject: [PATCH] Cleanup examples Signed-off-by: Jacob Perron --- examples/battery_level.cpp | 23 +++++-------------- examples/bumpers.cpp | 47 +++++++++++++++----------------------- examples/cliffs.cpp | 26 +++++++-------------- examples/drive_circle.cpp | 24 +++++-------------- examples/leds.cpp | 33 +++++++++----------------- examples/packets.cpp | 24 ++++--------------- examples/play_song.cpp | 18 ++++++--------- examples/wheeldrop.cpp | 27 +++++++--------------- 8 files changed, 69 insertions(+), 153 deletions(-) diff --git a/examples/battery_level.cpp b/examples/battery_level.cpp index 49fd70d..24a4f54 100644 --- a/examples/battery_level.cpp +++ b/examples/battery_level.cpp @@ -47,10 +47,10 @@ int main(int argc, char** argv) { } // Construct robot object - create::Create* robot = new create::Create(model); + create::Create robot(model); // Connect to robot - if (robot->connect(port, baud)) + if (robot.connect(port, baud)) std::cout << "Connected to robot" << std::endl; else { std::cout << "Failed to connect to robot on port " << port.c_str() << std::endl; @@ -58,17 +58,15 @@ int main(int argc, char** argv) { } // Switch to Full mode - robot->setMode(create::MODE_FULL); - - std::cout << std::endl << "Press center 'Clean' button to disconnect and end program" << std::endl; + robot.setMode(create::MODE_FULL); // Get battery capacity (max charge) - const float battery_capacity = robot->getBatteryCapacity(); + const float battery_capacity = robot.getBatteryCapacity(); float battery_charge = 0.0f; - while (!robot->isCleanButtonPressed()) { + while (true) { // Get battery charge - battery_charge = robot->getBatteryCharge(); + battery_charge = robot.getBatteryCharge(); // Print battery percentage std::cout << "\rBattery level: " << (battery_charge / battery_capacity) * 100.0 << "%"; @@ -76,14 +74,5 @@ int main(int argc, char** argv) { usleep(100000); // 10 Hz } - std::cout << std::endl; - - // Call disconnect to avoid leaving robot in Full mode - robot->disconnect(); - - // Clean up - delete robot; - - std::cout << "Bye!" << std::endl; return 0; } diff --git a/examples/bumpers.cpp b/examples/bumpers.cpp index 52c6877..bb9b432 100644 --- a/examples/bumpers.cpp +++ b/examples/bumpers.cpp @@ -47,10 +47,10 @@ int main(int argc, char** argv) { } // Construct robot object - create::Create* robot = new create::Create(model); + create::Create robot(model); // Connect to robot - if (robot->connect(port, baud)) + if (robot.connect(port, baud)) std::cout << "Connected to robot" << std::endl; else { std::cout << "Failed to connect to robot on port " << port.c_str() << std::endl; @@ -58,37 +58,35 @@ int main(int argc, char** argv) { } // Switch to Full mode - robot->setMode(create::MODE_FULL); - - std::cout << std::endl << "Press center 'Clean' button to disconnect and end program" << std::endl; + robot.setMode(create::MODE_FULL); uint16_t light_signals[6]; bool light_bumpers[6]; bool contact_bumpers[2]; - while (!robot->isCleanButtonPressed()) { + while (true) { // Get light sensor data (only available for Create 2 or later robots) if (model == create::RobotModel::CREATE_2) { // Get raw light signal values - light_signals[0] = robot->getLightSignalLeft(); - light_signals[1] = robot->getLightSignalFrontLeft(); - light_signals[2] = robot->getLightSignalCenterLeft(); - light_signals[3] = robot->getLightSignalCenterRight(); - light_signals[4] = robot->getLightSignalFrontRight(); - light_signals[5] = robot->getLightSignalRight(); + light_signals[0] = robot.getLightSignalLeft(); + light_signals[1] = robot.getLightSignalFrontLeft(); + light_signals[2] = robot.getLightSignalCenterLeft(); + light_signals[3] = robot.getLightSignalCenterRight(); + light_signals[4] = robot.getLightSignalFrontRight(); + light_signals[5] = robot.getLightSignalRight(); // Get obstacle data from light sensors (true/false) - light_bumpers[0] = robot->isLightBumperLeft(); - light_bumpers[1] = robot->isLightBumperFrontLeft(); - light_bumpers[2] = robot->isLightBumperCenterLeft(); - light_bumpers[3] = robot->isLightBumperCenterRight(); - light_bumpers[4] = robot->isLightBumperFrontRight(); - light_bumpers[5] = robot->isLightBumperRight(); + light_bumpers[0] = robot.isLightBumperLeft(); + light_bumpers[1] = robot.isLightBumperFrontLeft(); + light_bumpers[2] = robot.isLightBumperCenterLeft(); + light_bumpers[3] = robot.isLightBumperCenterRight(); + light_bumpers[4] = robot.isLightBumperFrontRight(); + light_bumpers[5] = robot.isLightBumperRight(); } // Get state of bumpers - contact_bumpers[0] = robot->isLeftBumper(); - contact_bumpers[1] = robot->isRightBumper(); + contact_bumpers[0] = robot.isLeftBumper(); + contact_bumpers[1] = robot.isRightBumper(); // Print signals from left to right std::cout << "[ " << light_signals[0] << " , " @@ -113,14 +111,5 @@ int main(int argc, char** argv) { usleep(100000); // 10 Hz } - std::cout << std::endl; - - // Call disconnect to avoid leaving robot in Full mode - robot->disconnect(); - - // Clean up - delete robot; - - std::cout << "Bye!" << std::endl; return 0; } diff --git a/examples/cliffs.cpp b/examples/cliffs.cpp index ff0738a..0f004cc 100644 --- a/examples/cliffs.cpp +++ b/examples/cliffs.cpp @@ -47,10 +47,10 @@ int main(int argc, char** argv) { } // Construct robot object - create::Create* robot = new create::Create(model); + create::Create robot(model); // Connect to robot - if (robot->connect(port, baud)) { + if (robot.connect(port, baud)) { std::cout << "Connected to robot" << std::endl; } else { @@ -59,16 +59,14 @@ int main(int argc, char** argv) { } // Switch to Full mode - robot->setMode(create::MODE_FULL); + robot.setMode(create::MODE_FULL); - std::cout << std::endl << "Press center 'Clean' button to disconnect and end program" << std::endl; - - while (!robot->isCleanButtonPressed()) { + while (true) { // Get cliff status - const bool cliff_left = robot->isCliffLeft(); - const bool cliff_front_left = robot->isCliffFrontLeft(); - const bool cliff_front_right = robot->isCliffFrontRight(); - const bool cliff_right = robot->isCliffRight(); + const bool cliff_left = robot.isCliffLeft(); + const bool cliff_front_left = robot.isCliffFrontLeft(); + const bool cliff_front_right = robot.isCliffFrontRight(); + const bool cliff_right = robot.isCliffRight(); // Print status std::cout << "\rCliffs (left to right): [ " << @@ -84,13 +82,5 @@ int main(int argc, char** argv) { usleep(10000); // 10 Hz } - std::cout << std::endl; - - // Call disconnect to avoid leaving robot in Full mode - robot->disconnect(); - - // Clean up - delete robot; - return 0; } diff --git a/examples/drive_circle.cpp b/examples/drive_circle.cpp index f7e1fcf..5c4c8b9 100644 --- a/examples/drive_circle.cpp +++ b/examples/drive_circle.cpp @@ -48,10 +48,10 @@ int main(int argc, char** argv) { } // Construct robot object - create::Create* robot = new create::Create(model); + create::Create robot(model); // Connect to robot - if (robot->connect(port, baud)) + if (robot.connect(port, baud)) std::cout << "Connected to robot" << std::endl; else { std::cout << "Failed to connect to robot on port " << port.c_str() << std::endl; @@ -59,19 +59,17 @@ int main(int argc, char** argv) { } // Switch to Full mode - robot->setMode(create::MODE_FULL); - - std::cout << std::endl << "Press center 'Clean' button to disconnect and end program" << std::endl; + robot.setMode(create::MODE_FULL); // There's a delay between switching modes and when the robot will accept drive commands usleep(100000); // Command robot to drive a radius of 0.15 metres at 0.2 m/s - robot->driveRadius(0.2, 0.15); + robot.driveRadius(0.2, 0.15); - while (!robot->isCleanButtonPressed()) { + while (true) { // Get robot odometry and print - const create::Pose pose = robot->getPose(); + const create::Pose pose = robot.getPose(); std::cout << std::fixed << std::setprecision(2) << "\rOdometry (x, y, yaw): (" << pose.x << ", " << pose.y << ", " << pose.yaw << ") "; @@ -79,15 +77,5 @@ int main(int argc, char** argv) { usleep(10000); // 10 Hz } - std::cout << std::endl; - - // Call disconnect to avoid leaving robot in Full mode - // Also, this consequently stops the robot from moving - robot->disconnect(); - - // Clean up - delete robot; - - std::cout << "Bye!" << std::endl; return 0; } diff --git a/examples/leds.cpp b/examples/leds.cpp index 31a542d..e4c566f 100644 --- a/examples/leds.cpp +++ b/examples/leds.cpp @@ -47,10 +47,10 @@ int main(int argc, char** argv) { } // Construct robot object - create::Create* robot = new create::Create(model); + create::Create robot(model); // Connect to robot - if (robot->connect(port, baud)) + if (robot.connect(port, baud)) std::cout << "Connected to robot" << std::endl; else { std::cout << "Failed to connect to robot on port " << port.c_str() << std::endl; @@ -58,29 +58,27 @@ int main(int argc, char** argv) { } // Switch to Full mode - robot->setMode(create::MODE_FULL); - - std::cout << std::endl << "Press center 'Clean' button to disconnect and end program" << std::endl; + robot.setMode(create::MODE_FULL); bool enable_check_robot_led = true; bool enable_debris_led = false; bool enable_dock_led = true; bool enable_spot_led = false; - uint8_t power_led = 0; + uint8_t power_led = 0; char digit_buffer[5]; - while (!robot->isCleanButtonPressed()) { + while (true) { // Set LEDs - robot->enableCheckRobotLED(enable_check_robot_led); - robot->enableDebrisLED(enable_debris_led); - robot->enableDockLED(enable_dock_led); - robot->enableSpotLED(enable_spot_led); - robot->setPowerLED(power_led); + robot.enableCheckRobotLED(enable_check_robot_led); + robot.enableDebrisLED(enable_debris_led); + robot.enableDockLED(enable_dock_led); + robot.enableSpotLED(enable_spot_led); + robot.setPowerLED(power_led); // Set 7-segment displays const int len = sprintf(digit_buffer, "%d", power_led); for (int i = len; i < 4; i++) digit_buffer[i] = ' '; - robot->setDigitsASCII(digit_buffer[0], digit_buffer[1], digit_buffer[2], digit_buffer[3]); + robot.setDigitsASCII(digit_buffer[0], digit_buffer[1], digit_buffer[2], digit_buffer[3]); // Update LED values enable_check_robot_led = !enable_check_robot_led; @@ -92,14 +90,5 @@ int main(int argc, char** argv) { usleep(250000); // 5 Hz } - std::cout << std::endl; - - // Call disconnect to avoid leaving robot in Full mode - robot->disconnect(); - - // Clean up - delete robot; - - std::cout << "Bye!" << std::endl; return 0; } diff --git a/examples/packets.cpp b/examples/packets.cpp index 4e109c2..d367487 100644 --- a/examples/packets.cpp +++ b/examples/packets.cpp @@ -47,25 +47,20 @@ int main(int argc, char** argv) { } // Construct robot object - create::Create* robot = new create::Create(model); + create::Create robot(model); // Connect to robot - if (robot->connect(port, baud)) + if (robot.connect(port, baud)) std::cout << "Connected to robot" << std::endl; else { std::cout << "Failed to connect to robot on port " << port.c_str() << std::endl; return 1; } - // Switch to Full mode - // robot->setMode(create::MODE_FULL); - - std::cout << std::endl << "Press center 'Clean' button to disconnect and end program" << std::endl; - - while (!robot->isCleanButtonPressed()) { + while (true) { // Get serial packet statistics - const uint64_t total_packets = robot->getTotalPackets(); - const uint64_t num_corrupt_packets = robot->getNumCorruptPackets(); + const uint64_t total_packets = robot.getTotalPackets(); + const uint64_t num_corrupt_packets = robot.getNumCorruptPackets(); // Print packet stats std::cout << "\rTotal packets: " << total_packets << " Corrupt packets: " << num_corrupt_packets; @@ -73,14 +68,5 @@ int main(int argc, char** argv) { usleep(100000); // 10 Hz } - std::cout << std::endl; - - // Call disconnect to avoid leaving robot in Full mode - robot->disconnect(); - - // Clean up - delete robot; - - std::cout << "Bye!" << std::endl; return 0; } diff --git a/examples/play_song.cpp b/examples/play_song.cpp index 979ef84..61132b9 100644 --- a/examples/play_song.cpp +++ b/examples/play_song.cpp @@ -47,10 +47,10 @@ int main(int argc, char** argv) { } // Construct robot object - create::Create* robot = new create::Create(model); + create::Create robot(model); // Connect to robot - if (robot->connect(port, baud)) + if (robot.connect(port, baud)) std::cout << "Connected to robot" << std::endl; else { std::cout << "Failed to connect to robot on port " << port.c_str() << std::endl; @@ -58,7 +58,7 @@ int main(int argc, char** argv) { } // Switch to Full mode - robot->setMode(create::MODE_FULL); + robot.setMode(create::MODE_FULL); // Useful note defintions const uint8_t G = 55; @@ -66,14 +66,14 @@ int main(int argc, char** argv) { const uint8_t DS = 51; const float half = 1.0f; const float quarter = 0.5f; - const float dotted_eigth = 0.375f; + const float dotted_eigth = 0.375f; const float sixteenth = 0.125f; // Define a song const uint8_t song_len = 9; const uint8_t notes[song_len] = { G, G, G, DS, AS, G, DS, AS, G }; const float durations[song_len] = { quarter, quarter, quarter, dotted_eigth, sixteenth, quarter, dotted_eigth, sixteenth, half }; - robot->defineSong(0, song_len, notes, durations); + robot.defineSong(0, song_len, notes, durations); // Sleep to provide time for song to register usleep(1000000); @@ -81,17 +81,13 @@ int main(int argc, char** argv) { std::cout << "Playing a song!" << std::endl; // Request to play the song we just defined - robot->playSong(0); + robot.playSong(0); // Expect the song to take about four seconds usleep(4500000); // Call disconnect to avoid leaving robot in Full mode - robot->disconnect(); + robot.disconnect(); - // Clean up - delete robot; - - std::cout << "Bye!" << std::endl; return 0; } diff --git a/examples/wheeldrop.cpp b/examples/wheeldrop.cpp index 59e02e6..5e0bf66 100644 --- a/examples/wheeldrop.cpp +++ b/examples/wheeldrop.cpp @@ -47,10 +47,10 @@ int main(int argc, char** argv) { } // Construct robot object - create::Create* robot = new create::Create(model); + create::Create robot(model); // Connect to robot - if (robot->connect(port, baud)) + if (robot.connect(port, baud)) std::cout << "Connected to robot" << std::endl; else { std::cout << "Failed to connect to robot on port " << port.c_str() << std::endl; @@ -58,14 +58,12 @@ int main(int argc, char** argv) { } // Switch to Full mode - robot->setMode(create::MODE_FULL); + robot.setMode(create::MODE_FULL); - std::cout << std::endl << "Press center 'Clean' button to disconnect and end program" << std::endl; - - while (!robot->isCleanButtonPressed()) { + while (true) { // Get wheeldrop status - const bool wheeldrop_left = robot->isLeftWheeldrop(); - const bool wheeldrop_right = robot->isRightWheeldrop(); + const bool wheeldrop_left = robot.isLeftWheeldrop(); + const bool wheeldrop_right = robot.isRightWheeldrop(); // Print status std::cout << "\rWheeldrop status (left and right): [ " << @@ -76,21 +74,12 @@ int main(int argc, char** argv) { // If dropped, then make light red if (wheeldrop_left || wheeldrop_right) - robot->setPowerLED(255); // Red + robot.setPowerLED(255); // Red else - robot->setPowerLED(0); // Green + robot.setPowerLED(0); // Green usleep(10000); // 10 Hz } - std::cout << std::endl; - - // Call disconnect to avoid leaving robot in Full mode - robot->disconnect(); - - // Clean up - delete robot; - - std::cout << "Bye!" << std::endl; return 0; }