예제 #1
0
파일: dpscore.c 프로젝트: BygoneWorlds/anet
/*-------------------------------------------------------------------------
 End a score report.
-------------------------------------------------------------------------*/
DP_API dp_result_t DP_APIX dpReportScoreEnd(dp_t *dp)
{
	precondition(dp);

#if 0
	if (dp->hGameServer == PLAYER_NONE) {
		commSetParamReq_t   req;
		commSetParamResp_t  resp; /* This is a dummy variable */

		memset(&req, 0, sizeof(commSetParamReq_t));
		req.param_num = comm_PARAM_FINALREPORT;
		req.param_value = FALSE;	/* To end score reporting */
		req.reqLen = sizeof(commSetParamReq_t);
		commSetParam(&req, &resp);
		return resp.status;  
	}
#endif
	return dp_RES_OK;
}
예제 #2
0
파일: dpscore.c 프로젝트: BygoneWorlds/anet
/*-------------------------------------------------------------------------
 Begin a score report.  
 Flag must be zero.
 This should only be called at the end of the game (but before dpClose).
 
 Call dpReportScoreStart before calling dpReportScore2, then
 call dpReportScore2 to report as many scores as you like, then finally
 call dpReportScoreEnd to finish sending the block of scores.
-------------------------------------------------------------------------*/
DP_API dp_result_t DP_APIX dpReportScoreStart(dp_t *dp, int flag)
{
	precondition(dp);
	/* dp->score_flag = flag; */		/* no longer used? */

#if 0
	if (dp->hGameServer == PLAYER_NONE) {
		commSetParamReq_t   req;
		commSetParamResp_t  resp; /* This is a dummy variable */

		memset(&req, 0, sizeof(commSetParamReq_t));
		req.param_num = comm_PARAM_FINALREPORT;
		req.param_value = TRUE;	/* To begin score reporting */
		req.param_value2 = (long) flag;
		req.reqLen = sizeof(commSetParamReq_t);
		commSetParam(&req, &resp);
		return resp.status;
	}
#endif
	return dp_RES_OK;
}
예제 #3
0
// --- MAIN ---
int main() {
	char choice;
	int device_id;
	int aux_int;
	short int aux_sint;
	unsigned char aux_uchar;
	float pid_control[3];
	short int offsets[NUM_OF_SENSORS];


	assert(open_port());

	printMainMenu();
	scanf("%c", &choice);
	printf("choice: %c\n", choice);

	switch(choice) {
		case 'g':
			get_or_set = choice;
			break;
		case 's':
			get_or_set = choice;
			break;
		case 'm':
			initMemory();
			break;
		case 'c':
			calibrate();
			break;

		default:
			break;
	}

	if (get_or_set == 'g' || get_or_set == 's') {
		printParamMenu();

		scanf(" %c", &choice);

		if (get_or_set == 's') {
			switch(choice) {
				// Set new ID
				case 'i':
					printf("Choose a new ID: ");
					scanf("%d", &device_id);

					commSetParam(&comm_settings_t, BROADCAST_ID,
	            		PARAM_ID, &device_id, 1);
					usleep(100000);
					commStoreDefaultParams(&comm_settings_t, BROADCAST_ID);
					usleep(500000);
					break;

				// Set new PID parameters
				case 'k':
					printf("Set the new values for the PID controller:\nK_p: ");
		            scanf("%f", pid_control);
		            printf("K_i: ");
		            scanf("%f", pid_control + 1);
		            printf("K_d: ");
		            scanf("%f", pid_control + 2);

		            commSetParam(&comm_settings_t, BROADCAST_ID,
		            	PARAM_PID_CONTROL, pid_control, 3);
			        usleep(100000);
			        commStoreDefaultParams(&comm_settings_t, BROADCAST_ID);
			        usleep(500000);
					break;
				// Set startup activation flag
				case 'a':
					printf("Do you want the motor to be ON [1] or OFF [0] at startup?: ");
					scanf("%d", &aux_int);

					if (aux_int == 0) {
						aux_uchar = 0;
						commSetParam(&comm_settings_t, BROADCAST_ID,
            				PARAM_STARTUP_ACTIVATION, &aux_uchar, 1);
					} else {
						aux_uchar = 3;
						commSetParam(&comm_settings_t, BROADCAST_ID,
            				PARAM_STARTUP_ACTIVATION, &aux_uchar, 1);
					}
					commStoreDefaultParams(&comm_settings_t, BROADCAST_ID);
					break;


				case 'm':
					break;
				case 's':
					break;
				case 'o':
					printf("Set the new offsets:\n");
					printf("Offset 1: ");
					scanf("%hd", &aux_sint);
					offsets[0] = aux_sint;
					printf("Offset 2: ");
					scanf("%hd", &aux_sint);
					offsets[1] = aux_sint;
					printf("Offset 3: ");
					scanf("%hd", &aux_sint);
					offsets[2] = aux_sint;

					commSetParam(&comm_settings_t, BROADCAST_ID, PARAM_MEASUREMENT_OFFSET,
				            offsets, NUM_OF_SENSORS);
				    usleep(100000);
				    commStoreDefaultParams(&comm_settings_t, BROADCAST_ID);
				    usleep(1000000);
					break;
				case 'u':
					break;
				case 'f':
					break;
				case 'l':
					break;
			}
		} else {
			switch(choice) {
				case 'i':
					break;
				case 'k':
					break;
				case 'a':
					break;
				case 'm':
					break;
				case 's':
					break;
				case 'o':
					break;
				case 'u':
					break;
				case 'f':
					break;
				case 'l':
					break;
			}

		}

	}

	return 1;
}
예제 #4
0
/** main loop
*/
int main (int argc, char **argv)
{
    int option;
    int longIndex = 0;
    FILE *file;

    comm_settings comm_settings_t;
    int  num_ports = 0;
    char ports[10][255];
    char list_of_devices[255];
    char port_s[255];

    int  i = 0;
    int  aux_int;
    unsigned char aux_uchar;
    char aux_string[10000];
    // unsigned int aux_uint;

    global_args.device_id            = 0;
    global_args.flag_id              = 0;
    global_args.flag_pid_control     = 0;
    global_args.flag_activation      = 0;
    global_args.flag_deactivation    = 0;
    global_args.flag_input_mode      = 0;
    global_args.flag_pos_resolution  = 0;
    global_args.flag_meas_offset     = 0;
    global_args.flag_meas_multiplier = 0;
    global_args.flag_list_devices    = 0;
    global_args.flag_serial_port     = 0;
    global_args.flag_ping            = 0;
    global_args.flag_restore         = 0;
    global_args.flag_verbose         = 0;
    global_args.flag_max_step        = 0;
    global_args.flag_max_current     = 0;

//=======================================================     processing options


    while ((option = getopt_long( argc, argv, optString, longOpts, &longIndex )) != -1)
    {
      switch (option)
        {
        case 'i':
            sscanf(optarg,"%d", &aux_int);
            global_args.new_id = aux_int;
            global_args.flag_id = 1;
            break;
        case 'k':
            printf("Set the new values for the PID controller:\nK_p: ");
            scanf("%f", global_args.pid_control);
            printf("K_i: ");
            scanf("%f", global_args.pid_control + 1);
            printf("K_d: ");
            scanf("%f", global_args.pid_control + 2);

            printf("PID parameters: %f, %f, %f\n", global_args.pid_control[0], global_args.pid_control[1], global_args.pid_control[2]);

            global_args.flag_pid_control = 1;
            break;
        case 'a':
            global_args.flag_activation = 1;
            break;
        case 'd':
            global_args.flag_deactivation = 1;
            break;
        case 'm':
            puts("\nChoose Input Mode:\n");
            puts("[0] - Default: Motors are commanded using the communication interface.");
            puts("[1] - Encoder 3 works as a 'joystick' for the motors.");
            puts("");
            puts("--- EMG PART ---");
            puts("[2] - EMG signal is used to proportionally control hand closure.");
            puts("[3] - EMG signals are used to control opening and closing of the hand.");
            puts("[4] - First EMG signal passing the threshold is used to control the hand.");
            puts("");
            scanf("%d",&aux_int);
            if (aux_int < 1 || aux_int > 4) {
                aux_int = 0;
            }
            global_args.input_mode = aux_int;
            global_args.flag_input_mode = 1;
            break;
        case 's':
            puts("[0] - 360   degrees (1   turn).");
            puts("[1] - 720   degrees (2   turn).");
            puts("[2] - 1440  degrees (4   turn).");
            puts("[3] - 2880  degrees (8   turn).");
            puts("[4] - 5760  degrees (16  turn).");
            puts("[5] - 11520 degrees (32  turn).");
            puts("[6] - 23040 degrees (64  turn).");
            puts("[7] - 46080 degrees (128 turn).");
            puts("[8] - 92160 degrees (256 turn).");
            puts("");

            for (i = 0; i < NUM_OF_SENSORS; i++) {
                printf("\nChoose angle resolution for sensor #%d:\n", (i + 1));
                puts("");
                scanf("%d", &aux_int);
                global_args.pos_resolution[i] = aux_int;
            }


            global_args.flag_pos_resolution  = 1;
            break;
        case 'u':
            #if NUM_OF_SENSORS == 4
                sscanf( optarg,"%f,%f,%f,%f",
                    global_args.measurement_multiplier,
                    global_args.measurement_multiplier + 1,
                    global_args.measurement_multiplier + 2,
                    global_args.measurement_multiplier + 3 );
            #else
                sscanf( optarg,"%f,%f,%f",
                    global_args.measurement_multiplier,
                    global_args.measurement_multiplier + 1,
                    global_args.measurement_multiplier + 2 );
            #endif
            global_args.flag_meas_multiplier = 1;
            break;
        case 'o':
            #if NUM_OF_SENSORS == 4
                sscanf( optarg,"%hd,%hd,%hd,%hd",
                        global_args.measurement_offset,
                        global_args.measurement_offset + 1,
                        global_args.measurement_offset + 2,
                        global_args.measurement_offset + 3);
            #else
                sscanf( optarg,"%hd,%hd,%hd",
                        global_args.measurement_offset,
                        global_args.measurement_offset + 1,
                        global_args.measurement_offset + 2);
            #endif
            global_args.flag_meas_offset = 1;
            break;
        case 'p':
            global_args.flag_ping = 1;
            break;
        case 'r':
            global_args.flag_restore = 1;
            break;
        case 't':
            global_args.flag_serial_port = 1;
            break;
        case 'l':
            global_args.flag_list_devices = 1;
            break;
        case 'v':
            global_args.flag_verbose = 1;
            break;
        case 'q':
            global_args.flag_set_limit = 1;
            break;
        case 'w':
            sscanf(optarg, "%d", &aux_int);
            global_args.activate_limit = aux_int;
            global_args.flag_activate_limit = 1;
            break;
        case 'b':
            printf("Set the new values for max step:\nmax_step_pos: ");
            scanf("%d", &global_args.max_step_pos);
            printf("max_step_neg: ");
            scanf("%d", &global_args.max_step_neg);
            global_args.flag_max_step = 1;
            break;
        case 'c':
            sscanf(optarg, "%d", &aux_int);
            global_args.max_current = (int16_t)aux_int;
            global_args.flag_max_current = 1;
            break;
        case 'h':
        case '?':
        default:
            display_usage();
            return 0;
            break;
        }
    }

    if((optind == 1) | (global_args.flag_verbose & (optind == 2)))
    {
        display_usage();
        return 0;
    }


//======================================================     setting serial port

    if(global_args.flag_serial_port)
    {
        num_ports = RS485listPorts(ports);

        if(num_ports)
        {
            puts("\nChoose serial port:\n");
            for(i = 0; i < num_ports; ++i)
            {
                printf("[%d] - %s\n\nSerial port: ", i+1, ports[i]);
                scanf("%d", &aux_int);

                if( aux_int && (aux_int <= num_ports) )
                {
                    file = fopen(HANDMOVE_FILE, "w");
                    fprintf(file,"serialport %s",ports[aux_int-1]);
                    fclose(file);
                }
                else puts("Choice not available");

            }
        }
        else
            puts("No serial port available.");

        return 0;
    }

//===============================================     reading configuration file

    if(global_args.flag_verbose)
        puts("Reading configuration file.");

    file = fopen(HANDMOVE_FILE, "r");

    if (file == NULL) {
        printf("Error opening %s\n", HANDMOVE_FILE);
        return 0;
    }

    fscanf(file, "serialport %s", port_s);

    fclose(file);

    if(global_args.flag_verbose)
        printf("Serial port: %s.\n",port_s);


//======================================================     opening serial port

    if(global_args.flag_verbose)
        puts("Connecting to serial port.");

    openRS485(&comm_settings_t, port_s);

    if(comm_settings_t.file_handle == INVALID_HANDLE_VALUE)
    {
        puts("Couldn't connect to the serial port.");

        if(global_args.flag_verbose)
            puts("Closing the application.");

        return 0;
    }

    if(global_args.flag_verbose)
        puts("Serial port connected.");

//=============================================================     list devices


    if(global_args.flag_list_devices)
    {
        aux_int = RS485ListDevices(&comm_settings_t, list_of_devices);

        printf("\nNumber of devices: %d \n\n", aux_int);
        puts("List of devices:");

        for(i = 0; i < aux_int; ++i)
        {
            printf("%d\n",list_of_devices[i]);
        }
        return 0;
    }

//========================================================     gettind device id

    if (argc - optind == 1)
    {
        sscanf(argv[optind++],"%d",&global_args.device_id);
        if(global_args.flag_verbose)
            printf("Device ID:%d\n", global_args.device_id);
    }


//=====================================================================     ping

    // If ping... then DOESN'T PROCESS OTHER COMMANDS
    if(global_args.flag_ping)
    {
        if(global_args.flag_verbose)
            puts("Pinging serial port.");

        if(global_args.device_id){
            commGetInfo(&comm_settings_t, global_args.device_id, INFO_ALL, aux_string);
            }
        else
            RS485GetInfo(&comm_settings_t, aux_string);

        puts(aux_string);

        if(global_args.flag_verbose)
            puts("Closing the application.");

         return 0;
    }

//========================================================     ...

    if ((!(argc - optind == 1)) & (global_args.flag_verbose))
        puts("No device ID was chosen. Running in broadcasting mode.");


//=====================================================     setting parameter ID


    // If id is changed... then DOESN'T PROCESS OTHER COMMANDS
    if(global_args.flag_id)
    {
        if(global_args.flag_verbose)
            printf("Changing device's id %d to %d.\n", global_args.device_id ,global_args.new_id);

        commSetParam(&comm_settings_t, global_args.device_id,
            PARAM_ID, &global_args.new_id, 1);
        commStoreParams(&comm_settings_t, global_args.device_id);

        if(global_args.flag_verbose)
            puts("Closing the application.");

         return 0;
    }

//==================================================     setting PID parameter


    if(global_args.flag_pid_control)
    {
        if(global_args.flag_verbose) {
            printf("Changing PID control constant to:\n");
            printf("P: %f\n", global_args.pid_control[0]);
            printf("I: %f\n", global_args.pid_control[1]);
            printf("D: %f\n", global_args.pid_control[2]);
        }
        //da correggere

        commSetParam(&comm_settings_t, global_args.device_id,
            PARAM_PID_CONTROL, global_args.pid_control, 3);
        //usleep(100000);     //100 ms
        commStoreParams(&comm_settings_t, global_args.device_id);

        if(global_args.flag_verbose)
            puts("Closing the application.");

         return 0;
    }

//=====================================     setting parameter startup activation

    if(global_args.flag_activation)
    {
        if(global_args.flag_verbose)
            puts("Changing startup activation to on.");

        aux_uchar = 3;
        commSetParam(&comm_settings_t, global_args.device_id,
            PARAM_STARTUP_ACTIVATION, &aux_uchar, 1);
        commStoreParams(&comm_settings_t, global_args.device_id);

        if(global_args.flag_verbose)
            puts("Closing the application.");

        return 0;
    }

//=====================================     setting parameter startup activation

    if(global_args.flag_deactivation)
    {
        if(global_args.flag_verbose)
            puts("Changing startup activation to off.");

        aux_uchar = 0;
        commSetParam(&comm_settings_t, global_args.device_id,
            PARAM_STARTUP_ACTIVATION, &aux_uchar, 1);
        commStoreParams(&comm_settings_t, global_args.device_id);

        if(global_args.flag_verbose)
            puts("Closing the application.");

        return 0;
    }

//==============================================    setting parameter input mode

    if(global_args.flag_input_mode)
    {
        if(global_args.flag_verbose)
            puts("Changing input mode.");

        commSetParam(&comm_settings_t, global_args.device_id,
            PARAM_INPUT_MODE, &global_args.input_mode, 1);
        commStoreParams(&comm_settings_t, global_args.device_id);

        if(global_args.flag_verbose)
            puts("Closing the application.");

        return 0;
    }

//====================================     setting parameter position resolution

    if(global_args.flag_pos_resolution)
    {
        if(global_args.flag_verbose)
            puts("Changing angle resolution.");

        commSetParam(&comm_settings_t, global_args.device_id,
            PARAM_POS_RESOLUTION, global_args.pos_resolution, NUM_OF_SENSORS);
        commStoreParams(&comm_settings_t, global_args.device_id);

        if(global_args.flag_verbose)
            puts("Closing the application.");

        return 0;
    }

//====================================     setting parameter position multiplier

    if(global_args.flag_meas_multiplier)
    {
        if(global_args.flag_verbose)
            puts("Changing measurement position multipliers.");

        commSetParam(&comm_settings_t, global_args.device_id,
            PARAM_MEASUREMENT_MULTIPLIER, global_args.measurement_multiplier, NUM_OF_SENSORS);
        commStoreParams(&comm_settings_t, global_args.device_id);

        if(global_args.flag_verbose)
            puts("Closing the application.");

        return 0;
    }

//========================================     setting parameter position offset

    if(global_args.flag_meas_offset)
    {
        if(global_args.flag_verbose)
            puts("Changing position offset.");

        commSetParam(&comm_settings_t, global_args.device_id,
            PARAM_MEASUREMENT_OFFSET, global_args.measurement_offset, NUM_OF_SENSORS);
        commStoreParams(&comm_settings_t, global_args.device_id);

        if(global_args.flag_verbose)
            puts("Closing the application.");

        return 0;
    }

//=======================================     restore factory default parameters

    if(global_args.flag_restore)
    {
        if(global_args.flag_verbose)
            puts("Restoring factory default parameters.");

        commRestoreParams(&comm_settings_t, global_args.device_id);

        if(global_args.flag_verbose)
            puts("Closing the application.");

        return 0;
    }

//======================================================     set position limits

    if (global_args.flag_set_limit) {
        short int limits[4];
        printf("\nInf limit 1: ");
        scanf("%hd", limits);
        printf("\nSup limit 1: ");
        scanf("%hd", limits + 1);
        printf("\nInf limit 2: ");
        scanf("%hd", limits + 2);
        printf("\nSup limit 2: ");
        scanf("%hd", limits + 3);

        printf("limits: %d, %d, %d, %d\n", limits[0], limits[1], limits[2], limits[3]);
        commSetParam(&comm_settings_t, global_args.device_id,
            PARAM_POS_LIMIT, limits, 4);
        usleep(100000);
        commStoreParams(&comm_settings_t, global_args.device_id);
    }

//==================================================     activate position limit

    if (global_args.flag_activate_limit) {
        commSetParam(&comm_settings_t, global_args.device_id,
            PARAM_POS_LIMIT_FLAG, &(global_args.activate_limit), 1);
        commStoreParams(&comm_settings_t, global_args.device_id);
    }

//=============================================================     set max step

    if (global_args.flag_max_step) {
        commSetParam(&comm_settings_t, global_args.device_id,
            PARAM_MAX_STEP_POS, &(global_args.max_step_pos), 1);
        commSetParam(&comm_settings_t, global_args.device_id,
            PARAM_MAX_STEP_NEG, &(global_args.max_step_neg), 1);
        usleep(100000);
        commStoreParams(&comm_settings_t, global_args.device_id);
    }

    if (global_args.flag_max_current) {
        commSetParam(&comm_settings_t, global_args.device_id,
            PARAM_CURRENT_LIMIT, &(global_args.max_current), 1);
        commStoreParams(&comm_settings_t, global_args.device_id);
    }

//==========================     closing serial port and closing the application

    closeRS485(&comm_settings_t);

    if(global_args.flag_verbose)
        puts("Closing the application.");

    return 0;
}
예제 #5
0
파일: dpscore.c 프로젝트: BygoneWorlds/anet
/*-------------------------------------------------------------------------
 Report the score(s) for player dpId.  

 The idea is to pack all the score info of interest about a particular player
 into a compact buffer, then call this function.  This should be done
 periodically, e.g. after every major event.  

 You should use the first two bytes for a rough 'score', most significant
 byte first, and the third byte should be 0 normally, and 1 if this player
 has 'won'.
 e.g.
	buf[0] = dpGETSHORT_FIRSTBYTE(score);
	buf[1] = dpGETSHORT_SECONDBYTE(score);
	buf[2] = 0;
 Additional details can be appended.  You should write up a spec on how
 to interpret these bytes, and provide it to the game server administrators.

 scoreId should be zero.
-------------------------------------------------------------------------*/
DP_API dp_result_t DP_APIX dpReportScoreBuf(dp_t *dp, dpid_t dpId, int scoreId, const char *scorebuf, int scorelen)
{
	dp_uid_t uid;
	dp_result_t err;

	precondition(dp);
	ASSERTMEM();

#if 0
	if (dp->hGameServer == PLAYER_NONE) {
		commSetParamReq_t   req;
		commSetParamResp_t  resp; /* This is a dummy variable */

		memset(&req, 0, sizeof(commSetParamReq_t));
		req.param_num = scoreId;
		req.param_value = scoreVal;
		/* set a playerHdl to param_value2 base on the given dpId */
		req.param_value2 = (long)dpid2commHdl(dp, dpId); 
		req.reqLen = sizeof(commSetParamReq_t);
		commSetParam(&req, &resp);
		/* Driver usually returns status dp_RES_OK or dp_RES_UNIMPLEMENTED */
		DPRINT(("dpReportScoreBuf: driver reports err:%d for scoreId %d\n",
				resp.status, scoreId));
		return resp.status;  
	}
#endif

	uid = dpGetPlayerUid(dp, dpId);
	ASSERTMEM();
	if (uid == dp_UID_NONE) {
		DPRINT(("dpReportScoreBuf: player %d had no uid.  Not reporting.\n", dpId));
		return dp_RES_OK;
	}
	
	/* Create the score matrix for this session, if needed, and register our
	 * player and uid
	 */
	if (!dp->scorerep) {
		dpid_t mydpid;
		dp_uid_t myuid;

		dp->scorerep = scorerep_create();
		if (!dp->scorerep)
			return dp_RES_NOMEM;
		/* Identify us as any player created on this machine */
		{
			mydpid = dpGetMyId(dp);
			ASSERTMEM();
			myuid = tserv_hdl2uid(dp->tserv, PLAYER_ME);
			ASSERTMEM();
			DPRINT(("dpReportScoreBuf: first report; I am id:%d, uid:%d\n", mydpid, myuid));
			scorerep_setSelf(dp->scorerep, mydpid, myuid);
			ASSERTMEM();
		}
	}

	/* Place this score into the matrix */
	err = scorerep_set(dp->scorerep, dpId, uid, scoreId, scorebuf, scorelen);
	ASSERTMEM();
	if (err != dp_RES_OK) {
		DPRINT(("dpReportScoreBuf: scorerep_set returns err:%d\n", err));
		return err;
	}
	
	return dp_RES_OK;
}