diff --git a/CMakeLists.txt b/CMakeLists.txt index 2eeb4a7..487e846 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -51,23 +51,24 @@ target_link_libraries(${LIBRARY_NAME} ) # Declare example executables -add_executable(create_demo examples/create_demo.cpp) -add_executable(bumper_example examples/bumper_example.cpp) -add_executable(odom_example examples/odom_example.cpp) +set(EXAMPLES + battery_level + bumpers + drive_circle + leds + packets + play_song + wheeldrop +) -# Specify libraries to link executable targets against -target_link_libraries(create_demo - ${Boost_LIBRARIES} - ${LIBRARY_NAME} -) -target_link_libraries(bumper_example - ${Boost_LIBRARIES} - ${LIBRARY_NAME} -) -target_link_libraries(odom_example - ${Boost_LIBRARIES} - ${LIBRARY_NAME} -) +foreach(EXAMPLE ${EXAMPLES}) + add_executable(${EXAMPLE} examples/${EXAMPLE}.cpp) + + target_link_libraries(${EXAMPLE} + ${Boost_LIBRARIES} + ${LIBRARY_NAME} + ) +endforeach() ################# # Configuration # diff --git a/examples/battery_level.cpp b/examples/battery_level.cpp new file mode 100644 index 0000000..49fd70d --- /dev/null +++ b/examples/battery_level.cpp @@ -0,0 +1,89 @@ +/** +Software License Agreement (BSD) + +\file battery_level.cpp +\authors Jacob Perron +\copyright Copyright (c) 2018, Autonomy Lab (Simon Fraser University), All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of Autonomy Lab nor the names of its contributors may + be used to endorse or promote products derived from this software without + specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +*/ +#include "create/create.h" + +#include + +int main(int argc, char** argv) { + // Select robot. Assume Create 2 unless argument says otherwise + create::RobotModel model = create::RobotModel::CREATE_2; + std::string port = "/dev/ttyUSB0"; + int baud = 115200; + if (argc > 1 && std::string(argv[1]) == "create1") { + model = create::RobotModel::CREATE_1; + baud = 57600; + std::cout << "Running driver for Create 1" << std::endl; + } + else { + std::cout << "Running driver for Create 2" << std::endl; + } + + // Construct robot object + create::Create* robot = new create::Create(model); + + // Connect to robot + 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; + + // Get battery capacity (max charge) + const float battery_capacity = robot->getBatteryCapacity(); + + float battery_charge = 0.0f; + while (!robot->isCleanButtonPressed()) { + // Get battery charge + battery_charge = robot->getBatteryCharge(); + + // Print battery percentage + std::cout << "\rBattery level: " << (battery_charge / battery_capacity) * 100.0 << "%"; + + 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/bumper_example.cpp b/examples/bumper_example.cpp deleted file mode 100644 index 11e0ab8..0000000 --- a/examples/bumper_example.cpp +++ /dev/null @@ -1,73 +0,0 @@ -#include "create/create.h" - -int main(int argc, char** argv) { - std::string port = "/dev/ttyUSB0"; - int baud = 115200; - create::RobotModel model = create::RobotModel::CREATE_2; - - create::Create* robot = new create::Create(model); - - // Attempt to connect to Create - if (robot->connect(port, baud)) - std::cout << "Successfully connected to Create" << std::endl; - else { - std::cout << "Failed to connect to Create on port " << port.c_str() << std::endl; - return 1; - } - - robot->setMode(create::MODE_FULL); - - uint16_t signals[6]; - bool contact_bumpers[2]; - bool light_bumpers[6]; - - // Stop program when clean button is pressed - while (!robot->isCleanButtonPressed()) { - signals[0] = robot->getLightSignalLeft(); - signals[1] = robot->getLightSignalFrontLeft(); - signals[2] = robot->getLightSignalCenterLeft(); - signals[3] = robot->getLightSignalCenterRight(); - signals[4] = robot->getLightSignalFrontRight(); - signals[5] = robot->getLightSignalRight(); - - contact_bumpers[0] = robot->isLeftBumper(); - contact_bumpers[1] = robot->isRightBumper(); - - 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(); - - // print signals from left to right - std::cout << "[ " << signals[0] << " , " - << signals[1] << " , " - << signals[2] << " , " - << signals[3] << " , " - << signals[4] << " , " - << signals[5] - << " ]" << std::endl; - std::cout << "[ " << light_bumpers[0] << " , " - << light_bumpers[1] << " , " - << light_bumpers[2] << " , " - << light_bumpers[3] << " , " - << light_bumpers[4] << " , " - << light_bumpers[5] - << " ]" << std::endl; - std::cout << "[ " << contact_bumpers[0] << " , " - << contact_bumpers[1] - << " ]" << std::endl; - std::cout << std::endl; - - usleep(1000 * 100); //10hz - } - - std::cout << "Stopping Create" << std::endl; - - // Disconnect from robot - robot->disconnect(); - delete robot; - - return 0; -} diff --git a/examples/bumpers.cpp b/examples/bumpers.cpp new file mode 100644 index 0000000..52c6877 --- /dev/null +++ b/examples/bumpers.cpp @@ -0,0 +1,126 @@ +/** +Software License Agreement (BSD) + +\file bumpers.cpp +\authors Jacob Perron +\copyright Copyright (c) 2018, Autonomy Lab (Simon Fraser University), All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of Autonomy Lab nor the names of its contributors may + be used to endorse or promote products derived from this software without + specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +*/ +#include "create/create.h" + +#include + +int main(int argc, char** argv) { + // Select robot. Assume Create 2 unless argument says otherwise + create::RobotModel model = create::RobotModel::CREATE_2; + std::string port = "/dev/ttyUSB0"; + int baud = 115200; + if (argc > 1 && std::string(argv[1]) == "create1") { + model = create::RobotModel::CREATE_1; + baud = 57600; + std::cout << "Running driver for Create 1" << std::endl; + } + else { + std::cout << "Running driver for Create 2" << std::endl; + } + + // Construct robot object + create::Create* robot = new create::Create(model); + + // Connect to robot + 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; + + uint16_t light_signals[6]; + bool light_bumpers[6]; + bool contact_bumpers[2]; + + while (!robot->isCleanButtonPressed()) { + // 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(); + + // 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(); + } + + // Get state of bumpers + contact_bumpers[0] = robot->isLeftBumper(); + contact_bumpers[1] = robot->isRightBumper(); + + // Print signals from left to right + std::cout << "[ " << light_signals[0] << " , " + << light_signals[1] << " , " + << light_signals[2] << " , " + << light_signals[3] << " , " + << light_signals[4] << " , " + << light_signals[5] + << " ]" << std::endl; + std::cout << "[ " << light_bumpers[0] << " , " + << light_bumpers[1] << " , " + << light_bumpers[2] << " , " + << light_bumpers[3] << " , " + << light_bumpers[4] << " , " + << light_bumpers[5] + << " ]" << std::endl; + std::cout << "[ " << contact_bumpers[0] << " , " + << contact_bumpers[1] + << " ]" << std::endl; + std::cout << std::endl; + + 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/create_demo.cpp b/examples/create_demo.cpp deleted file mode 100644 index 243eaa6..0000000 --- a/examples/create_demo.cpp +++ /dev/null @@ -1,133 +0,0 @@ -#include "create/create.h" - -create::Create* robot; - -int main(int argc, char** argv) { - std::string port = "/dev/ttyUSB0"; - int baud = 115200; - create::RobotModel model = create::RobotModel::CREATE_2; - - if (argc > 1 && std::string(argv[1]) == "create1") { - model = create::RobotModel::CREATE_1; - baud = 57600; - std::cout << "1st generation Create selected" << std::endl; - } - - robot = new create::Create(model); - - // Attempt to connect to Create - if (robot->connect(port, baud)) - std::cout << "Successfully connected to Create" << std::endl; - else { - std::cout << "Failed to connect to Create on port " << port.c_str() << std::endl; - return 1; - } - - // Note: Some functionality does not work as expected in Safe mode - robot->setMode(create::MODE_FULL); - - std::cout << "battery level: " << - robot->getBatteryCharge() / (float) robot->getBatteryCapacity() * 100.0 << "%" << std::endl; - - bool drive = false; - - // Make a song - //uint8_t songLength = 16; - //uint8_t notes[16] = { 67, 67, 66, 66, 65, 65, 66, 66, - // 67, 67, 66, 66, 65, 65, 66, 66 }; - //float durations[songLength]; - //for (int i = 0; i < songLength; i++) { - // durations[i] = 0.25; - //} - //robot->defineSong(0, songLength, notes, durations); - //usleep(1000000); - //robot->playSong(0); - - // Quit when center "Clean" button pressed - while (!robot->isCleanButtonPressed()) { - // Check for button presses - if (robot->isDayButtonPressed()) - std::cout << "day button press" << std::endl; - if (robot->isMinButtonPressed()) - std::cout << "min button press" << std::endl; - if (robot->isDockButtonPressed()) { - std::cout << "dock button press" << std::endl; - robot->enableCheckRobotLED(false); - } - if (robot->isSpotButtonPressed()) { - std::cout << "spot button press" << std::endl; - robot->enableCheckRobotLED(true); - } - if (robot->isHourButtonPressed()) { - std::cout << "hour button press" << std::endl; - drive = !drive; - } - - // Check for wheeldrop or cliffs - if (robot->isWheeldrop() || robot->isCliff()) { - drive = false; - robot->setPowerLED(255); - } - - // If everything is ok, drive forward using IR's to avoid obstacles - if (drive) { - robot->setPowerLED(0); // green - if (robot->isLightBumperLeft() || - robot->isLightBumperFrontLeft() || - robot->isLightBumperCenterLeft()) { - // turn right - robot->drive(0.1, -1.0); - robot->setDigitsASCII('-','-','-',']'); - } - else if (robot->isLightBumperRight() || - robot->isLightBumperFrontRight() || - robot->isLightBumperCenterRight()) { - // turn left - robot->drive(0.1, 1.0); - robot->setDigitsASCII('[','-','-','-'); - } - else { - // drive straight - robot->drive(0.1, 0.0); - robot->setDigitsASCII(' ','^','^',' '); - } - } - else { // drive == false - // stop moving - robot->drive(0, 0); - robot->setDigitsASCII('S','T','O','P'); - } - - // Turn on blue 'debris' light if moving forward - if (robot->isMovingForward()) { - robot->enableDebrisLED(true); - } - else { - robot->enableDebrisLED(false); - } - - // Check bumpers - if (robot->isLeftBumper()) { - std::cout << "left bump detected!" << std::endl; - } - if (robot->isRightBumper()) { - std::cout << "right bump detected!" << std::endl; - } - - usleep(1000 * 100); //10hz - } - - std::cout << "Stopping Create." << std::endl; - - // Turn off lights - robot->setPowerLED(0, 0); - robot->enableDebrisLED(false); - robot->enableCheckRobotLED(false); - robot->setDigitsASCII(' ', ' ', ' ', ' '); - - // Make sure to disconnect to clean up - robot->disconnect(); - delete robot; - - return 0; -} diff --git a/examples/drive_circle.cpp b/examples/drive_circle.cpp new file mode 100644 index 0000000..f7e1fcf --- /dev/null +++ b/examples/drive_circle.cpp @@ -0,0 +1,93 @@ +/** +Software License Agreement (BSD) + +\file drive_circle.cpp +\authors Jacob Perron +\copyright Copyright (c) 2018, Autonomy Lab (Simon Fraser University), All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of Autonomy Lab nor the names of its contributors may + be used to endorse or promote products derived from this software without + specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +*/ +#include "create/create.h" + +#include +#include + +int main(int argc, char** argv) { + // Select robot. Assume Create 2 unless argument says otherwise + create::RobotModel model = create::RobotModel::CREATE_2; + std::string port = "/dev/ttyUSB0"; + int baud = 115200; + if (argc > 1 && std::string(argv[1]) == "create1") { + model = create::RobotModel::CREATE_1; + baud = 57600; + std::cout << "Running driver for Create 1" << std::endl; + } + else { + std::cout << "Running driver for Create 2" << std::endl; + } + + // Construct robot object + create::Create* robot = new create::Create(model); + + // Connect to robot + 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; + + // 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); + + while (!robot->isCleanButtonPressed()) { + // Get robot odometry and print + const create::Pose pose = robot->getPose(); + + std::cout << std::fixed << std::setprecision(2) << "\rOdometry (x, y, yaw): (" + << pose.x << ", " << pose.y << ", " << pose.yaw << ") "; + + 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 new file mode 100644 index 0000000..31a542d --- /dev/null +++ b/examples/leds.cpp @@ -0,0 +1,105 @@ +/** +Software License Agreement (BSD) + +\file leds.cpp +\authors Jacob Perron +\copyright Copyright (c) 2018, Autonomy Lab (Simon Fraser University), All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of Autonomy Lab nor the names of its contributors may + be used to endorse or promote products derived from this software without + specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +*/ +#include "create/create.h" + +#include + +int main(int argc, char** argv) { + // Select robot. Assume Create 2 unless argument says otherwise + create::RobotModel model = create::RobotModel::CREATE_2; + std::string port = "/dev/ttyUSB0"; + int baud = 115200; + if (argc > 1 && std::string(argv[1]) == "create1") { + model = create::RobotModel::CREATE_1; + baud = 57600; + std::cout << "Running driver for Create 1" << std::endl; + } + else { + std::cout << "Running driver for Create 2" << std::endl; + } + + // Construct robot object + create::Create* robot = new create::Create(model); + + // Connect to robot + 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; + + 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; + char digit_buffer[5]; + + while (!robot->isCleanButtonPressed()) { + // 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); + + // 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]); + + // Update LED values + enable_check_robot_led = !enable_check_robot_led; + enable_debris_led = !enable_debris_led; + enable_dock_led = !enable_dock_led; + enable_spot_led = !enable_spot_led; + power_led++; + + 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/odom_example.cpp b/examples/odom_example.cpp deleted file mode 100644 index 4499f9f..0000000 --- a/examples/odom_example.cpp +++ /dev/null @@ -1,64 +0,0 @@ -#include "create/create.h" - -create::Create* robot; - -int main(int argc, char** argv) { - std::string port = "/dev/ttyUSB0"; - int baud = 115200; - create::RobotModel model = create::RobotModel::CREATE_2; - - if (argc > 1 && std::string(argv[1]) == "create1") { - model = create::RobotModel::CREATE_1; - baud = 57600; - std::cout << "1st generation Create selected" << std::endl; - } - - robot = new create::Create(model); - - // Attempt to connect to Create - if (robot->connect(port, baud)) - std::cout << "Successfully connected to Create" << std::endl; - else { - std::cout << "Failed to connect to Create on port " << port.c_str() << std::endl; - return 1; - } - - robot->setMode(create::MODE_FULL); - - usleep(1000000); - - // Drive in a circle - robot->drive(0.1, 0.5); - - // Quit when center "Clean" button pressed - while (!robot->isCleanButtonPressed()) { - create::Pose pose = robot->getPose(); - create::Vel vel = robot->getVel(); - - // Print pose - std::cout << "x: " << pose.x - << "\ty: " << pose.y - << "\tyaw: " << pose.yaw * 180.0/create::util::PI << std::endl << std::endl; - - // Print velocity - std::cout << "vx: " << vel.x - << "\tvy: " << vel.y - << "\tvyaw: " << vel.yaw * 180.0/create::util::PI << std::endl << std::endl; - - // Print covariances - std::cout << "[ " << pose.covariance[0] << ", " << pose.covariance[1] << ", " << pose.covariance[2] << std::endl - << " " << pose.covariance[3] << ", " << pose.covariance[4] << ", " << pose.covariance[5] << std::endl - << " " << pose.covariance[6] << ", " << pose.covariance[7] << ", " << pose.covariance[8] << " ]" << std::endl << std::endl;; - std::cout << "[ " << vel.covariance[0] << ", " << vel.covariance[1] << ", " << vel.covariance[2] << std::endl - << " " << vel.covariance[3] << ", " << vel.covariance[4] << ", " << vel.covariance[5] << std::endl - << " " << vel.covariance[6] << ", " << vel.covariance[7] << ", " << vel.covariance[8] << " ]" << std::endl << std::endl;; - usleep(1000 * 100); //10hz - } - - std::cout << "Stopping Create." << std::endl; - - robot->disconnect(); - delete robot; - - return 0; -} diff --git a/examples/packets.cpp b/examples/packets.cpp new file mode 100644 index 0000000..4e109c2 --- /dev/null +++ b/examples/packets.cpp @@ -0,0 +1,86 @@ +/** +Software License Agreement (BSD) + +\file packets.cpp +\authors Jacob Perron +\copyright Copyright (c) 2018, Autonomy Lab (Simon Fraser University), All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of Autonomy Lab nor the names of its contributors may + be used to endorse or promote products derived from this software without + specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +*/ +#include "create/create.h" + +#include + +int main(int argc, char** argv) { + // Select robot. Assume Create 2 unless argument says otherwise + create::RobotModel model = create::RobotModel::CREATE_2; + std::string port = "/dev/ttyUSB0"; + int baud = 115200; + if (argc > 1 && std::string(argv[1]) == "create1") { + model = create::RobotModel::CREATE_1; + baud = 57600; + std::cout << "Running driver for Create 1" << std::endl; + } + else { + std::cout << "Running driver for Create 2" << std::endl; + } + + // Construct robot object + create::Create* robot = new create::Create(model); + + // Connect to robot + 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()) { + // Get serial packet statistics + 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; + + 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 new file mode 100644 index 0000000..979ef84 --- /dev/null +++ b/examples/play_song.cpp @@ -0,0 +1,97 @@ +/** +Software License Agreement (BSD) + +\file play_song.cpp +\authors Jacob Perron +\copyright Copyright (c) 2018, Autonomy Lab (Simon Fraser University), All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of Autonomy Lab nor the names of its contributors may + be used to endorse or promote products derived from this software without + specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +*/ +#include "create/create.h" + +#include + +int main(int argc, char** argv) { + // Select robot. Assume Create 2 unless argument says otherwise + create::RobotModel model = create::RobotModel::CREATE_2; + std::string port = "/dev/ttyUSB0"; + int baud = 115200; + if (argc > 1 && std::string(argv[1]) == "create1") { + model = create::RobotModel::CREATE_1; + baud = 57600; + std::cout << "Running driver for Create 1" << std::endl; + } + else { + std::cout << "Running driver for Create 2" << std::endl; + } + + // Construct robot object + create::Create* robot = new create::Create(model); + + // Connect to robot + 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); + + // Useful note defintions + const uint8_t G = 55; + const uint8_t AS = 58; + const uint8_t DS = 51; + const float half = 1.0f; + const float quarter = 0.5f; + 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); + + // Sleep to provide time for song to register + usleep(1000000); + + std::cout << "Playing a song!" << std::endl; + + // Request to play the song we just defined + robot->playSong(0); + + // Expect the song to take about four seconds + usleep(4500000); + + // 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/wheeldrop.cpp b/examples/wheeldrop.cpp new file mode 100644 index 0000000..5564539 --- /dev/null +++ b/examples/wheeldrop.cpp @@ -0,0 +1,91 @@ +/** +Software License Agreement (BSD) + +\file wheeldrop.cpp +\authors Jacob Perron +\copyright Copyright (c) 2018, Autonomy Lab (Simon Fraser University), All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of Autonomy Lab nor the names of its contributors may + be used to endorse or promote products derived from this software without + specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +*/ +#include "create/create.h" + +#include + +int main(int argc, char** argv) { + // Select robot. Assume Create 2 unless argument says otherwise + create::RobotModel model = create::RobotModel::CREATE_2; + std::string port = "/dev/ttyUSB0"; + int baud = 115200; + if (argc > 1 && std::string(argv[1]) == "create1") { + model = create::RobotModel::CREATE_1; + baud = 57600; + std::cout << "Running driver for Create 1" << std::endl; + } + else { + std::cout << "Running driver for Create 2" << std::endl; + } + + // Construct robot object + create::Create* robot = new create::Create(model); + + // Connect to robot + 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()) { + // Get wheeldrop status + const bool wheeldrop = robot->isWheeldrop(); + + // Print status + std::cout << "\rWheeldrop status: " << wheeldrop; + + // If dropped, then make light red + if (wheeldrop) + robot->setPowerLED(255); // Red + else + 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; +}