📄 nclient.c
字号:
/* if there was an error, we must not initialize the sensors, * but return an error immediately instead */ if (error == 0) return 0; /* initialize clients Smask to match the one on the server side */ init_sensors(); return ( error ); } else { printf("failed to connect to server on machine %s via tcp port #%d \n", Host_name, SERV_TCP_PORT); connectedp = 0; strcpy(Host_name, ""); State[ STATE_ERROR ] = IPC_ERROR; /* indicate IPC_ERROR */ return(FALSE); }}/* * disconnect_robot - requests the server to close the connect with robot * with id = robot_id. * * parameters: * long robot_id -- robot's id. In order to disconnect a robot, you * must know it's id. */int disconnect_robot(long robot_id){ pid_t process_id; process_id = getpid(); the_request.type = DISCONNECT_MSG; the_request.size = 2; the_request.mesg[0] = robot_id; the_request.mesg[1] = process_id; if (ipc_comm(&the_request, 0)) { dest_socket = 0; own_tcp_port = 0; connectedp = 0; return(TRUE); } else { State[ STATE_ERROR ] = IPC_ERROR; /* indicate IPC_ERROR */ connectedp = 0; return(FALSE); }}/* * ac - sets accelerations of the robot. Currently it has no effect in * simulation mode. * * parameters: * int t_ac, s_ac, r_ac -- the translation, steering, and turret * accelerations. t_ac is in 1/10 inch/sec^2 * s_ac and r_ac are in 1/10 degree/sec^2. */int ac(int t_ac, int s_ac, int r_ac){ the_request.type = AC_MSG; the_request.size = 3; the_request.mesg[0] = t_ac; the_request.mesg[1] = s_ac; the_request.mesg[2] = r_ac; if (ipc_comm(&the_request, &the_reply)) { return(process_state_reply(&the_reply)); } else { State[ STATE_ERROR ] = IPC_ERROR; /* indicate IPC_ERROR */ return(FALSE); }}/* * sp - sets speeds of the robot, this function will not cause the robot to * move. However, the set speed will be used when executing a pr() * or a pa(). * * parameters: * int t_sp, s_sp, r_sp -- the translation, steering, and turret * speeds. t_sp is in 1/10 inch/sec and * s_sp and r_sp are in 1/10 degree/sec. */int sp(int t_sp, int s_sp, int r_sp){ the_request.type = SP_MSG; the_request.size = 3; the_request.mesg[0] = t_sp; the_request.mesg[1] = s_sp; the_request.mesg[2] = r_sp; if (ipc_comm(&the_request, &the_reply)) { return(process_state_reply(&the_reply)); } else { State[ STATE_ERROR ] = IPC_ERROR; /* indicate IPC_ERROR */ return(FALSE); }}/* * pr - moves the motors of the robot by a relative distance, using the speeds * set by sp(). The three parameters specify the relative distances for * the three motors: translation, steering, and turret. All the three * motors move concurrently if the speeds are not set to zero and the * distances to be traveled are non-zero. Depending on the timeout * period set (by function conf_tm(timeout)), the motion may * terminate before the robot has moved the specified distances * * parameters: * int t_pr, s_pr, r_pr -- the specified relative distances of the * translation, steering, and turret motors. * t_pr is in 1/10 inch and s_pr and r_pr are * in 1/10 degrees. */int pr(int t_pr, int s_pr, int r_pr){ the_request.type = PR_MSG; the_request.size = 3; the_request.mesg[0] = t_pr; the_request.mesg[1] = s_pr; the_request.mesg[2] = r_pr; if (ipc_comm(&the_request, &the_reply)) { return(process_state_reply(&the_reply)); } else { State[ STATE_ERROR ] = IPC_ERROR; /* indicate IPC_ERROR */ return(FALSE); }}/* * pa - moves the motors of the robot to the specified absolute positions * using the speeds set by sp(). Depending on the timeout period set * (by conf_tm()), the motion may terminate before the robot has * moved to the specified positions. * * parameters: * int t_pa, s_pa, r_pa -- the specified absolute positions of the * translation, steering, and turret motors. * t_pa is in 1/10 inch and s_pa and r_pa are * in 1/10 degrees. */int pa(int t_pa, int s_pa, int r_pa);int pa(int t_pa, int s_pa, int r_pa){ return(FALSE); the_request.type = PA_MSG; the_request.size = 3; the_request.mesg[0] = t_pa; the_request.mesg[1] = s_pa; the_request.mesg[2] = r_pa; if (ipc_comm(&the_request, &the_reply)) { return(process_state_reply(&the_reply)); } else { State[ STATE_ERROR ] = IPC_ERROR; /* indicate IPC_ERROR */ return(FALSE); }}/* * vm - velocity mode, command the robot to move at translational * velocity = tv, steering velocity = sv, and rotational velocity = * rv. The robot will continue to move at these velocities until * either it receives another command or this command has been * timeout (in which case it will stop its motion). * * parameters: * int t_vm, s_vm, r_vm -- the desired translation, steering, and turret * velocities. tv is in 1/10 inch/sec and * sv and rv are in 1/10 degree/sec. */int vm(int t_vm, int s_vm, int r_vm){ the_request.type = VM_MSG; the_request.size = 3; the_request.mesg[0] = t_vm; the_request.mesg[1] = s_vm; the_request.mesg[2] = r_vm; if (ipc_comm(&the_request, &the_reply)) { return(process_state_reply(&the_reply)); } else { State[ STATE_ERROR ] = IPC_ERROR; /* indicate IPC_ERROR */ return(FALSE); }}/* * mv - move, send a generalized motion command to the robot. * For each of the three axis (translation, steering, and * turret) a motion mode (t_mode, s_mode, r_mode) can be * specified (using the values MV_IGNORE, MV_AC, MV_SP, * MV_LP, MV_VM, and MV_PR defined above): * * MV_IGNORE : the argument for this axis is ignored * and the axis's motion will remain * unchanged. * MV_AC : the argument for this axis specifies * an acceleration value that will be used * during motion commands. * MV_SP : the argument for this axis specifies * a speed value that will be used during * position relative (PR) commands. * MV_LP : the arguemnt for this axis is ignored * but the motor is turned off. * MV_VM : the argument for this axis specifies * a velocity and the axis will be moved * with this velocity until a new motion * command is issued (vm,pr,mv) or * recieves a timeout. * MV_PR : the argument for this axis specifies * a position and the axis will be moved * to this position, unless this command * is overwritten by another (vm,pr,mv). * * parameters: * int t_mode - the desired mode for the tranlation axis * int t_mv - the value for that axis, velocity or position, * depending on t_mode * int s_mode - the desired mode for the steering axis * int s_mv - the value for that axis, velocity or position, * depending on t_mode * int r_mode - the desired mode for the turret axis * int r_mv - the value for that axis, velocity or position, * depending on t_mode */int mv(int t_mode, int t_mv, int s_mode, int s_mv, int r_mode, int r_mv){ /* check if the modes are correct */ if (((t_mode != MV_IGNORE) && (t_mode != MV_AC) && (t_mode != MV_SP) && (t_mode != MV_LP) && (t_mode != MV_VM) && (t_mode != MV_PR)) || ((s_mode != MV_IGNORE) && (s_mode != MV_AC) && (s_mode != MV_SP) && (s_mode != MV_LP) && (s_mode != MV_VM) && (s_mode != MV_PR)) || ((r_mode != MV_IGNORE) && (r_mode != MV_AC) && (r_mode != MV_SP) && (r_mode != MV_LP) && (r_mode != MV_VM) && (r_mode != MV_PR))) return ( FALSE ); /* build the request packet */ the_request.type = MV_MSG; the_request.size = 6; the_request.mesg[0] = t_mode; the_request.mesg[1] = t_mv; the_request.mesg[2] = s_mode; the_request.mesg[3] = s_mv; the_request.mesg[4] = r_mode; the_request.mesg[5] = r_mv; /* communicate with robot */ if (ipc_comm(&the_request, &the_reply)) { /* process the reply packet that contains the state info */ return(process_state_reply(&the_reply)); } else { /* indicate IPC_ERROR */ State[ STATE_ERROR ] = IPC_ERROR; return(FALSE); }}/* * ct - send the sensor mask, Smask, to the robot. You must first change * the global variable Smask to the desired communication mask before * calling this function. * * to avoid inconsistencies usedSmask is used in all other function. * once ct is called the user accessible mask Smask is used to * redefine usedSmask. This avoids that a returning package is encoded * with a different mask than the one it was sent with, in case * the mask has been changed on the client side, but has not been * updated on the server side. */int ct(void){ int i; unsigned char b0, b1, b2, b3, b4, b5, b6; for ( i = 0; i < NUM_MASK; i++ ) usedSmask[i] = Smask[i]; /* first encode Smask */ b0 = bits_to_byte (Smask[1], Smask[2], Smask[3], Smask[4], Smask[5], Smask[6], Smask[7], Smask[8]); b1 = bits_to_byte (Smask[9], Smask[10], Smask[11], Smask[12], Smask[13], Smask[14], Smask[15], Smask[16]); b2 = bits_to_byte (Smask[17], Smask[18], Smask[19], Smask[20], Smask[21], Smask[22], Smask[23], Smask[24]); b3 = bits_to_byte (Smask[25], Smask[26], Smask[27], Smask[28], Smask[29], Smask[30], Smask[31], Smask[32]); b4 = bits_to_byte (Smask[33], Smask[34], Smask[35], Smask[36], Smask[37], Smask[38], Smask[39], Smask[40]); b5 = bits_to_byte (Smask[0], Smask[41], Smask[42], Smask[43], 0,0,0,0); b6 = (unsigned char) Smask[0]; the_request.type = CT_MSG; the_request.size = 7; the_request.mesg[0] = b0; the_request.mesg[1] = b1; the_request.mesg[2] = b2; the_request.mesg[3] = b3; the_request.mesg[4] = b4; the_request.mesg[5] = b5; the_request.mesg[6] = b6; if (ipc_comm(&the_request, &the_reply)) { return(process_state_reply(&the_reply)); } else { State[ STATE_ERROR ] = IPC_ERROR; /* indicate IPC_ERROR */ return(FALSE); }}/* * gs - get the current state of the robot according to the mask (of * the communication channel) */int gs(void){ the_request.type = GS_MSG; the_request.size = 0; if (ipc_comm(&the_request, &the_reply)) { return(process_state_reply(&the_reply)); } else { State[ STATE_ERROR ] = IPC_ERROR; /* indicate IPC_ERROR */ return(FALSE); }}/* * st - stops the robot (the robot holds its current position) */int st(void){ the_request.type = ST_MSG; the_request.size = 0; if (ipc_comm(&the_request, &the_reply)) { return(process_state_reply(&the_reply)); } else { State[ STATE_ERROR ] = IPC_ERROR; /* indicate IPC_ERROR */ return(FALSE); }}/* * lp - set motor limp (the robot may not hold its position). */int lp(void){ the_request.type = LP_MSG; the_request.size = 0; if (ipc_comm(&the_request, &the_reply)) { return(process_state_reply(&the_reply)); } else { State[ STATE_ERROR ] = IPC_ERROR; /* indicate IPC_ERROR */ return(FALSE); }}/* * tk - sends the character stream, talk_string, to the voice synthesizer * to make the robot talk. * * parameters: * char *talk_string -- the string to be sent to the synthesizer. */int tk(char *talk_string){ the_request.type = TK_MSG; the_request.size = (strlen(talk_string)+4)/4; strcpy((char *)the_request.mesg, talk_string); if (ipc_comm(&the_request, &the_reply)) { return(process_simple_reply(&the_reply)); } else { State[ STATE_ERROR ] = IPC_ERROR; /* indicate IPC_ERROR */ return(FALSE); }}/* * dp - define the current position of the robot as (x,y) * * parameters: * int x, y -- the position to set the robot to. */int dp(int x, int y){ the_request.type = DP_MSG; the_request.size = 2; the_request.mesg[0] = x; the_request.mesg[1] = y; if (ipc_comm(&the_request, &the_reply)) { return(process_state_reply(&the_reply)); } else { State[ STATE_ERROR ] = IPC_ERROR; /* indicate IPC_ERROR */ return(FALSE); }}/* * zr - zeroing the robot, align steering and turret with bumper zero. * The position, steering and turret angles are all set to zero. * This function returns when the zeroing process has completed. */int zr(void){ the_request.type = ZR_MSG; the_request.size = 0; if (ipc_comm(&the_request, &the_reply)) { return(process_state_reply(&the_reply)); } else { State[ STATE_ERROR ] = IPC_ERROR; /* indicate IPC_ERROR */ return(FALSE); }}/* * conf_ir - configure infrared sensor system. * * parameters: * int history -- specifies the percentage dependency of the current * returned reading on the previous returned reading. * It should be set between 0 and 10: 0 = no dependency * 10 = full dependency, i.e. the reading will not change * int order[16] -- specifies the firing sequence of the infrared * (#0 .. #15). You can terminate the order list by a * "255". For example, if you want to use only the * front three infrared sensors then set order[0]=0, * order[1]=1, order[2]=15, order[3]=255 (terminator). */int conf_ir(int history, int order[16]){ int i; the_request.type = CONF_IR_MSG; the_request.size = 17; the_request.mesg[0] = history; for (i=0; i<16; i++) { the_request.mesg[i+1] = order[i]; } if (ipc_comm(&the_request, &the_reply)) { return(process_state_reply(&the_reply)); } else { State[ STATE_ERROR ] = IPC_ERROR; /* indicate IPC_ERROR */ return(FALSE); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -