motor_serial_test.cc
Go to the documentation of this file.
00001 
00031 #include <gtest/gtest.h>
00032 
00033 #include <ubiquity_motor/motor_serial.h>
00034 #include <ubiquity_motor/motor_message.h>
00035 #include <ros/ros.h>
00036 
00037 
00038 #include <string>
00039 
00040 #if defined(__linux__)
00041 #include <pty.h>
00042 #else
00043 #include <util.h>
00044 #endif
00045 
00046 class MotorSerialTests : public ::testing::Test {
00047 protected:
00048   virtual void SetUp() {
00049     if (openpty(&master_fd, &slave_fd, name, NULL, NULL) == -1) {
00050       perror("openpty");
00051       exit(127);
00052     }
00053 
00054     ASSERT_TRUE(master_fd > 0);
00055     ASSERT_TRUE(slave_fd > 0);
00056     ASSERT_TRUE(std::string(name).length() > 0);
00057 
00058     ros::Time::init();
00059     motors = new MotorSerial(std::string(name), 9600, 1000);
00060   }
00061 
00062   virtual void TearDown() {
00063     delete motors;
00064   }
00065 
00066   MotorSerial * motors;
00067   int master_fd;
00068   int slave_fd;
00069   char name[100];
00070 };
00071 
00072 TEST_F(MotorSerialTests, goodReadWorks){
00073   uint8_t test[]= {0x7E, 0x02, 0xBB, 0x07, 0x00, 0x00, 0x01, 0x2C, 0x0E};
00074   //char test[]= {0x0E, 0x2C, 0x01, 0x00, 0x00, 0x07, 0xBB, 0x02, 0x7E};
00075   write(master_fd, test, 9);
00076 
00077   while(!motors->commandAvailable()) {
00078   }
00079 
00080   MotorMessage mm;
00081   mm = motors-> receiveCommand();
00082   ASSERT_EQ(300, mm.getData());
00083   ASSERT_EQ(MotorMessage::TYPE_WRITE, mm.getType());
00084   ASSERT_EQ(MotorMessage::REG_LEFT_SPEED_SET, mm.getRegister());
00085 }
00086 
00087 TEST_F(MotorSerialTests, misalignedOneGoodReadWorks){
00088   uint8_t test[]= {0x00, 0x7E, 0x02, 0xBB, 0x07, 0x00, 0x00, 0x01, 0x2C, 0x0E};
00089   //char test[]= {0x0E, 0x2C, 0x01, 0x00, 0x00, 0x07, 0xBB, 0x02, 0x7E};
00090   write(master_fd, test, 10);
00091 
00092   while(!motors->commandAvailable()) {
00093   }
00094 
00095   MotorMessage mm;
00096   mm = motors-> receiveCommand();
00097   ASSERT_EQ(300, mm.getData());
00098   ASSERT_EQ(MotorMessage::TYPE_WRITE, mm.getType());
00099   ASSERT_EQ(MotorMessage::REG_LEFT_SPEED_SET, mm.getRegister());
00100 }
00101 
00102 TEST_F(MotorSerialTests, misalignedManyGoodReadWorks){
00103   uint8_t test[]= {0x01, 0x2C, 0x0E, 0x7E, 0x02, 0xBB, 0x07, 0x00, 0x00, 0x01, 0x2C, 0x0E};
00104   //char test[]= {0x0E, 0x2C, 0x01, 0x00, 0x00, 0x07, 0xBB, 0x02, 0x7E};
00105   write(master_fd, test, 12);
00106 
00107   while(!motors->commandAvailable()) {
00108   }
00109 
00110   MotorMessage mm;
00111   mm = motors-> receiveCommand();
00112   ASSERT_EQ(300, mm.getData());
00113   ASSERT_EQ(MotorMessage::TYPE_WRITE, mm.getType());
00114   ASSERT_EQ(MotorMessage::REG_LEFT_SPEED_SET, mm.getRegister());
00115 }
00116 
00117 TEST_F(MotorSerialTests, badReadFails){
00118   uint8_t test[]= {0xdd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
00119   //uint8_t test[]= {0x7E, 0x02, 0xBB, 0x07, 0x00, 0x00, 0x01, 0x2C, 0x0E};
00120   write(master_fd, test, 9);
00121 
00122   ros::Rate loop(100);
00123   int times = 0;
00124   while(!motors->commandAvailable()) {
00125     loop.sleep();
00126     times++;
00127     if(times >= 20) {
00128       break;
00129     }
00130   }
00131 
00132   if(times >= 20) {
00133       SUCCEED();
00134   }
00135   else {
00136     FAIL();
00137   }
00138 }
00139 
00140 TEST_F(MotorSerialTests, misalignedOneBadReadFails){
00141   uint8_t test[]= {0x00, 0x7d, 0x02, 0xBB, 0x07, 0x00, 0x00, 0x01, 0x2C, 0x0E};
00142   //char test[]= {0x0E, 0x2C, 0x01, 0x00, 0x00, 0x07, 0xBB, 0x02, 0x7E};
00143   write(master_fd, test, 10);
00144 
00145   ros::Rate loop(100);
00146   int times = 0;
00147   while(!motors->commandAvailable()) {
00148     loop.sleep();
00149     times++;
00150     if(times >= 20) {
00151       break;
00152     }
00153   }
00154 
00155   if(times >= 20) {
00156       SUCCEED();
00157   }
00158   else {
00159     FAIL();
00160   }
00161 }
00162 
00163 TEST_F(MotorSerialTests, incompleteReadFails){
00164   uint8_t test[]= {0x7E, 0x02, 0xBB, 0x00};
00165   //char test[]= {0x0E, 0x2C, 0x01, 0x00, 0x00, 0x07, 0xBB, 0x02, 0x7E};
00166   write(master_fd, test, 4);
00167 
00168   ros::Rate loop(100);
00169   int times = 0;
00170   while(!motors->commandAvailable()) {
00171     loop.sleep();
00172     times++;
00173     if(times >= 20) {
00174       break;
00175     }
00176   }
00177 
00178   if(times >= 20) {
00179       SUCCEED();
00180   }
00181   else {
00182     FAIL();
00183   }
00184 }
00185 
00186 TEST_F(MotorSerialTests, incompleteMisalignedReadFails){
00187   uint8_t test[]= {0x0f,0x7E, 0x02, 0xBB, 0x00};
00188   //char test[]= {0x0E, 0x2C, 0x01, 0x00, 0x00, 0x07, 0xBB, 0x02, 0x7E};
00189   write(master_fd, test, 5);
00190 
00191   ros::Rate loop(100);
00192   int times = 0;
00193   while(!motors->commandAvailable()) {
00194     loop.sleep();
00195     times++;
00196     if(times >= 20) {
00197       break;
00198     }
00199   }
00200 
00201   if(times >= 20) {
00202       SUCCEED();
00203   }
00204   else {
00205     FAIL();
00206   }
00207 }
00208 
00209 TEST_F(MotorSerialTests, writeWorks) {
00210         MotorMessage version;
00211         version.setRegister(MotorMessage::REG_FIRMWARE_VERSION);
00212         version.setType(MotorMessage::TYPE_READ);
00213         version.setData(0);
00214         motors->transmitCommand(version);
00215 
00216   uint8_t arr[9];
00217   read(master_fd, arr, 9);
00218 
00219   std::vector<uint8_t> input(arr, arr + sizeof(arr)/ sizeof(uint8_t));
00220 
00221   ASSERT_EQ(input, version.serialize());
00222 }
00223 
00224 TEST_F(MotorSerialTests, writeMultipleWorks) {
00225   std::vector<MotorMessage> commands;
00226 
00227   MotorMessage left_odom;
00228   left_odom.setRegister(MotorMessage::REG_LEFT_ODOM);
00229   left_odom.setType(MotorMessage::TYPE_READ);
00230   left_odom.setData(0);
00231   commands.push_back(left_odom);
00232 
00233   MotorMessage right_odom;
00234   right_odom.setRegister(MotorMessage::REG_RIGHT_ODOM);
00235   right_odom.setType(MotorMessage::TYPE_READ);
00236   right_odom.setData(0);
00237   commands.push_back(right_odom);
00238 
00239   MotorMessage left_vel;
00240   left_vel.setRegister(MotorMessage::REG_LEFT_SPEED_MEASURED);
00241   left_vel.setType(MotorMessage::TYPE_READ);
00242   left_vel.setData(0);
00243   commands.push_back(left_vel);
00244 
00245   MotorMessage right_vel;
00246   right_vel.setRegister(MotorMessage::REG_RIGHT_SPEED_MEASURED);
00247   right_vel.setType(MotorMessage::TYPE_READ);
00248   right_vel.setData(0);
00249   commands.push_back(right_vel);
00250 
00251   motors->transmitCommands(commands);
00252 
00253   sleep(2);
00254 
00255   uint8_t arr[36];
00256   read(master_fd, arr, 36);
00257   std::vector<uint8_t> input(arr, arr + sizeof(arr)/ sizeof(uint8_t));
00258 
00259   std::vector<uint8_t> expected(0);
00260   for (std::vector<MotorMessage>::iterator i = commands.begin(); i != commands.end(); ++i){
00261    std::vector<uint8_t> serialized = i->serialize();
00262    expected.insert(expected.end(), serialized.begin(), serialized.end());   
00263   }
00264 
00265   ASSERT_EQ(expected, input);
00266 }
00267 
00268 
00269 
00270 int main(int argc, char **argv){
00271   testing::InitGoogleTest(&argc, argv);
00272   return RUN_ALL_TESTS();
00273 }


ubiquity_motor
Author(s):
autogenerated on Thu Jun 6 2019 18:33:28