00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00031 #include <c_atlasmv.h>
00032
00038 c_atlasmv::c_atlasmv(bool debug)
00039 {
00040 debug_mode=debug;
00041
00042 robot_details = NULL;
00043
00044 timer = new c_timer;
00045 prev_sign = 123434;
00046 prev_lights = 122344;
00047
00048
00049
00050
00051 slope_rad2pulse = -1473.32;
00052 bias_rad2pulse = 1480;
00053 slope_pulse2rad = -0.000678;
00054 bias_pulse2rad = 1.0035;
00055 servo_initialized=0;
00056
00057
00058 actual_dir = 0.0;
00059 actual_brk=0.0;
00060
00061
00062
00063 dioc = NULL;
00064 dioc_initialized=0;
00065
00066
00067 porthandler_des = new int;
00068 *porthandler_des=0;
00069
00070 des_initialized=0;
00071
00072 pthread_mutex_init(&errors_mutex, NULL);
00073 pthread_mutex_init(&flags_mutex, NULL);
00074
00075 pthread_mutex_init(&c_atlasmv_robot_details_mutex, NULL);
00076
00077
00078
00079 pthread_mutex_init(&flags_mutex, NULL);
00080 pthread_mutex_init(&max_forward_speed_mutex, NULL);
00081 pthread_mutex_init(&max_backward_speed_mutex, NULL);
00082 pthread_mutex_init(&brake_speed_dif_mutex, NULL);
00083 pthread_mutex_init(&servo_direction_id_mutex, NULL);
00084 pthread_mutex_init(&servo_brake_id_mutex, NULL);
00085 pthread_mutex_init(&com_des_mutex, NULL);
00086 pthread_mutex_init(&porthandler_des_mutex, NULL);
00087 pthread_mutex_init(&length_mutex, NULL);
00088 pthread_mutex_init(&width_mutex, NULL);
00089 pthread_mutex_init(&back_wheel_diam_mutex, NULL);
00090 pthread_mutex_init(&wheelaxisdistance_mutex, NULL);
00091 pthread_mutex_init(&maximum_dir_mutex, NULL);
00092 pthread_mutex_init(&minimum_dir_mutex, NULL);
00093 pthread_mutex_init(&maximum_brk_mutex, NULL);
00094 pthread_mutex_init(&minimum_brk_mutex, NULL);
00095 pthread_mutex_init(&transmission_relation_mutex, NULL);
00096 pthread_mutex_init(&min_pulse_brk_mutex, NULL);
00097 pthread_mutex_init(&max_pulse_brk_mutex, NULL);
00098 pthread_mutex_init(&min_pulse_dir_mutex, NULL);
00099 pthread_mutex_init(&max_pulse_dir_mutex, NULL);
00100 pthread_mutex_init(&sDES_sysparam_mutex, NULL);
00101 pthread_mutex_init(&rpm_mutex, NULL);
00102 pthread_mutex_init(&linear_speed_mutex, NULL);
00103 pthread_mutex_init(&des_initialized_mutex, NULL);
00104
00105
00106
00107
00108 pthread_mutex_init(&com_servos_mutex, NULL);
00109 pthread_mutex_init(&actual_dir_mutex, NULL);
00110 pthread_mutex_init(&actual_brk_mutex, NULL);
00111 pthread_mutex_init(&servo_initialized_mutex, NULL);
00112 pthread_mutex_init(&min_pulse_brk_mutex, NULL);
00113 pthread_mutex_init(&max_pulse_brk_mutex, NULL);
00114 pthread_mutex_init(&min_pulse_dir_mutex, NULL);
00115 pthread_mutex_init(&max_pulse_dir_mutex, NULL);
00116 pthread_mutex_init(&brk_time_no_maxon_mutex, NULL);
00117
00118
00119
00120
00121 pthread_mutex_init(&com_dioc_mutex, NULL);
00122 pthread_mutex_init(&dioc_initialized_mutex, NULL);
00123 pthread_mutex_init(&sign_mutex, NULL);
00124 pthread_mutex_init(&lights_mutex, NULL);
00125 pthread_mutex_init(&cross_sensor_mutex, NULL);
00126 };
00127
00131 c_atlasmv::~c_atlasmv()
00132 {
00133 printf("Shutting down ports & robot...");
00134
00135 stopDES(*porthandler_des,debug_mode);
00136
00137
00138
00139 delete timer;
00140
00141
00142 delete servo;
00143
00144
00145 delete dioc;
00146
00147
00148
00149 delete porthandler_des;
00150
00151 printf("Done\n");
00152 };
00153
00154 void c_atlasmv::set_brk_time_no_maxon(double *a)
00155 {
00156 pthread_mutex_lock(&brk_time_no_maxon_mutex);
00157 brk_time_no_maxon = *a;
00158 pthread_mutex_unlock(&brk_time_no_maxon_mutex);
00159 };
00160
00161
00162 double c_atlasmv::get_brk_time_no_maxon()
00163 {
00164 pthread_mutex_lock(&brk_time_no_maxon_mutex);
00165 double *a = &brk_time_no_maxon;
00166 pthread_mutex_unlock(&brk_time_no_maxon_mutex);
00167 return *a;
00168 };
00174 char c_atlasmv::get_executionflags_debug()
00175 {
00176 pthread_mutex_lock(&flags_mutex);
00177 char a = debug_mode;
00178 pthread_mutex_unlock(&flags_mutex);
00179 return a;
00180 };
00186 int c_atlasmv::get_errors()
00187 {
00188 pthread_mutex_lock(&errors_mutex);
00189 int a = errors;
00190 pthread_mutex_unlock(&errors_mutex);
00191 return a;
00192 };
00193
00201 int c_atlasmv::set_errors(int err)
00202 {
00203 pthread_mutex_lock(&errors_mutex);
00204 errors = err;
00205 pthread_mutex_unlock(&errors_mutex);
00206 return 1;
00207 };
00208
00214 void c_atlasmv::set_com_des(std::string com)
00215 {
00216 pthread_mutex_lock(&com_des_mutex);
00217
00218 com_des = com;
00219 pthread_mutex_unlock(&com_des_mutex);
00220 };
00221
00227 std::string c_atlasmv::get_com_des()
00228 {
00229 std::string local;
00230 pthread_mutex_lock(&com_des_mutex);
00231 local=com_des;
00232
00233 pthread_mutex_unlock(&com_des_mutex);
00234 return local;
00235 };
00236
00242 void c_atlasmv::set_porthandler_des(int port)
00243 {
00244 pthread_mutex_lock(&porthandler_des_mutex);
00245 *porthandler_des = port;
00246 pthread_mutex_unlock(&porthandler_des_mutex);
00247 };
00248
00254 int c_atlasmv::get_porthandler_des()
00255 {
00256 pthread_mutex_lock(&porthandler_des_mutex);
00257 int a = *porthandler_des;
00258 pthread_mutex_unlock(&porthandler_des_mutex);
00259 return a;
00260 };
00261
00268 void c_atlasmv::set_rpm(int *act_rpm, int *rpm_req)
00269 {
00270 pthread_mutex_lock(&rpm_mutex);
00271 actual_rpm = *act_rpm;
00272 req_rpm = *rpm_req;
00273 pthread_mutex_unlock(&rpm_mutex);
00274 };
00275
00276
00283 void c_atlasmv::get_rpm(int *rpm, int *demanded_rpm)
00284 {
00285 pthread_mutex_lock(&rpm_mutex);
00286 *rpm = actual_rpm;
00287 *demanded_rpm = req_rpm;
00288 pthread_mutex_unlock(&rpm_mutex);
00289 };
00290
00296 void c_atlasmv::set_back_wheel_diam(double *diam)
00297 {
00298 pthread_mutex_lock(&back_wheel_diam_mutex);
00299 back_wheel_diam = *diam;
00300 pthread_mutex_unlock(&back_wheel_diam_mutex);
00301 };
00302
00308 double c_atlasmv::get_back_wheel_diam()
00309 {
00310 pthread_mutex_lock(&back_wheel_diam_mutex);
00311 double a=back_wheel_diam;
00312 pthread_mutex_unlock(&back_wheel_diam_mutex);
00313 return a;
00314 };
00315
00321 void c_atlasmv::set_transmission_relation(double *i)
00322 {
00323 pthread_mutex_lock(&transmission_relation_mutex);
00324 transmission_relation = *i;
00325 pthread_mutex_unlock(&transmission_relation_mutex);
00326 };
00327
00333 double c_atlasmv::get_transmission_relation()
00334 {
00335 pthread_mutex_lock(&transmission_relation_mutex);
00336 double a=transmission_relation;
00337 pthread_mutex_unlock(&transmission_relation_mutex);
00338 return a;
00339 };
00340
00347 void c_atlasmv::get_linear_speed(double *speed, double *demanded_speed)
00348 {
00349
00350 int rpm1,rpm2;
00351 get_rpm(&rpm1, &rpm2);
00352
00353
00354 pthread_mutex_lock(&linear_speed_mutex);
00355 actual_speed = -1*(M_PI*rpm1*get_back_wheel_diam())/(60*get_transmission_relation());
00356 req_speed = -1*(M_PI*rpm2*get_back_wheel_diam())/(60*get_transmission_relation());
00357
00358
00359
00360 if(speed)
00361 {
00362 *speed = actual_speed;
00363 }
00364 if(demanded_speed)
00365 {
00366 *demanded_speed = req_speed;
00367 }
00368 pthread_mutex_unlock(&linear_speed_mutex);
00369 }
00370
00376 void c_atlasmv::set_max_forward_speed(double *a)
00377 {
00378 pthread_mutex_lock(&max_forward_speed_mutex);
00379 max_forward_speed = *a;
00380 pthread_mutex_unlock(&max_forward_speed_mutex);
00381 };
00382
00388 double c_atlasmv::get_max_forward_speed()
00389 {
00390 pthread_mutex_lock(&max_forward_speed_mutex);
00391 double a = max_forward_speed;
00392 pthread_mutex_unlock(&max_forward_speed_mutex);
00393 return a;
00394 };
00395
00396 void c_atlasmv::set_max_backward_speed(double *a)
00397 {
00398 pthread_mutex_lock(&max_backward_speed_mutex);
00399 max_backward_speed = *a;
00400 pthread_mutex_unlock(&max_backward_speed_mutex);
00401 };
00402
00403 double c_atlasmv::get_max_backward_speed()
00404 {
00405 pthread_mutex_lock(&max_backward_speed_mutex);
00406 double a = max_backward_speed;
00407 pthread_mutex_unlock(&max_backward_speed_mutex);
00408 return a;
00409 };
00410
00411 void c_atlasmv::set_brake_speed_dif(double *a)
00412 {
00413 pthread_mutex_lock(&brake_speed_dif_mutex);
00414 brake_speed_dif = *a;
00415 pthread_mutex_unlock(&brake_speed_dif_mutex);
00416 };
00417 double c_atlasmv::get_brake_speed_dif()
00418 {
00419 pthread_mutex_lock(&brake_speed_dif_mutex);
00420 double a = brake_speed_dif;
00421 pthread_mutex_unlock(&brake_speed_dif_mutex);
00422 return a;
00423 };
00424
00425 void c_atlasmv::set_length(double *a)
00426 {
00427 pthread_mutex_lock(&length_mutex);
00428 length = *a;
00429 pthread_mutex_unlock(&length_mutex);
00430 };
00431
00432 double c_atlasmv::get_length()
00433 {
00434 pthread_mutex_lock(&length_mutex);
00435 double a = length;
00436 pthread_mutex_unlock(&length_mutex);
00437 return a;
00438 };
00439
00440 void c_atlasmv::set_width(double *a)
00441 {
00442 pthread_mutex_lock(&width_mutex);
00443 width = *a;
00444 pthread_mutex_unlock(&width_mutex);
00445 };
00446
00447 double c_atlasmv::get_width()
00448 {
00449 pthread_mutex_lock(&width_mutex);
00450 double a = width;
00451 pthread_mutex_unlock(&width_mutex);
00452 return a;
00453 };
00454
00455 void c_atlasmv::set_wheelaxisdistance(double *a)
00456 {
00457 pthread_mutex_lock(&wheelaxisdistance_mutex);
00458 wheelaxisdistance = *a;
00459 pthread_mutex_unlock(&wheelaxisdistance_mutex);
00460 };
00461
00462 double c_atlasmv::get_wheelaxisdistance()
00463 {
00464 pthread_mutex_lock(&wheelaxisdistance_mutex);
00465 double a = wheelaxisdistance;
00466 pthread_mutex_unlock(&wheelaxisdistance_mutex);
00467 return a;
00468 };
00469
00470 void c_atlasmv::set_min_pulse_brk(int *a)
00471 {
00472 pthread_mutex_lock(&min_pulse_brk_mutex);
00473 min_pulse_brk = *a;
00474 pthread_mutex_unlock(&min_pulse_brk_mutex);
00475 };
00476
00477 int c_atlasmv::get_min_pulse_brk()
00478 {
00479 pthread_mutex_lock(&min_pulse_brk_mutex);
00480 int a = min_pulse_brk;
00481 pthread_mutex_unlock(&min_pulse_brk_mutex);
00482 return a;
00483 };
00484
00485
00486 void c_atlasmv::set_max_pulse_brk(int *a)
00487 {
00488 pthread_mutex_lock(&max_pulse_brk_mutex);
00489 max_pulse_brk = *a;
00490 pthread_mutex_unlock(&max_pulse_brk_mutex);
00491 };
00492
00493 int c_atlasmv::get_max_pulse_brk()
00494 {
00495 pthread_mutex_lock(&max_pulse_brk_mutex);
00496 int a = max_pulse_brk;
00497 pthread_mutex_unlock(&max_pulse_brk_mutex);
00498 return a;
00499 };
00500
00501 void c_atlasmv::set_min_pulse_dir(int *q)
00502 {
00503 pthread_mutex_lock(&min_pulse_dir_mutex);
00504 min_pulse_dir = *q;
00505 pthread_mutex_unlock(&min_pulse_dir_mutex);
00506 };
00507
00508 int c_atlasmv::get_min_pulse_dir()
00509 {
00510 pthread_mutex_lock(&min_pulse_dir_mutex);
00511 int a = min_pulse_dir;
00512 pthread_mutex_unlock(&min_pulse_dir_mutex);
00513 return a;
00514 };
00515
00516
00517 void c_atlasmv::set_max_pulse_dir(int *a)
00518 {
00519 pthread_mutex_lock(&max_pulse_dir_mutex);
00520 max_pulse_dir = *a;
00521 pthread_mutex_unlock(&max_pulse_dir_mutex);
00522 };
00523
00524 int c_atlasmv::get_max_pulse_dir()
00525 {
00526 pthread_mutex_lock(&max_pulse_dir_mutex);
00527 int a = max_pulse_dir;
00528 pthread_mutex_unlock(&max_pulse_dir_mutex);
00529 return a;
00530 };
00531
00537 void c_atlasmv::set_sDES_sysparam(TYPE_des_sysparam* sDESconfig)
00538 {
00539 pthread_mutex_lock(&sDES_sysparam_mutex);
00540 sDES_sysparam = *sDESconfig;
00541 pthread_mutex_unlock(&sDES_sysparam_mutex);
00542 };
00543
00544 void c_atlasmv::get_sDES_sysparam(TYPE_des_sysparam *b)
00545 {
00546 pthread_mutex_lock(&sDES_sysparam_mutex);
00547 *b = sDES_sysparam;
00548 pthread_mutex_unlock(&sDES_sysparam_mutex);
00549 };
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565 void c_atlasmv::set_des_initialized(char a)
00566 {
00567 pthread_mutex_lock(&des_initialized_mutex);
00568 des_initialized=a;
00569 pthread_mutex_unlock(&des_initialized_mutex);
00570 };
00571
00572 char c_atlasmv::get_des_initialized()
00573 {
00574 pthread_mutex_lock(&des_initialized_mutex);
00575 char a=des_initialized;
00576 pthread_mutex_unlock(&des_initialized_mutex);
00577 return a;
00578 };
00579 void c_atlasmv::set_com_dioc(std::string a)
00580 {
00581 pthread_mutex_lock(&com_dioc_mutex);
00582 com_dioc=a;
00583
00584 pthread_mutex_unlock(&com_dioc_mutex);
00585 };
00586
00587 std::string c_atlasmv::get_com_dioc()
00588 {
00589 std::string local;
00590 pthread_mutex_lock(&com_dioc_mutex);
00591 local = com_dioc;
00592
00593 pthread_mutex_unlock(&com_dioc_mutex);
00594 return local;
00595 };
00596
00597 void c_atlasmv::set_com_servo(std::string com)
00598 {
00599 pthread_mutex_lock(&com_servos_mutex);
00600 com_servos=com;
00601 pthread_mutex_unlock(&com_servos_mutex);
00602 };
00603
00604 std::string c_atlasmv::get_com_servo()
00605 {
00606 std::string local;
00607 pthread_mutex_lock(&com_servos_mutex);
00608
00609
00610
00611
00612 local=com_servos;
00613 pthread_mutex_unlock(&com_servos_mutex);
00614 return local;
00615 };
00616
00617 void c_atlasmv::set_dir_srv_id(unsigned char a)
00618 {
00619 pthread_mutex_lock(&servo_direction_id_mutex);
00620 servo_direction_id = a;
00621 pthread_mutex_unlock(&servo_direction_id_mutex);
00622 };
00623
00624 char c_atlasmv::get_dir_srv_id()
00625 {
00626 pthread_mutex_lock(&servo_direction_id_mutex);
00627 unsigned char a = servo_direction_id;
00628 pthread_mutex_unlock(&servo_direction_id_mutex);
00629 return a;
00630 };
00631
00632 void c_atlasmv::set_brk_srv_id(unsigned char a)
00633 {
00634 pthread_mutex_lock(&servo_brake_id_mutex);
00635 servo_brake_id = a;
00636 pthread_mutex_unlock(&servo_brake_id_mutex);
00637 };
00638
00639 char c_atlasmv::get_brk_srv_id()
00640 {
00641 pthread_mutex_lock(&servo_brake_id_mutex);
00642 unsigned char a = servo_brake_id;
00643 pthread_mutex_unlock(&servo_brake_id_mutex);
00644 return a;
00645 };
00646
00647 void c_atlasmv::set_maximum_brk_angle(double *a)
00648 {
00649 pthread_mutex_lock(&maximum_brk_mutex);
00650 maximum_brk = *a;
00651 pthread_mutex_unlock(&maximum_brk_mutex);
00652 };
00653 double c_atlasmv::get_maximum_brk_angle()
00654 {
00655 pthread_mutex_lock(&maximum_brk_mutex);
00656 double a=maximum_brk;
00657 pthread_mutex_unlock(&maximum_brk_mutex);
00658 return a;
00659 };
00660
00661 void c_atlasmv::set_minimum_brk_angle(double *a)
00662 {
00663 pthread_mutex_lock(&minimum_brk_mutex);
00664 minimum_brk = *a;
00665 pthread_mutex_unlock(&minimum_brk_mutex);
00666 };
00667 double c_atlasmv::get_minimum_brk_angle()
00668 {
00669 pthread_mutex_lock(&minimum_brk_mutex);
00670 double a=minimum_brk;
00671 pthread_mutex_unlock(&minimum_brk_mutex);
00672 return a;
00673 };
00674
00675 void c_atlasmv::set_maximum_dir_angle(double *a)
00676 {
00677 pthread_mutex_lock(&maximum_dir_mutex);
00678 maximum_dir=*a;
00679 pthread_mutex_unlock(&maximum_dir_mutex);
00680 };
00681 double c_atlasmv::get_maximum_dir_angle()
00682 {
00683 pthread_mutex_lock(&maximum_dir_mutex);
00684 double a=maximum_dir;
00685 pthread_mutex_unlock(&maximum_dir_mutex);
00686 return a;
00687 };
00688
00689 void c_atlasmv::set_minimum_dir_angle(double *a)
00690 {
00691 pthread_mutex_lock(&minimum_dir_mutex);
00692 minimum_dir=*a;
00693 pthread_mutex_unlock(&minimum_dir_mutex);
00694 };
00695 double c_atlasmv::get_minimum_dir_angle()
00696 {
00697 pthread_mutex_lock(&minimum_dir_mutex);
00698 double a=minimum_dir;
00699 pthread_mutex_unlock(&minimum_dir_mutex);
00700 return a;
00701 };
00702
00703
00709 int c_atlasmv::init_com_des()
00710 {
00711 int result = 0;
00712
00713 printf("###\t\33[1m\33[34mSet Up ServoAmplifier\33[0m\t###\n");
00714
00715 {
00716
00717 int port=1234;
00718 if(InitDES_communication(get_com_des(), &port))
00719 {
00720
00721
00722 set_porthandler_des(port);
00723 result = restart_des();
00724
00725 }else{
00726 perror("Error opening DES serial connection");
00727 }
00728 }
00729
00730
00731
00732
00733
00734 printf("###\t\33[34mFinished\33[0m \t###\n");
00735
00736 return result;
00737 }
00738
00744 int c_atlasmv::init_com_servo()
00745 {
00746 int result=1;
00747
00748 printf("###\t\33[1m\33[34mSet Up Servos\33[0m\t###\n");
00749
00750 servo = new hitec_5980SG(com_servos.c_str());
00751
00752
00753
00754 unsigned short position=0;
00755 unsigned short brake=0;
00756 servo->SetPosition(get_dir_srv_id(), 1500);
00757
00758 position = servo->SetSpeedPosition(get_dir_srv_id(), 255);
00759 brake = servo->SetSpeedPosition(get_brk_srv_id(), 255);
00760
00761
00762
00763 if((!position) || (!brake))
00764 {
00765 printf("\33[1m\33[31mError\33[0m Direction or Brake Servo did not respond\n");
00766 result = 0;
00767 };
00768
00769 printf("###\t\33[34mFinished\33[0m\t###\n");
00770 return result;
00771 }
00772
00773
00774
00782 int c_atlasmv::set_atlasmv_details(TYPE_atlasmv_public_params* p_robot)
00783 {
00784
00785 pthread_mutex_lock(&c_atlasmv_robot_details_mutex);
00786 robot_details = p_robot;
00787 set_max_forward_speed( &p_robot->max_forward_speed);
00788 set_max_backward_speed( &p_robot->max_backward_speed);
00789 set_brake_speed_dif( &p_robot->brake_speed_dif);
00790 set_dir_srv_id( p_robot->servo_direction_id);
00791 set_brk_srv_id( p_robot->servo_brake_id);
00792
00793 printf("servo:%s\n", p_robot->com_servos.c_str());
00794 set_com_servo(p_robot->com_servos.c_str());
00795 set_com_dioc( p_robot->com_dioc.c_str());
00796 set_com_des( p_robot->com_des.c_str());
00797
00798 set_length( &p_robot->length);
00799 set_width( &p_robot->width);
00800 set_back_wheel_diam( &p_robot->back_wheel_diam);
00801 set_wheelaxisdistance( &p_robot->wheelaxisdistance);
00802 set_maximum_dir_angle( &p_robot->maximum_dir);
00803 set_minimum_dir_angle( &p_robot->minimum_dir);
00804 set_maximum_brk_angle( &p_robot->maximum_brk);
00805 set_minimum_brk_angle( &p_robot->minimum_brk);
00806 set_transmission_relation( &p_robot->transmission_relation);
00807 set_min_pulse_brk( &p_robot->min_pulse_brk);
00808 set_max_pulse_brk( &p_robot->max_pulse_brk);
00809 set_min_pulse_dir( &p_robot->min_pulse_dir);
00810 set_max_pulse_dir( &p_robot->max_pulse_dir);
00811 set_sDES_sysparam( &p_robot->sDES_sysparam);
00812 set_brk_time_no_maxon( &p_robot->brk_time_no_maxon);
00813 pthread_mutex_unlock(&c_atlasmv_robot_details_mutex);
00814
00815 return 1;
00816 };
00817
00818 void c_atlasmv::set_dioc_initialized(char a)
00819 {
00820 pthread_mutex_lock(&dioc_initialized_mutex);
00821 dioc_initialized=a;
00822 pthread_mutex_unlock(&dioc_initialized_mutex);
00823 };
00824 char c_atlasmv::get_dioc_initialized()
00825 {
00826 pthread_mutex_lock(&dioc_initialized_mutex);
00827 char a=dioc_initialized;
00828 pthread_mutex_unlock(&dioc_initialized_mutex);
00829 return a;
00830 };
00831
00832 void c_atlasmv::set_servo_initialized(char a)
00833 {
00834 pthread_mutex_lock(&servo_initialized_mutex);
00835 servo_initialized=a;
00836 pthread_mutex_unlock(&servo_initialized_mutex);
00837 };
00838
00839
00840 char c_atlasmv::get_servo_initialized()
00841 {
00842 pthread_mutex_lock(&servo_initialized_mutex);
00843 char a=servo_initialized;
00844 pthread_mutex_unlock(&servo_initialized_mutex);
00845 return a;
00846 };
00852 int c_atlasmv::init_com_dioc()
00853 {
00854 int result=1;
00855
00856
00857 if(!get_com_dioc().empty())
00858 {
00859 printf("###\t\33[1m\33[34mSet Up DIOC\33[0m\t###\n");
00860 dioc = new class_dioc(get_com_dioc().c_str());
00861 }else
00862 {
00863 printf("###\t\33[1m\33[34mDo not init DIOC\33[0m\t###\n");
00864 dioc = new class_dioc(get_com_dioc().c_str());
00865 }
00866
00867
00868
00869
00870
00871 if(dioc->CommStatus()==-1)
00872 {
00873 result = 0;
00874 printf("\33[1m\33[31mError\33[0m DIOC did not respond\n");
00875 }
00876
00877
00878 printf("###\t\33[34mFinished\33[0m\t###\n");
00879
00880 return result;
00881 };
00882
00888 int c_atlasmv::initialize_robot(void)
00889 {
00890 int result = 1;
00891 int a,b,c;
00892
00893 if(init_com_dioc())
00894 {
00895 set_dioc_initialized(1);
00896 a=0;
00897 }
00898
00899 if(init_com_des())
00900 {
00901 set_des_initialized(1);
00902 b=0;
00903 }
00904
00905 if(init_com_servo())
00906 {
00907 set_servo_initialized(1);
00908 c=0;
00909 }
00910
00911
00912 result = (((a)<<1) | ((b) << 2) | ((c)<<3));
00913
00914 if(!result)
00915 result = 1;
00916
00917 timer->tic(0);
00918 return result;
00919 };
00920
00921
00927 int c_atlasmv::restart_des()
00928 {
00929 TYPE_des_sysparam a;
00930 get_sDES_sysparam(&a);
00931
00932
00933
00934 sleep(1);
00935 int port = get_porthandler_des();
00936
00937 tcflush(port, TCIOFLUSH);
00938 if(!InitDES(port, &a, 1, 1, get_executionflags_debug()))
00939 {
00940 tcflush(port, TCIOFLUSH);
00941 set_errors(ENUM_ERR_SUPPLY_VOLT_LOW);
00942
00943
00944 return 0;
00945 }
00946
00947 return 1;
00948 }
00949
00955 int c_atlasmv::check_des(void)
00956 {
00957 int result = 1;
00958 int error;
00959 int des_status;
00960
00961 int port=get_porthandler_des();
00962 char c=get_executionflags_debug();
00963 result = DES_ST_read_sys_status(port, &des_status, c);
00964
00965 bool done_A=false,done_B=false;
00966
00967 if(des_status & 0b0000001000000000)
00968 {
00969 done_A=true;
00970 tcflush(port, TCIOFLUSH);
00971 result = DES_ST_read_error(port, &error, c);
00972 if (result)
00973 {
00974 tcflush(port, TCIOFLUSH);
00975 result = DES_ST_clear_errors(port, c);
00976 printf("Clear errors\n");
00977 }
00978 printf("Result of clear erros %d\n",result);
00979 }
00980
00981 if((des_status & 0b0010000000000000) || !(des_status & 0b0000010000000000))
00982 {
00983 done_B=true;
00984 tcflush(port, TCIOFLUSH);
00985 printf("Restarting des\n");
00986 set_des_initialized(restart_des());
00987 }
00988
00989 if(!done_A && !done_B)
00990 {
00991 tcflush(port, TCIOFLUSH);
00992 printf("Forcing des restart\n");
00993 set_des_initialized(restart_des());
00994 }
00995
00996 return result;
00997 };
00998
00999
01000
01001
01007 int c_atlasmv::read_speed(int speed_type)
01008 {
01009 int result = 0;
01010 static int rpm1=0,rpm2=0;
01011
01012 int port=get_porthandler_des();
01013 char debug = get_executionflags_debug();
01014
01015 if(!get_des_initialized())
01016 {
01017
01018 set_rpm(&rpm1, &rpm2);
01019 return result;
01020 }
01021
01022
01023 tcflush(port, TCIOFLUSH);
01024 if(DES_MF_read_velocity_is_must(port, speed_type, &rpm1, &rpm2, debug))
01025 {
01026 set_rpm(&rpm1, &rpm2);
01027 result = 1;
01028 }else
01029 {
01030
01031 set_rpm(&rpm1, &rpm2);
01032 set_des_initialized(0);
01033 printf("cannot com with DES\n");
01034 };
01035
01036 return result;
01037 };
01038
01039
01040
01048 int c_atlasmv::set_speed(double *newVel)
01049 {
01050 int result = 1;
01051
01052
01053 int rpm = -1*round((60*(get_transmission_relation())*(*newVel))/(M_PI*(get_back_wheel_diam())));
01054
01055
01056
01057 TYPE_des_sysparam a;
01058 get_sDES_sysparam(&a);
01059 ((fabs(rpm) > a.max_speed)? rpm = (rpm>0?1:-1)*a.max_speed: 0);
01060
01061
01062
01063 if(get_des_initialized())
01064 {
01065 int port = get_porthandler_des();
01066 char a = get_executionflags_debug();
01067 result = DES_SF_set_velocity(port, rpm, a);
01068 if(!result)
01069 set_des_initialized(0);
01070 }
01071 else
01072 result=0;
01073
01074 fflush(stdout);
01075
01076
01077 return result;
01078 };
01079
01080
01088 int c_atlasmv::set_direction(double *ang)
01089 {
01090 cout<<"ANG: "<<*ang<<endl;
01091 int result = 1;
01092
01093 *ang=*ang>get_maximum_dir_angle()?get_maximum_dir_angle():*ang;
01094 *ang=*ang<get_minimum_dir_angle()?get_minimum_dir_angle():*ang;
01095
01096 int numpulses = rad2pulses(*ang);
01097
01098 if(numpulses > get_max_pulse_dir())
01099 numpulses = get_max_pulse_dir();
01100
01101 if(numpulses < get_min_pulse_dir())
01102 numpulses = get_min_pulse_dir();
01103
01104 int a = get_dir_srv_id();
01105 if(get_servo_initialized())
01106 {
01107 printf("Id %d *ang:%2.2f pulses:%d\n",a, *ang, numpulses);
01108 servo->SetPosition(a, numpulses);
01109 servo->SetSpeedPosition(a, 255);
01110 }else{
01111 result=0;
01112 };
01113
01114 return result;
01115 };
01116
01117
01125 int c_atlasmv::read_direction(double *ang)
01126 {
01127 int result = 1;
01128
01129 if(!get_servo_initialized())
01130 return 0;
01131
01132 int a = get_dir_srv_id();
01133
01134
01135 unsigned short position = servo->SetSpeedPosition(a, 255);
01136
01137 if(position==0xFFFF)
01138 {
01139 result = 0;
01140 set_errors(ENUM_ERR_READ_DIR);
01141 }
01142 else
01143 {
01144 double a = pulses2rad(position);
01145 cout<<"Pulses Read: "<<position<<" angle: "<<a<< endl;
01146
01147 set_actual_dir(&a);
01148 *ang=a;
01149
01150 }
01151
01152 return result;
01153 };
01154
01155
01163 double c_atlasmv::pulses2rad(int pulses)
01164 {
01165 static bool init=true;
01166 static vector<pair<double,double> > calib_pul2rad;
01167
01168 if(init)
01169 {
01170 calib_pul2rad.push_back(make_pair(900,0.4283));
01171 calib_pul2rad.push_back(make_pair(1500,-0.009315));
01172
01173 calib_pul2rad.push_back(make_pair(2100,-0.55));
01174
01175 init=false;
01176 }
01177 return map_linear((double)pulses,calib_pul2rad);
01178
01179 }
01180
01188 int c_atlasmv::rad2pulses(double rad)
01189 {
01190 static bool init=true;
01191 static vector<pair<double,double> > calib_rad2pul;
01192
01193 if(init)
01194 {
01195
01196 calib_rad2pul.push_back(make_pair(-0.55,2100));
01197 calib_rad2pul.push_back(make_pair(-0.009315,1500));
01198 calib_rad2pul.push_back(make_pair(0.4283,900));
01199
01200 init=false;
01201 }
01202
01203 return round(map_linear(rad,calib_rad2pul));
01204
01205 }
01206
01207
01215 int c_atlasmv::read_brake(double *brk, int *num_pulses)
01216 {
01217 int result = 1;
01218
01219 double m = ((get_max_pulse_brk()-get_min_pulse_brk())/(get_maximum_brk_angle()-get_minimum_brk_angle()));
01220
01221 if(!get_servo_initialized())
01222 return 0;
01223
01224 unsigned short numpulses = servo->SetSpeedPosition(get_brk_srv_id(), 255);
01225
01226 *brk=(numpulses-get_min_pulse_brk())/m;
01227
01228
01229 set_actual_brk(brk);
01230 if(num_pulses)
01231 {
01232 *num_pulses = numpulses;
01233 }
01234
01235
01236
01237 return result;
01238
01239 };
01240
01241
01249 int c_atlasmv::set_brake(double *brk)
01250 {
01251 int result = 1;
01252
01253 int a = get_max_pulse_brk();
01254 int b = get_min_pulse_brk();
01255
01256 unsigned short numpulses = round((*brk)*(a-b)+b);
01257
01258 if(numpulses > a)
01259 numpulses = a;
01260 else if(numpulses < b)
01261 numpulses = b;
01262
01263 if(get_servo_initialized())
01264 {
01265 servo->SetPosition(get_brk_srv_id(), numpulses);
01266 servo->SetSpeedPosition(get_brk_srv_id(), 255);
01267
01268 }else
01269 result=0;
01270
01271 return result;
01272 };
01273
01283 int c_atlasmv::new_robot_state(atlasmv_base::AtlasmvMotionCommand&motion_command,atlasmv_base::AtlasmvVertSignsCommand&sign_command,atlasmv_base::AtlasmvLightsCommand&lights_command)
01284 {
01285 #define DES_VELOCITY_TYPE 1 //velocity can be 0 - mean value or 1 - instant
01286 int result = 0;
01287
01288 double new_brake = 0;
01289
01290
01291 if(!get_des_initialized() && !get_dioc_initialized() && !get_servo_initialized())
01292 return result;
01293
01294 set_errors(ENUM_ERR_NONE);
01295
01296 timer->toc(0);
01297 if(timer->get_toc(0)>0.5)
01298 {
01299 check_des();
01300 timer->tic(0);
01301 }
01302
01303
01304
01305
01306 read_speed(DES_VELOCITY_TYPE);
01307 double speed_mom, speed_demanded;
01308 get_linear_speed(&speed_mom, &speed_demanded);
01309
01310
01311 double direction;
01312 read_direction(&direction);
01313
01314
01315 motion_command.speed=motion_command.speed>get_max_forward_speed()?get_max_forward_speed():motion_command.speed;
01316 motion_command.speed=motion_command.speed<get_max_backward_speed()?get_max_backward_speed():motion_command.speed;
01317
01318 if(fabs((motion_command.speed)) < fabs(speed_mom))
01319 {
01320
01321 set_speed(&motion_command.speed);
01322
01323 if(fabs((motion_command.speed)- speed_mom)>get_brake_speed_dif())
01324 {
01325 new_brake = 1.0;
01326 }else
01327 {
01328 new_brake = 0.0;
01329 };
01330
01331
01332 set_brake(&new_brake);
01333 }else
01334 {
01335 new_brake = 0.0;
01336 set_brake(&new_brake);
01337
01338 set_speed(&motion_command.speed);
01339 if(debug_mode)printf("new_speed:%.3f, actual_speed:%.3f\n", motion_command.speed, speed_mom);
01340
01341 };
01342
01343
01344
01345 if(fabs(direction - (motion_command.dir)) > 0.0349)
01346 {
01347
01348 }
01349 else
01350 {
01351
01352 }
01353
01354
01355 set_lights(sign_command, lights_command);
01356
01357
01358 return result;
01359 };
01360
01361
01370 int c_atlasmv::set_lights(atlasmv_base::AtlasmvVertSignsCommand&sign_command,atlasmv_base::AtlasmvLightsCommand&lights_command)
01371 {
01372 int result = 1;
01373
01374 pthread_mutex_lock(&robot_status_mutex);
01375 pthread_mutex_lock(&sign_mutex);
01376
01377 if(sign_command.vert_sign != prev_sign)
01378 {
01379 prev_sign = sign_command.vert_sign;
01380 switch (prev_sign)
01381 {
01382 case MANDATORY_MEDIUM_LIGTHS:
01383 dioc->SetStatus(dioc->BLUE, dioc->BLINK1);
01384 break;
01385 case MANDATORY_BUS_LANE:
01386 dioc->SetStatus(dioc->BLUE, dioc->BLINK3);
01387 break;
01388 case WARNING_DIP_AHEAD:
01389 dioc->SetStatus(dioc->RED, dioc->BLINK1);
01390 break;
01391 case WARNING_ROAD_NARROWS:
01392 dioc->SetStatus(dioc->RED, dioc->BLINK3);
01393 break;
01394 case INFORMATION_HOSPITAL:
01395 dioc->SetStatus(dioc->GREEN, dioc->BLINK1);
01396 break;
01397 case INFORMATION_REC_SPEED_60:
01398 dioc->SetStatus(dioc->GREEN, dioc->BLINK3);
01399 break;
01400
01401 }
01402 };
01403 vert_sign = sign_command.vert_sign;
01404 pthread_mutex_unlock(&sign_mutex);
01405
01406
01407 pthread_mutex_lock(&lights_mutex);
01408
01409 int a = lights_command.headlights+lights_command.reverselights+lights_command.taillights+lights_command.turnleft+lights_command.turnright;
01410 if(a != prev_lights)
01411 {
01412
01413 prev_lights = a;
01414 dioc->SetStatus(dioc->LEFT, lights_command.turnleft);
01415 turnleft = lights_command.turnleft;
01416 dioc->SetStatus(dioc->RIGHT, lights_command.turnright);
01417 turnright = lights_command.turnright;
01418
01419 dioc->SetStatus(dioc->FRONT, lights_command.headlights);
01420 headlights = lights_command.headlights;
01421 dioc->SetStatus(dioc->REVERSE, lights_command.reverselights);
01422 reverselights = lights_command.reverselights;
01423 dioc->SetStatus(dioc->BRAKE, lights_command.taillights);
01424 taillights = lights_command.taillights;
01425 }
01426
01427 pthread_mutex_unlock(&lights_mutex);
01428 pthread_mutex_unlock(&robot_status_mutex);
01429 return result;
01430 };
01431
01432 void c_atlasmv::set_cross_sensor(int a)
01433 {
01434 pthread_mutex_lock(&cross_sensor_mutex);
01435 cross_sensor=a;
01436 pthread_mutex_unlock(&cross_sensor_mutex);
01437 };
01438
01439
01440 int c_atlasmv::get_cross_sensor()
01441 {
01442 pthread_mutex_lock(&cross_sensor_mutex);
01443 int a = cross_sensor;
01444 pthread_mutex_unlock(&cross_sensor_mutex);
01445 return a;
01446 };
01447
01453 int c_atlasmv::read_cross_sensor()
01454 {
01455 int a=(dioc->GetStatus(dioc->CROSS_A));
01456 set_cross_sensor(a);
01457 return a;
01458 };
01459
01460 void c_atlasmv::set_actual_dir(double *a)
01461 {
01462 pthread_mutex_lock(&actual_dir_mutex);
01463 actual_dir = *a;
01464 pthread_mutex_unlock(&actual_dir_mutex);
01465 };
01466
01467 double c_atlasmv::get_actual_dir()
01468 {
01469 pthread_mutex_lock(&actual_dir_mutex);
01470 double a=actual_dir;
01471 pthread_mutex_unlock(&actual_dir_mutex);
01472 return a;
01473 };
01474
01475 void c_atlasmv::set_actual_brk(double *a)
01476 {
01477 pthread_mutex_lock(&actual_brk_mutex);
01478 actual_brk = *a;
01479 pthread_mutex_unlock(&actual_brk_mutex);
01480 }
01481
01482 double c_atlasmv::get_actual_brk()
01483 {
01484 pthread_mutex_lock(&actual_brk_mutex);
01485 double a=actual_brk;
01486 pthread_mutex_unlock(&actual_brk_mutex);
01487 return a;
01488 };
01489
01490
01498 int c_atlasmv::get_lights(atlasmv_base::AtlasmvStatus& robot_status)
01499 {
01500 pthread_mutex_lock(&robot_status_mutex);
01501 robot_status.headlights = headlights;
01502 robot_status.reverselights = reverselights;
01503 robot_status.taillights = taillights;
01504 robot_status.turnleft = turnleft;
01505 robot_status.turnright = turnright;
01506 robot_status.vert_sign = vert_sign ;
01507 pthread_mutex_unlock(&robot_status_mutex);
01508
01509 return 1;
01510 };
01511
01519 int c_atlasmv::update_robot_status(atlasmv_base::AtlasmvStatus&robot_status)
01520 {
01521 int result = 1;
01522
01523
01524
01525
01526
01527
01528
01529
01530 robot_status.errors = get_errors();
01531 robot_status.dir = get_actual_dir();
01532 get_linear_speed(&robot_status.speed, NULL);
01533 robot_status.cross_sensor = get_cross_sensor();
01534 get_lights(robot_status);
01535
01536
01537
01538
01539
01540 return result;
01541 };