iob.cpp
Go to the documentation of this file.
00001 #include <unistd.h>
00002 #include <iostream>
00003 #include <cstdlib>
00004 #include <cstring>
00005 #include <vector>
00006 #include "iob.h"
00007 
00008 static std::vector<double> command;
00009 static std::vector<std::vector<double> > forces;
00010 static std::vector<std::vector<double> > gyros;
00011 static std::vector<std::vector<double> > accelerometers;
00012 static std::vector<std::vector<double> > attitude_sensors;
00013 static std::vector<std::vector<double> > force_offset;
00014 static std::vector<std::vector<double> > gyro_offset;
00015 static std::vector<std::vector<double> > accel_offset;
00016 static std::vector<int> power;
00017 static std::vector<int> servo;
00018 static bool isLocked = false;
00019 static int frame = 0;
00020 static timespec g_ts;
00021 static long g_period_ns=5000000;
00022 
00023 #define CHECK_JOINT_ID(id) if ((id) < 0 || (id) >= number_of_joints()) return E_ID
00024 #define CHECK_FORCE_SENSOR_ID(id) if ((id) < 0 || (id) >= number_of_force_sensors()) return E_ID
00025 #define CHECK_ACCELEROMETER_ID(id) if ((id) < 0 || (id) >= number_of_accelerometers()) return E_ID
00026 #define CHECK_GYRO_SENSOR_ID(id) if ((id) < 0 || (id) >= number_of_gyro_sensors()) return E_ID
00027 #define CHECK_ATTITUDE_SENSOR_ID(id) if ((id) < 0 || (id) >= number_of_attitude_sensors()) return E_ID
00028 
00029 #if (defined __APPLE__)
00030 #include <mach/mach_time.h>  
00031 int clock_gettime(clockid_t clk_id, struct timespec *tp)
00032 {
00033     if (clk_id != CLOCK_MONOTONIC) return -1;
00034 
00035     uint64_t clk;
00036     clk = mach_absolute_time();  
00037 
00038     static mach_timebase_info_data_t info = {0,0};  
00039     if (info.denom == 0) mach_timebase_info(&info);  
00040   
00041     uint64_t elapsednano = clk * (info.numer / info.denom);  
00042   
00043     tp->tv_sec = elapsednano * 1e-9;  
00044     tp->tv_nsec = elapsednano - (tp->tv_sec * 1e9);  
00045     return 0;
00046 }
00047 
00048 #define TIMER_ABSTIME 0
00049 int clock_nanosleep(clockid_t clk_id, int flags, struct timespec *tp,
00050     struct timespec *remain)
00051 {
00052     if (clk_id != CLOCK_MONOTONIC || flags != TIMER_ABSTIME) return -1;
00053 
00054     static mach_timebase_info_data_t info = {0,0};  
00055     if (info.denom == 0) mach_timebase_info(&info);  
00056   
00057     uint64_t clk = (tp->tv_sec*1e9 + tp->tv_nsec)/(info.numer/info.denom);
00058     
00059     mach_wait_until(clk);
00060     return 0;
00061 }
00062 #endif
00063 
00064 
00065 int number_of_joints()
00066 {
00067     return (int)command.size();
00068 }
00069 
00070 int number_of_force_sensors()
00071 {
00072     return (int)forces.size();
00073 }
00074 
00075 int number_of_gyro_sensors()
00076 {
00077     return (int)gyros.size();
00078 }
00079 
00080 int number_of_accelerometers()
00081 {
00082     return (int)accelerometers.size();
00083 }
00084 
00085 int number_of_attitude_sensors()
00086 {
00087     return (int)attitude_sensors.size();
00088 }
00089 
00090 int set_number_of_joints(int num)
00091 {
00092     command.resize(num);
00093     power.resize(num);
00094     servo.resize(num);
00095     for (int i=0; i<num; i++){
00096         command[i] = power[i] = servo[i] = 0;
00097     }
00098     return TRUE;
00099 }
00100 
00101 int set_number_of_force_sensors(int num)
00102 {
00103     forces.resize(num);
00104     force_offset.resize(num);
00105     for (unsigned int i=0; i<forces.size();i++){
00106         forces[i].resize(6);
00107         force_offset[i].resize(6);
00108         for (int j=0; j<6; j++){
00109             forces[i][j] = 0;
00110             force_offset[i][j] = 0;
00111         }
00112     }
00113     return TRUE;
00114 }
00115 
00116 int set_number_of_gyro_sensors(int num)
00117 {
00118     gyros.resize(num);
00119     gyro_offset.resize(num);
00120     for (unsigned int i=0; i<gyros.size();i++){
00121         gyros[i].resize(3);
00122         gyro_offset[i].resize(3);
00123         for (int j=0; j<3; j++){
00124             gyros[i][j] = 0.0;
00125             gyro_offset[i][j] = 0.0;
00126         }
00127     }
00128     return TRUE;
00129 }
00130 
00131 int set_number_of_accelerometers(int num)
00132 {
00133     accelerometers.resize(num);
00134     accel_offset.resize(num);
00135     for (unsigned int i=0; i<accelerometers.size();i++){
00136         accelerometers[i].resize(3);
00137         accel_offset[i].resize(3);
00138         for (int j=0; j<3; j++){
00139             accelerometers[i][j] = j == 2 ? 9.81 : 0.0;
00140             accel_offset[i][j] = 0;
00141         }
00142     }
00143     return TRUE;
00144 }
00145 
00146 int set_number_of_attitude_sensors(int num)
00147 {
00148     attitude_sensors.resize(num);
00149     for (unsigned int i=0; i<attitude_sensors.size();i++){
00150         attitude_sensors[i].resize(3);
00151         for (int j=0; j<3; j++){
00152             attitude_sensors[i][j] = 0.0;
00153         }
00154     }
00155     return TRUE;
00156 }
00157 
00158 int read_power_state(int id, int *s)
00159 {
00160     CHECK_JOINT_ID(id);
00161     *s = power[id];
00162     return TRUE;
00163 }
00164 
00165 int write_power_command(int id, int com)
00166 {
00167     CHECK_JOINT_ID(id);
00168     power[id] = com;
00169     return TRUE;
00170 }
00171 
00172 int read_power_command(int id, int *com)
00173 {
00174     CHECK_JOINT_ID(id);
00175     *com = power[id];
00176     return TRUE;
00177 }
00178 
00179 int read_servo_state(int id, int *s)
00180 {
00181     CHECK_JOINT_ID(id);
00182     *s = servo[id];
00183     return TRUE;
00184 }
00185 
00186 int read_servo_alarm(int id, int *a)
00187 {
00188     CHECK_JOINT_ID(id);
00189     *a = 0;
00190     return TRUE;
00191 }
00192     
00193 int read_control_mode(int id, joint_control_mode *s)
00194 {
00195     CHECK_JOINT_ID(id);
00196     *s = JCM_POSITION;
00197     return TRUE;
00198 }
00199 
00200 int write_control_mode(int id, joint_control_mode s)
00201 {
00202     CHECK_JOINT_ID(id);
00203     return TRUE;
00204 }
00205 
00206 int read_actual_angle(int id, double *angle)
00207 {
00208     CHECK_JOINT_ID(id);
00209     *angle = command[id]+0.01;
00210     return TRUE;
00211 }
00212 
00213 int read_actual_angles(double *angles)
00214 {
00215     for (int i=0; i<number_of_joints(); i++){
00216         angles[i] = command[i]+0.01;
00217     }
00218     return TRUE;
00219 }
00220 
00221 int read_actual_torques(double *torques)
00222 {
00223     return FALSE;
00224 }
00225 
00226 int read_command_torque(int id, double *torque)
00227 {
00228     return FALSE;
00229 }
00230 
00231 int write_command_torque(int id, double torque)
00232 {
00233     return FALSE;
00234 }
00235 
00236 int read_command_torques(double *torques)
00237 {
00238     return FALSE;
00239 }
00240 
00241 int write_command_torques(const double *torques)
00242 {
00243     return FALSE;
00244 }
00245 
00246 int read_command_angle(int id, double *angle)
00247 {
00248     CHECK_JOINT_ID(id);
00249     *angle = command[id];
00250     return TRUE;
00251 }
00252 
00253 int write_command_angle(int id, double angle)
00254 {
00255     CHECK_JOINT_ID(id);
00256     command[id] = angle;
00257     return TRUE;
00258 }
00259 
00260 int read_command_angles(double *angles)
00261 {
00262     for (int i=0; i<number_of_joints(); i++){
00263         angles[i] = command[i];
00264     }
00265     return TRUE;
00266 }
00267 
00268 int write_command_angles(const double *angles)
00269 {
00270     for (int i=0; i<number_of_joints(); i++){
00271         command[i] = angles[i];
00272     }
00273     return TRUE;
00274 }
00275 
00276 int read_pgain(int id, double *gain)
00277 {
00278     return FALSE;
00279 }
00280 
00281 int write_pgain(int id, double gain)
00282 {
00283     return FALSE;
00284 }
00285 
00286 int read_dgain(int id, double *gain)
00287 {
00288     return FALSE;
00289 }
00290 
00291 int write_dgain(int id, double gain)
00292 {
00293     return FALSE;
00294 }
00295 
00296 int read_force_sensor(int id, double *forces)
00297 {
00298     for (int i=0; i<6; i++){
00299         forces[i] = ((double)random()-RAND_MAX/2)/(RAND_MAX/2)*2 
00300             + 2 + force_offset[id][i]; // 2 = initial offset
00301     }
00302     return TRUE;
00303 }
00304 
00305 int read_gyro_sensor(int id, double *rates)
00306 {
00307     CHECK_GYRO_SENSOR_ID(id);
00308     for (int i=0; i<3; i++){
00309         rates[i] = ((double)random()-RAND_MAX/2)/(RAND_MAX/2)*0.01
00310             + 0.01 + gyro_offset[id][i]; // 0.01 = initial offset
00311     }
00312     return TRUE;
00313 }
00314 
00315 int read_accelerometer(int id, double *accels)
00316 {
00317     for (int i=0; i<3; i++){
00318         double randv = ((double)random()-RAND_MAX/2)/(RAND_MAX/2)*0.01;
00319         accels[i] = (i == 2 ? (9.8+randv) : randv) 
00320             + 0.01 + accel_offset[id][i]; // 0.01 = initial offset
00321     }
00322     return TRUE;
00323 }
00324 
00325 int read_touch_sensors(unsigned short *onoff)
00326 {
00327     return FALSE;
00328 }
00329 
00330 int read_attitude_sensor(int id, double *att)
00331 {
00332     return FALSE;
00333 }
00334 
00335 int read_current(int id, double *mcurrent)
00336 {
00337     return FALSE;
00338 }
00339 
00340 int read_current_limit(int id, double *v)
00341 {
00342     return FALSE;
00343 }
00344 
00345 int read_currents(double *currents)
00346 {
00347     return FALSE;
00348 }
00349 
00350 int read_gauges(double *gauges)
00351 {
00352     return FALSE;
00353 }
00354 
00355 int read_actual_velocity(int id, double *vel)
00356 {
00357     return FALSE;
00358 }
00359 
00360 int read_command_velocity(int id, double *vel)
00361 {
00362     return FALSE;
00363 }
00364 
00365 int write_command_velocity(int id, double vel)
00366 {
00367     return FALSE;
00368 }
00369 
00370 int read_actual_velocities(double *vels)
00371 {
00372     return FALSE;
00373 }
00374 
00375 int read_command_velocities(double *vels)
00376 {
00377     return FALSE;
00378 }
00379 
00380 int write_command_velocities(const double *vels)
00381 {
00382     return FALSE;
00383 }
00384 
00385 int read_temperature(int id, double *v)
00386 {
00387     return FALSE;
00388 }
00389 
00390 int write_servo(int id, int com)
00391 {
00392     servo[id] = com;
00393     return TRUE;
00394 }
00395 
00396 int write_dio(unsigned short buf)
00397 {
00398     return FALSE;
00399 }
00400 
00401 int open_iob(void)
00402 {
00403     std::cout << "dummy IOB is opened" << std::endl;
00404     for (int i=0; i<number_of_joints(); i++){
00405         command[i] = 0.0;
00406         power[i] = OFF;
00407         servo[i] = OFF;
00408     }
00409     clock_gettime(CLOCK_MONOTONIC, &g_ts);
00410     return TRUE;
00411 } 
00412 
00413 int close_iob(void)
00414 {
00415     std::cout << "dummy IOB is closed" << std::endl;
00416     return TRUE;
00417 }
00418 
00419 int reset_body(void)
00420 {
00421     for (int i=0; i<number_of_joints(); i++){
00422         power[i] = servo[i] = OFF;
00423     }
00424     return TRUE;
00425 }
00426 
00427 int joint_calibration(int id, double angle)
00428 {
00429     return FALSE;
00430 }
00431 
00432 int read_gyro_sensor_offset(int id, double *offset)
00433 {
00434     for (int i=0; i<3; i++){
00435         offset[i] = gyro_offset[id][i];
00436     }
00437     return TRUE;
00438 }
00439 
00440 int write_gyro_sensor_offset(int id, double *offset)
00441 {
00442     for (int i=0; i<3; i++){
00443         gyro_offset[id][i] = offset[i];
00444     }
00445     return TRUE;
00446 }
00447 
00448 int read_accelerometer_offset(int id, double *offset)
00449 {
00450     for (int i=0; i<3; i++){
00451         offset[i] = accel_offset[id][i];
00452     }
00453     return TRUE;
00454 }
00455 
00456 int write_accelerometer_offset(int id, double *offset)
00457 {
00458     for (int i=0; i<3; i++){
00459         accel_offset[id][i] = offset[i];
00460     }
00461     return TRUE;
00462 }
00463 
00464 int read_force_offset(int id, double *offsets)
00465 {
00466     for (int i=0; i<6; i++){
00467         offsets[i] = force_offset[id][i];
00468     }
00469     return TRUE;
00470 }
00471 
00472 int write_force_offset(int id, double *offsets)
00473 {
00474     for (int i=0; i<6; i++){
00475         force_offset[id][i] = offsets[i];
00476     }
00477     return TRUE;
00478 }
00479 
00480 int write_attitude_sensor_offset(int id, double *offset)
00481 {
00482     return FALSE;
00483 }
00484 
00485 int read_calib_state(int id, int *s)
00486 {
00487     CHECK_JOINT_ID(id);
00488     int v = id/2;
00489     *s = v%2==0 ? ON : OFF;
00490     return TRUE;
00491 }
00492 
00493 int lock_iob()
00494 {
00495     if (isLocked) return FALSE;
00496 
00497     isLocked = true;
00498     return TRUE;
00499 }
00500 int unlock_iob()
00501 {
00502     isLocked = false;
00503     return TRUE;
00504 }
00505 
00506 int read_lock_owner(pid_t *pid)
00507 {
00508   return FALSE;
00509 }
00510 
00511 int read_limit_angle(int id, double *angle)
00512 {
00513   return FALSE;
00514 }
00515 
00516 int read_angle_offset(int id, double *angle)
00517 {
00518   return FALSE;
00519 }
00520 
00521 int write_angle_offset(int id, double angle)
00522 {
00523   return FALSE;
00524 }
00525 
00526 int read_ulimit_angle(int id, double *angle)
00527 {
00528   return FALSE;
00529 }
00530 int read_llimit_angle(int id, double *angle)
00531 {
00532   return FALSE;
00533 }
00534 int read_encoder_pulse(int id, double *ec)
00535 {
00536   return FALSE;
00537 }
00538 int read_gear_ratio(int id, double *gr)
00539 {
00540   return FALSE;
00541 }
00542 int read_torque_const(int id, double *tc)
00543 {
00544   return FALSE;
00545 }
00546 int read_torque_limit(int id, double *limit)
00547 {
00548   return FALSE;
00549 }
00550 
00551 unsigned long long read_iob_frame()
00552 {
00553     ++frame;
00554     if (frame == 5) frame = 0;
00555     return frame;
00556 }
00557 
00558 int number_of_substeps()
00559 {
00560     return 5;
00561 }
00562 
00563 int read_power(double *voltage, double *current)
00564 {
00565     *voltage = ((double)random()-RAND_MAX/2)/(RAND_MAX/2)*1+48;
00566     *current = ((double)random()-RAND_MAX/2)/(RAND_MAX/2)*0.5+1;
00567     return TRUE;
00568 }
00569 
00570 #if defined(ROBOT_IOB_VERSION) && ROBOT_IOB_VERSION >= 2
00571 int number_of_batteries()
00572 {
00573     return 1;
00574 }
00575 
00576 int read_battery(int id, double *voltage, double *current, double *soc)
00577 {
00578     *voltage = ((double)random()-RAND_MAX/2)/(RAND_MAX/2)*1+48;
00579     *current = ((double)random()-RAND_MAX/2)/(RAND_MAX/2)*0.5+1;
00580     *soc = ((double)random()-RAND_MAX/2)/(RAND_MAX/2)*0.5+50;
00581     return TRUE;
00582 }
00583 
00584 int number_of_thermometers()
00585 {
00586     return 0;
00587 }
00588 #endif
00589 
00590 #if defined(ROBOT_IOB_VERSION) && ROBOT_IOB_VERSION >= 3
00591 int write_command_acceleration(int id, double acc)
00592 {
00593     return FALSE;
00594 }
00595 
00596 int write_command_accelerations(const double *accs)
00597 {
00598     return FALSE;
00599 }
00600 
00601 int write_joint_inertia(int id, double mn)
00602 {
00603     return FALSE;
00604 }
00605 
00606 int write_joint_inertias(const double *mns)
00607 {
00608     return FALSE;
00609 }
00610 
00611 int read_pd_controller_torques(double *torques)
00612 {
00613     return FALSE;
00614 }
00615 
00616 int write_disturbance_observer(int com)
00617 {
00618     return FALSE;
00619 }
00620 
00621 int write_disturbance_observer_gain(double gain)
00622 {
00623     return FALSE;
00624 }
00625 #endif
00626 
00627 #if defined(ROBOT_IOB_VERSION) && ROBOT_IOB_VERSION >= 4
00628 int read_torque_pgain(int id, double *gain)
00629 {
00630     return FALSE;
00631 }
00632 
00633 int write_torque_pgain(int id, double gain)
00634 {
00635     return FALSE;
00636 }
00637 
00638 int read_torque_dgain(int id, double *gain)
00639 {
00640     return FALSE;
00641 }
00642 
00643 int write_torque_dgain(int id, double gain)
00644 {
00645     return FALSE;
00646 }
00647 #endif
00648 
00649 int read_driver_temperature(int id, unsigned char *v)
00650 {
00651     *v = id * 2;
00652     return TRUE;
00653 }
00654 
00655 void timespec_add_ns(timespec *ts, long ns)
00656 {
00657     ts->tv_nsec += ns;
00658     while (ts->tv_nsec > 1e9){
00659         ts->tv_sec += 1;
00660         ts->tv_nsec -= 1e9;
00661     }
00662 }
00663 
00664 double timespec_compare(timespec *ts1, timespec *ts2)
00665 {
00666     double dts = ts1->tv_sec - ts2->tv_sec;
00667     double dtn = ts1->tv_nsec - ts2->tv_nsec;
00668     return dts*1e9+dtn;
00669 }
00670 
00671 int wait_for_iob_signal()
00672 {
00673     clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &g_ts, 0);
00674     timespec_add_ns(&g_ts, g_period_ns);
00675     timespec now;
00676     clock_gettime(CLOCK_MONOTONIC, &now);
00677     double dt = timespec_compare(&g_ts, &now);
00678     if (dt <= 0){
00679         //printf("overrun(%d[ms])\n", -dt*1e6);
00680         do {
00681             timespec_add_ns(&g_ts, g_period_ns);
00682         }while(timespec_compare(&g_ts, &now)<=0);
00683     }
00684     return 0;
00685 }
00686 
00687 size_t length_of_extra_servo_state(int id)
00688 {
00689     return 0;
00690 }
00691 
00692 int read_extra_servo_state(int id, int *state)
00693 {
00694     return TRUE;
00695 }
00696 
00697 int set_signal_period(long period_ns)
00698 {
00699     g_period_ns = period_ns;
00700     return TRUE;
00701 }
00702 
00703 long get_signal_period()
00704 {
00705     return g_period_ns;
00706 }
00707 
00708 int initializeJointAngle(const char *name, const char *option)
00709 {
00710     sleep(3);
00711     return TRUE;
00712 }
00713 
00714 int read_digital_input(char *dinput)
00715 {
00716     return FALSE;
00717 }
00718 
00719 int length_digital_input()
00720 {
00721     return 0;
00722 }
00723 
00724 int write_digital_output(const char *doutput)
00725 {
00726     return FALSE;
00727 }
00728 
00729 int write_digital_output_with_mask(const char *doutput, const char *mask)
00730 {
00731     return FALSE;
00732 }
00733 
00734 int length_digital_output()
00735 {
00736     return 0;
00737 }
00738 
00739 int read_digital_output(char *doutput)
00740 {
00741     return FALSE;
00742 }
00743 
00744 


hrpsys
Author(s): AIST, Fumio Kanehiro
autogenerated on Wed May 15 2019 05:02:18