Cleanup examples
Signed-off-by: Jacob Perron <jacobmperron@gmail.com>
This commit is contained in:
parent
eaeea24a21
commit
dff0308b1b
8 changed files with 69 additions and 153 deletions
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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,9 +58,7 @@ 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;
|
||||
|
@ -69,18 +67,18 @@ int main(int argc, char** argv) {
|
|||
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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
@ -73,7 +73,7 @@ int main(int argc, char** argv) {
|
|||
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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue