segwayrmp_example.cc
Go to the documentation of this file.
00001 #include <iostream>
00002 #include <string>
00003 #include <algorithm>
00004 
00005 #include "segwayrmp/segwayrmp.h"
00006 
00007 void handleSegwayStatus(segwayrmp::SegwayStatus::Ptr &ss) {
00008   std::cout << ss->str() << std::endl << std::endl;
00009 }
00010 
00011 int run_segway(segwayrmp::InterfaceType interface_type, std::string configuration, int config_type = 0) {
00012   std::cout << "!!!!!!!!!!!!WARNING!!!!!!!!!!!!" << std::endl;
00013   std::cout << "This example moves the segway!!" << std::endl;
00014   std::cout << "(If you continue, ctrl-c quits)" << std::endl;
00015   std::cout << "Do you want to continue? [No/yes]  ";
00016   std::string response;
00017   std::getline(std::cin, response);
00018   std::transform(response.begin(), response.end(),
00019   response.begin(), ::tolower);
00020   if (response != std::string("yes") and response != std::string("y")) {
00021     std::cout << "Aborting." << std::endl;
00022     return 1;
00023   }
00024   try {
00025     // segwayrmp::SegwayRMP rmp(interface_type, segwayrmp::rmp100);
00026     segwayrmp::SegwayRMP rmp(interface_type);
00027     if (interface_type == segwayrmp::serial) {
00028       rmp.configureSerial(configuration);
00029     } else if (interface_type == segwayrmp::usb) {
00030       switch (config_type) {
00031         case 1:
00032           rmp.configureUSBBySerial(configuration);
00033           break;
00034         case 2:
00035           rmp.configureUSBByDescription(configuration);
00036           break;
00037         case 3:
00038           rmp.configureUSBByIndex(atoi(configuration.c_str()));
00039           break;
00040         case 0:
00041         default:
00042           std::cout << "Invalid interface type provided."
00043                     << std::endl;
00044           return 0;
00045       }
00046     }
00047     rmp.setStatusCallback(handleSegwayStatus);
00048     rmp.connect();
00049     rmp.setOperationalMode(segwayrmp::balanced);
00050     while(true) {
00051       rmp.move(0.1, 0);
00052       usleep(100000);
00053     }
00054   } catch (std::exception &e) {
00055     std::cerr << "Error: " << e.what() << std::endl;
00056   }
00057   return 0;
00058 }
00059 
00060 void print_usage() {
00061   std::cout << "Usage: " << std::endl;
00062   std::cout << "       segwayrmp_example usb <serial_number | description | "
00063                "index> <\"00000056\" | \"Robotic Mobile Platform\" | 0>"
00064             << std::endl;
00065   std::cout << "       or" << std::endl;
00066   std::cout << "       segwayrmp_example serial <serial port>" << std::endl;
00067   std::cout << "Examples:" << std::endl;
00068   std::cout << "       segwayrmp_example usb index 0" << std::endl;
00069   std::cout << "       segwayrmp_example usb serial_number \"00000056\""
00070             << std::endl;
00071   std::cout << "       segwayrmp_example serial /dev/ttyUSB0" << std::endl;
00072   std::cout << "       segwayrmp_example serial COM0" << std::endl;
00073 }
00074 
00075 int main(int argc, char *argv[]) {
00076   if (argc < 2) {
00077     print_usage();
00078     return 0;
00079   }
00080   if (std::strcmp(argv[1], "serial") == 0) {
00081     if (argc < 3) {
00082       print_usage();
00083       return 0;
00084     }
00085     run_segway(segwayrmp::serial, std::string(argv[2]));
00086   } else if (std::strcmp(argv[1], "usb") == 0) {
00087     if (argc < 4) {
00088       print_usage();
00089       return 0;
00090     }
00091     
00092     if (std::strcmp(argv[2], "serial_number") == 0) {
00093       run_segway(segwayrmp::usb, std::string(argv[3]), 1);
00094     } else if (std::strcmp(argv[2], "description") == 0) {
00095       run_segway(segwayrmp::usb, std::string(argv[3]), 2);
00096     } else if (std::strcmp(argv[2], "index") == 0) {
00097       run_segway(segwayrmp::usb, std::string(argv[3]), 3);
00098     } else {
00099       print_usage();
00100       return 0;
00101     }
00102   } else {
00103     print_usage();
00104     return 0;
00105   }
00106 }


libsegwayrmp
Author(s): William Woodall
autogenerated on Fri Jan 3 2014 11:28:21