📄 p2os.cc
字号:
psos_state = NO_SYNC; } break; } usleep(P2OS_CYCLETIME_USEC); } if(psos_state != READY) { if(this->psos_use_tcp) printf("Couldn't synchronize with P2OS.\n" " Most likely because the robot is not connected %s %s\n", this->psos_use_tcp ? "to the ethernet-serial bridge device " : "to the serial port", this->psos_use_tcp ? this->psos_tcp_host : this->psos_serial_port); close(this->psos_fd); this->psos_fd = -1; return(1); } cnt = 4; cnt += sprintf(name, "%s", &receivedpacket.packet[cnt]); cnt++; cnt += sprintf(type, "%s", &receivedpacket.packet[cnt]); cnt++; cnt += sprintf(subtype, "%s", &receivedpacket.packet[cnt]); cnt++; command = OPEN; packet.Build(&command, 1); packet.Send(this->psos_fd); usleep(P2OS_CYCLETIME_USEC); command = PULSE; packet.Build(&command, 1); packet.Send(this->psos_fd); usleep(P2OS_CYCLETIME_USEC); printf("Done.\n Connected to %s, a %s %s\n", name, type, subtype); // now, based on robot type, find the right set of parameters for(i=0;i<PLAYER_NUM_ROBOT_TYPES;i++) { if(!strcasecmp(PlayerRobotParams[i].Class,type) && !strcasecmp(PlayerRobotParams[i].Subclass,subtype)) { param_idx = i; break; } } if(i == PLAYER_NUM_ROBOT_TYPES) { fputs("P2OS: Warning: couldn't find parameters for this robot; " "using defaults\n",stderr); param_idx = 0; } // first, receive a packet so we know we're connected. if(!this->sippacket) this->sippacket = new SIP(param_idx); this->sippacket->x_offset = 0; this->sippacket->y_offset = 0; this->sippacket->angle_offset = 0; SendReceive((P2OSPacket*)NULL,false); // turn off the sonars at first this->ToggleSonarPower(0); if(this->joystickp) { // enable joystick control P2OSPacket js_packet; unsigned char js_command[4]; js_command[0] = JOYDRIVE; js_command[1] = ARGINT; js_command[2] = 1; js_command[3] = 0; js_packet.Build(js_command, 4); this->SendReceive(&js_packet,false); } if(this->blobfinder_id.interf) CMUcamReset(false); if(this->gyro_id.interf) { // request that gyro data be sent each cycle P2OSPacket gyro_packet; unsigned char gyro_command[4]; gyro_command[0] = GYRO; gyro_command[1] = ARGINT; gyro_command[2] = 1; gyro_command[3] = 0; gyro_packet.Build(gyro_command, 4); this->SendReceive(&gyro_packet,false); } if (this->actarray_id.interf) { // Start a continuous stream of ARMpac packets P2OSPacket aaPacket; unsigned char aaCmd[4]; aaCmd[0] = ARM_STATUS; aaCmd[1] = ARGINT; aaCmd[2] = 2; aaCmd[3] = 0; aaPacket.Build (aaCmd, 4); SendReceive (&aaPacket,false); // Ask for an ARMINFOpac packet too aaCmd[0] = ARM_INFO; aaPacket.Build (aaCmd, 1); SendReceive (&aaPacket,false); } // if requested, set max accel/decel limits P2OSPacket accel_packet; unsigned char accel_command[4]; if(this->motor_max_trans_accel > 0) { accel_command[0] = SETA; accel_command[1] = ARGINT; accel_command[2] = this->motor_max_trans_accel & 0x00FF; accel_command[3] = (this->motor_max_trans_accel & 0xFF00) >> 8; accel_packet.Build(accel_command, 4); this->SendReceive(&accel_packet,false); } if(this->motor_max_trans_decel < 0) { accel_command[0] = SETA; accel_command[1] = ARGNINT; accel_command[2] = abs(this->motor_max_trans_decel) & 0x00FF; accel_command[3] = (abs(this->motor_max_trans_decel) & 0xFF00) >> 8; accel_packet.Build(accel_command, 4); this->SendReceive(&accel_packet,false); } if(this->motor_max_rot_accel > 0) { accel_command[0] = SETRA; accel_command[1] = ARGINT; accel_command[2] = this->motor_max_rot_accel & 0x00FF; accel_command[3] = (this->motor_max_rot_accel & 0xFF00) >> 8; accel_packet.Build(accel_command, 4); this->SendReceive(&accel_packet,false); } if(this->motor_max_rot_decel < 0) { accel_command[0] = SETRA; accel_command[1] = ARGNINT; accel_command[2] = abs(this->motor_max_rot_decel) & 0x00FF; accel_command[3] = (abs(this->motor_max_rot_decel) & 0xFF00) >> 8; accel_packet.Build(accel_command, 4); this->SendReceive(&accel_packet,false); } // if requested, change PID settings P2OSPacket pid_packet; unsigned char pid_command[4]; if(this->rot_kp >= 0) { pid_command[0] = ROTKP; pid_command[1] = ARGINT; pid_command[2] = this->rot_kp & 0x00FF; pid_command[3] = (this->rot_kp & 0xFF00) >> 8; pid_packet.Build(pid_command, 4); this->SendReceive(&pid_packet); } if(this->rot_kv >= 0) { pid_command[0] = ROTKV; pid_command[1] = ARGINT; pid_command[2] = this->rot_kv & 0x00FF; pid_command[3] = (this->rot_kv & 0xFF00) >> 8; pid_packet.Build(pid_command, 4); this->SendReceive(&pid_packet); } if(this->rot_ki >= 0) { pid_command[0] = ROTKI; pid_command[1] = ARGINT; pid_command[2] = this->rot_ki & 0x00FF; pid_command[3] = (this->rot_ki & 0xFF00) >> 8; pid_packet.Build(pid_command, 4); this->SendReceive(&pid_packet); } if(this->trans_kp >= 0) { pid_command[0] = TRANSKP; pid_command[1] = ARGINT; pid_command[2] = this->trans_kp & 0x00FF; pid_command[3] = (this->trans_kp & 0xFF00) >> 8; pid_packet.Build(pid_command, 4); this->SendReceive(&pid_packet); } if(this->trans_kv >= 0) { pid_command[0] = TRANSKV; pid_command[1] = ARGINT; pid_command[2] = this->trans_kv & 0x00FF; pid_command[3] = (this->trans_kv & 0xFF00) >> 8; pid_packet.Build(pid_command, 4); this->SendReceive(&pid_packet); } if(this->trans_ki >= 0) { pid_command[0] = TRANSKI; pid_command[1] = ARGINT; pid_command[2] = this->trans_ki & 0x00FF; pid_command[3] = (this->trans_ki & 0xFF00) >> 8; pid_packet.Build(pid_command, 4); this->SendReceive(&pid_packet); } // if requested, change bumper-stall behavior // 0 = don't stall // 1 = stall on front bumper contact // 2 = stall on rear bumper contact // 3 = stall on either bumper contact if(this->bumpstall >= 0) { if(this->bumpstall > 3) PLAYER_ERROR1("ignoring bumpstall value %d; should be 0, 1, 2, or 3", this->bumpstall); else { PLAYER_MSG1(1, "setting bumpstall to %d", this->bumpstall); P2OSPacket bumpstall_packet;; unsigned char bumpstall_command[4]; bumpstall_command[0] = BUMP_STALL; bumpstall_command[1] = ARGINT; bumpstall_command[2] = (unsigned char)this->bumpstall; bumpstall_command[3] = 0; bumpstall_packet.Build(bumpstall_command, 4); this->SendReceive(&bumpstall_packet,false); } } // TODO: figure out what the right behavior here is#if 0 // zero position command buffer player_position_cmd_t zero; memset(&zero,0,sizeof(player_position_cmd_t)); this->PutCommand(this->position_id,(void*)&zero, sizeof(player_position_cmd_t),NULL);#endif /* now spawn reading thread */ this->StartThread(); return(0);}int P2OS::Shutdown(){ unsigned char command[20],buffer[20]; P2OSPacket packet; memset(buffer,0,20); if(this->psos_fd == -1) return(0); this->StopThread(); command[0] = STOP; packet.Build(command, 1); packet.Send(this->psos_fd); usleep(P2OS_CYCLETIME_USEC); command[0] = CLOSE; packet.Build(command, 1); packet.Send(this->psos_fd); usleep(P2OS_CYCLETIME_USEC); close(this->psos_fd); this->psos_fd = -1; puts("P2OS has been shutdown"); delete this->sippacket; this->sippacket = NULL; return(0);}P2OS::~P2OS (void){ if (kineCalc) { delete kineCalc; kineCalc = NULL; }}intP2OS::Subscribe(player_devaddr_t id){ int setupResult; // do the subscription if((setupResult = Driver::Subscribe(id)) == 0) { // also increment the appropriate subscription counter if(Device::MatchDeviceAddress(id, this->position_id)) this->position_subscriptions++; else if(Device::MatchDeviceAddress(id, this->sonar_id)) this->sonar_subscriptions++; else if(Device::MatchDeviceAddress(id, this->actarray_id)) this->actarray_subscriptions++; else if(Device::MatchDeviceAddress(id, this->limb_id)) // We use the actarray subscriptions count for the limb // interface too since they're the same physical hardware this->actarray_subscriptions++; } return(setupResult);}intP2OS::Unsubscribe(player_devaddr_t id){ int shutdownResult; // do the unsubscription if((shutdownResult = Driver::Unsubscribe(id)) == 0) { // also decrement the appropriate subscription counter if(Device::MatchDeviceAddress(id, this->position_id)) { this->position_subscriptions--; assert(this->position_subscriptions >= 0); } else if(Device::MatchDeviceAddress(id, this->sonar_id)) { this->sonar_subscriptions--; assert(this->sonar_subscriptions >= 0); } else if(Device::MatchDeviceAddress(id, this->actarray_id)) { this->actarray_subscriptions--; assert(this->actarray_subscriptions >= 0); } else if(Device::MatchDeviceAddress(id, this->limb_id)) { // We use the actarray subscriptions count for the limb // interface too since they're the same physical hardware this->actarray_subscriptions--; assert(this->actarray_subscriptions >= 0); } } return(shutdownResult);}voidP2OS::PutData(void){ // TODO: something smarter about timestamping. // put odometry data this->Publish(this->position_id, NULL, PLAYER_MSGTYPE_DATA, PLAYER_POSITION2D_DATA_STATE, (void*)&(this->p2os_data.position), sizeof(player_position2d_data_t), NULL); // put sonar data this->Publish(this->sonar_id, NULL, PLAYER_MSGTYPE_DATA, PLAYER_SONAR_DATA_RANGES, (void*)&(this->p2os_data.sonar), sizeof(player_sonar_data_t), NULL); // put aio data this->Publish(this->aio_id, NULL, PLAYER_MSGTYPE_DATA, PLAYER_AIO_DATA_STATE, (void*)&(this->p2os_data.aio), sizeof(player_aio_data_t), NULL); // put dio data this->Publish(this->dio_id, NULL, PLAYER_MSGTYPE_DATA, PLAYER_DIO_DATA_VALUES, (void*)&(this->p2os_data.dio), sizeof(player_dio_data_t), NULL); // put gripper data this->Publish(this->gripper_id, NULL, PLAYER_MSGTYPE_DATA, PLAYER_GRIPPER_DATA_STATE, (void*)&(this->p2os_data.gripper), sizeof(player_gripper_data_t), NULL); // put bumper data this->Publish(this->bumper_id, NULL, PLAYER_MSGTYPE_DATA, PLAYER_BUMPER_DATA_STATE, (void*)&(this->p2os_data.bumper), sizeof(player_bumper_data_t), NULL); // put power data this->Publish(this->power_id, NULL, PLAYER_MSGTYPE_DATA, PLAYER_POWER_DATA_STATE, (void*)&(this->p2os_data.power), sizeof(player_power_data_t), NULL);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -