Exemplo n.º 1
0
PSMoveCalibration *
psmove_calibration_new(PSMove *move)
{
    PSMove_Data_BTAddr addr;
    char *serial;
    int i;

    PSMoveCalibration *calibration =
        (PSMoveCalibration*)calloc(1, sizeof(PSMoveCalibration));

    calibration->move = move;

    if (psmove_connection_type(move) == Conn_USB) {
        _psmove_read_btaddrs(move, NULL, &addr);
        serial = _psmove_btaddr_to_string(addr);
    } else {
        serial = psmove_get_serial(move);
    }

    if (!serial) {
        psmove_CRITICAL("Could not determine serial from controller");
        free(calibration);
        return NULL;
    }

    for (i=0; i<strlen(serial); i++) {
        if (serial[i] == ':') {
            serial[i] = '_';
        }
    }

    char *template = malloc(strlen(serial) +
Exemplo n.º 2
0
ControllerGlue::ControllerGlue(int index, const std::string &serial, PSMove *handle_a, PSMove *handle_b)
    : move_bluetooth(nullptr)
    , move_usb(nullptr)
    , serial(serial)
    , controller()
    , connected(true)
    , sent_connect(false)
    , sent_disconnect(false)
{
    memset(&controller, 0, sizeof(controller));
    controller.index = index;
    controller.serial = this->serial.c_str();

    if (psmove_connection_type(handle_a) == Conn_USB) {
        move_bluetooth = handle_b;
        move_usb = handle_a;
    } else {
        move_bluetooth = handle_a;
        move_usb = handle_b;
    }

    controller.usb = (move_usb != nullptr);
    controller.bluetooth = (move_bluetooth != nullptr);

    if (controller.usb) {
        controller.battery = Batt_CHARGING;
    }
}
int main(int argc, char* argv[])
{
    PSMove *move;
    
    if (psmove_count_connected() == 0) {
        printf("No controllers connected.\n");
        return 2;
    }

    move = psmove_connect();
    if (move == NULL) {
        printf("Cannot connect to controller.\n");
        return 3;
    }


    printf("Serial: %s\n", psmove_get_serial(move));

    if (psmove_connection_type(move) == Conn_USB) {
        _psmove_get_firmware(move);
    }
    else {
        printf("Please connect the controller via USB.\n");
    }

    psmove_disconnect(move);
    return 0;
}
Exemplo n.º 4
0
int pair(const char *custom_addr)
{
	if (!psmove_init(PSMOVE_CURRENT_VERSION)) {
		fprintf(stderr, "PS Move API init failed (wrong version?)\n");
		exit(1);		
	}

    int count = psmove_count_connected();
    int i;
    PSMove *move;
    int result = 0;

    printf("Connected controllers: %d\n", count);

    for (i=0; i<count; i++) {
        move = psmove_connect_by_id(i);

        if (move == NULL) {
            printf("Error connecting to PSMove #%d\n", i+1);
            result = 1;
            continue;
        }

        if (psmove_connection_type(move) != Conn_Bluetooth) {
            printf("PSMove #%d connected via USB.\n", i+1);
            int result = 0;

            if (custom_addr != NULL) {
                result = psmove_pair_custom(move, custom_addr);
            } else {
                result = psmove_pair(move);
            }

            if (result) {
                printf("Pairing of #%d succeeded!\n", i+1);
                char *serial = psmove_get_serial(move);
                printf("Controller address: %s\n", serial);
                free(serial);
            } else {
                printf("Pairing of #%d failed.\n", i+1);
            }

            if (psmove_has_calibration(move)) {
                printf("Calibration data available and saved.\n");
            } else {
                printf("Error reading/loading calibration data.\n");
            }
        } else {
            printf("Ignoring non-USB PSMove #%d\n", i+1);
        }

        psmove_disconnect(move);
    }

	psmove_shutdown();

    return result;
}
Exemplo n.º 5
0
int
PSMoveQt::connectionType() const
{
    switch (psmove_connection_type(_move)) {
        case Conn_USB:
            return PSMoveQt::USB;
        case Conn_Bluetooth:
            return PSMoveQt::Bluetooth;
        default:
            return PSMoveQt::Unknown;
    }
}
Exemplo n.º 6
0
int main(int argc, char* argv[])
{
    PSMove *move;
    int i;

    if (psmove_count_connected() == 0) {
        printf("No controllers connected.\n");
        return 1;
    }

    move = psmove_connect();
    if (move == NULL) {
        printf("Cannot connect to controller.\n");
        return 2;
    }

    printf("Connection established.\n");
    printf("Serial: %s\n", psmove_get_serial(move));

    if (psmove_connection_type(move) != Conn_Bluetooth) {
        printf("Controller must be connected via Bluetooth.\n");
        return 3;
    }

    PSMove_Data_AuthChallenge challenge = {
        0x02, 0x00, 0x1D, 0x91, 0xE5, 0x81, 0x30, 0x6A,
        0x22, 0x9A, 0xAB, 0x2E, 0x80, 0xB4, 0xED, 0x2E,
        0xDE, 0x40, 0x0A, 0xF0, 0x02, 0xB0, 0x42, 0x8B,
        0x01, 0x41, 0xB2, 0xA4, 0x3D, 0xE7, 0xD4, 0xBF,
        0x05, 0x92
    };

    challenge[0] = 0;
    send_and_receive(move, &challenge);

    challenge[0] = 1;
    send_and_receive(move, &challenge);

    for (i = 1; i < 0x10; i++) {
        challenge[0] = 0;
		challenge[1] = (unsigned char)i;
        send_and_receive(move, &challenge);
    }

    memset(challenge, 0, sizeof(challenge));
    send_and_receive(move, &challenge);

    psmove_disconnect(move);
    return 0;
}
Exemplo n.º 7
0
        void run()
        {
            PSMove *move;

            /* 6 values = ax, ay, az, gx, gy, gz (accel + gyro) */
            float gx, gy, gz;

            int i = 0;
            qreal rate = 1.;

            assert((move = psmove_connect()) != NULL);
            assert(psmove_connection_type(move) == Conn_Bluetooth);

            assert(psmove_has_calibration(move));

            while (true) {
                if (psmove_poll(move)) {
                    psmove_get_gyroscope_frame(move, Frame_SecondHalf, &gx, &gy, &gz);

                    /**
                     * Playback rate is calculated based on the RPM of the
                     * controller on the Z axis and the normal
                     * playback rate of the turntable (see above).
                     **/
                    rate = RAD_PER_S_TO_RPM(gz) / TURNTABLE_RPM;

                    /* Rate-limiting the updates of the playback rate */
                    if (i % READ_FRAME_UPDATE_RATE == 0) {
                        /**
                         * On Linux, the rate must not be negative. It might be
                         * possible that on Mac OS X or Windows, the rate can
                         * also be negative (depends on the backend).
                         **/
                        if (rate > 0 && rate != player->playbackRate()) {
                            qint64 pos = player->position();
                            player->setPlaybackRate(rate);
                            player->setPosition(pos);
                        }
                    }

                    i++;
                }
            }

            psmove_disconnect(move);
        }
Exemplo n.º 8
0
int main( void )
{
	// get number of attached controllers
	int count = psmove_count_connected();
	if( count < 1 )
	{
		printf( "No controller connected.\n" );
		return 0;
	}

	printf( "Number of connected controllers: %d\n", count );

	// connect to the controller
	PSMove* move = psmove_connect();
	if( ! move )
	{
		printf( "Cannot connect to controller.\n" );
		return 0;
	}

	// print the connection type
	printf( "Connection type: " );
	switch( psmove_connection_type( move ) )
	{
		case Conn_Bluetooth:
			printf( "Bluetooth" );
			break;

		case Conn_USB:
			printf( "USB" );
			break;

		default:
			printf( "Unknown" );
	}
	printf( "\n" );

	// send and receive some different Feature Reports
	test_send_auth( move );
	test_send_led( move, 255, 255, 0 );
	test_read_auth( move );

	psmove_disconnect( move );

	return 0;
}
Exemplo n.º 9
0
int
main(int argc, char* argv[])
{
    PSMove *move;

	if (!psmove_init(PSMOVE_CURRENT_VERSION)) {
		fprintf(stderr, "PS Move API init failed (wrong version?)\n");
		exit(1);
	}

    move = psmove_connect();

    if (move == NULL) {
        fprintf(stderr, "Could not connect to controller.\n");
        return EXIT_FAILURE;
    }

    assert(psmove_has_calibration(move));

    if (psmove_connection_type(move) == Conn_Bluetooth) {
        float ax, ay, az, gx, gy, gz;

        while (1) {
            int res = psmove_poll(move);
            if (res) {
                psmove_get_accelerometer_frame(move, Frame_SecondHalf,
                        &ax, &ay, &az);
                psmove_get_gyroscope_frame(move, Frame_SecondHalf,
                        &gx, &gy, &gz);

                printf("A: %5.2f %5.2f %5.2f ", ax, ay, az);
                printf("G: %6.2f %6.2f %6.2f ", gx, gy, gz);
                printf("\n");
            }
        }
    }

    psmove_disconnect(move);
	psmove_shutdown();

    return EXIT_SUCCESS;
}
Exemplo n.º 10
0
int main(int argc, char *argv[])
{
    if (!psmove_init(PSMOVE_CURRENT_VERSION)) {
        fprintf(stderr, "PS Move API init failed (wrong version?)\n");
        exit(1);
    }

    /* Check if at least one controller is attached */
    if (psmove_count_connected() < 1) {
        printf("No Move controller attached.\n");
        exit(1);
    }

    PSMove *move = psmove_connect();
    if(move == NULL) {
        printf("Could not connect to default Move controller.\n");
        exit(1);
    }

    /* Make sure we are connected via Bluetooth */
    if (psmove_connection_type(move) != Conn_Bluetooth) {
        printf("Controller must be connected via Bluetooth.\n");
        psmove_disconnect(move);
        exit(1);
    }

    unsigned int freq_idx = 0;

    while (!(psmove_get_buttons(move) & Btn_PS)) {
        if (!psmove_poll(move)) {
            continue;
        }

        unsigned int pressed_buttons;
        psmove_get_button_events(move, &pressed_buttons, NULL);

        unsigned long frequency = freqs[freq_idx];

        /* Cycle through the list of frequencies */
        if (pressed_buttons & Btn_CROSS) {
            freq_idx = (freq_idx + 1) % NUM_FREQS;
            frequency = freqs[freq_idx];
            printf("Selecting frequency %lu Hz. Press SQUARE to apply.\n", frequency);
        }

        /* Apply the currently selected frequency as new PWM frequency */
        if (pressed_buttons & Btn_SQUARE) {
            printf("Setting LED PWM frequency to %lu Hz.\n", frequency);

            /*
               Switch off the LEDs. If we do not do this, changing the PWM
               frequency will switch off the LEDs and keep them off until
               the Bluetooth connection has been teared down (at least
               once) and then reestablished.
            */
            psmove_set_leds(move, 0, 0, 0);
            psmove_update_leds(move);

            /*
               TODO:
               How can we make sure the LEDs are really off before changing
               the PWM frequency? It seems to work in all tests so far, but
               a proper verification would be nice.
            */
            if (!psmove_set_led_pwm_frequency(move, frequency)) {
                printf("Failed to set LED PWM frequency.\n");
            }
        }

        /* Keep fixed color for visual feedback */
        psmove_set_leds(move, 0, 0, 63);
        psmove_update_leds(move);
    }

    psmove_disconnect(move);
	psmove_shutdown();

    return 0;
}
Exemplo n.º 11
0
int main(int argc, char *argv[])
{
    if (!psmove_init(PSMOVE_CURRENT_VERSION)) {
        fprintf(stderr, "PS Move API init failed (wrong version?)\n");
        exit(1);
    }

    /* check if at least one controller is attached */
    if (psmove_count_connected() < 1) {
        printf("No Move controller attached.\n");
        exit(1);
    }

    PSMove *move = psmove_connect();
    if(move == NULL) {
        printf("Could not connect to default Move controller.\n");
        exit(1);
    }

    /* make sure we are connected via Bluetooth */
    if (psmove_connection_type(move) != Conn_Bluetooth) {
        printf("Controller must be connected via Bluetooth.\n");
        psmove_disconnect(move);
        exit(1);
    }

    int ext_connected = 0;
    enum Extension_Device ext_device = Ext_Unknown;

    while (!(psmove_get_buttons(move) & Btn_PS)) {
        if (!psmove_poll(move)) {
            continue;
        }

        if(psmove_is_ext_connected(move)) {
            /* if the extension device was not connected before, report connect */
            if (!ext_connected) {
                PSMove_Ext_Device_Info ext;
                enum PSMove_Bool success = psmove_get_ext_device_info(move, &ext);

                if (success) {
                    switch (ext.dev_id) {
                        case EXT_DEVICE_ID_SHARP_SHOOTER:
                            ext_device = Ext_Sharp_Shooter;
                            printf("Sharp Shooter extension connected!\n");
                            break;
                        case EXT_DEVICE_ID_RACING_WHEEL:
                            ext_device = Ext_Racing_Wheel;
                            printf("Racing Wheel extension connected!\n");
                            break;
                        default:
                            ext_device = Ext_Unknown;
                            printf("Unknown extension device (id 0x%04X) connected!\n", ext.dev_id);
                            break;
                    }
                }
                else {
                    printf("Unknown extension device connected! Failed to get device info.\n");
                }
            }

            ext_connected = 1;

            /* handle button presses etc. for a known extension device only */
            PSMove_Ext_Data data;
            if (psmove_get_ext_data(move, &data)) {
                unsigned int move_buttons = psmove_get_buttons(move);

                switch (ext_device) {
                    case Ext_Sharp_Shooter:
                        handle_sharp_shooter(&data, move_buttons);
                        break;
                    case Ext_Racing_Wheel:
                        handle_racing_wheel(move, &data, move_buttons);
                        break;
                    default:
                        break;
                }
            }
        } else {
            /* if the extension device was connected before, report disconnect */
            if (ext_connected) {
                printf("Extension device disconnected!\n");
            }

            ext_connected = 0;
        }
    }

    psmove_disconnect(move);

    return 0;
}
Exemplo n.º 12
0
int main(int argc, char* argv[])
{
    PSMove *move;
    enum PSMove_Connection_Type ctype;
    int i;

    i = psmove_count_connected();
    printf("Connected controllers: %d\n", i);

    move = psmove_connect();

    if (move == NULL) {
        printf("Could not connect to default Move controller.\n"
               "Please connect one via USB or Bluetooth.\n");
        exit(1);
    }

    ctype = psmove_connection_type(move);
    switch (ctype) {
        case Conn_USB:
            printf("Connected via USB.\n");
            break;
        case Conn_Bluetooth:
            printf("Connected via Bluetooth.\n");
            break;
        case Conn_Unknown:
            printf("Unknown connection type.\n");
            break;
    }


    if (ctype == Conn_USB) {
        PSMove_Data_BTAddr addr;
        psmove_read_btaddrs(move, &addr, NULL);
        printf("Current BT Host: ");
        for (i=0; i<6; i++) {
            printf("%02x ", addr[i]);
        }
        printf("\n");

#if 0
        /* This is the easy method (pair to this host): */
        if (psmove_pair(move)) {
            printf("Paired. Press the PS Button now :)\n");
        } else {
            printf("psmove_pair() failed :/\n");
        }

        /* This is the advanced method: */

        /* Example BT Address: 01:23:45:67:89:ab */
        const PSMove_Data_BTAddr newhost = {
            0x01, 0x23, 0x45, 0x67, 0x89, 0xab,
        };
        if (!psmove_set_btaddr(move, &newhost)) {
            printf("Could not set BT address!\n");
        }
#endif
    }

    for (i=0; i<10; i++) {
        psmove_set_leds(move, 0, 255*(i%3==0), 0);
        psmove_set_rumble(move, 255*(i%2));
        psmove_update_leds(move);
        usleep(10000*(i%10));
    }

    for (i=250; i>=0; i-=5) {
        psmove_set_leds(move, i, i, 0);
        psmove_set_rumble(move, 0);
        psmove_update_leds(move);
    }

    /* Enable rate limiting for LED updates */
    psmove_set_rate_limiting(move, 1);

    psmove_set_leds(move, 0, 0, 0);
    psmove_set_rumble(move, 0);
    psmove_update_leds(move);

    while (!(psmove_get_buttons(move) & Btn_PS)) {
        int res = psmove_poll(move);
        if (res) {
            if (psmove_get_buttons(move) & Btn_TRIANGLE) {
                printf("Triangle pressed, with trigger value: %d\n",
                        psmove_get_trigger(move));
                psmove_set_rumble(move, psmove_get_trigger(move));
            } else {
                psmove_set_rumble(move, 0x00);
            }

            psmove_set_leds(move, 0, 0, psmove_get_trigger(move));

            int x, y, z;
            psmove_get_accelerometer(move, &x, &y, &z);
            printf("accel: %5d %5d %5d\n", x, y, z);
            psmove_get_gyroscope(move, &x, &y, &z);
            printf("gyro: %5d %5d %5d\n", x, y, z);
            psmove_get_magnetometer(move, &x, &y, &z);
            printf("magnetometer: %5d %5d %5d\n", x, y, z);
            printf("buttons: %x\n", psmove_get_buttons(move));

            int battery = psmove_get_battery(move);

            if (battery == Batt_CHARGING) {
                printf("battery charging\n");
            } else if (battery >= Batt_MIN && battery <= Batt_MAX) {
                printf("battery level: %d / %d\n", battery, Batt_MAX);
            } else {
                printf("battery level: unknown (%x)\n", battery);
            }

            printf("temperature: %d\n", psmove_get_temperature(move));

            psmove_update_leds(move);
        }
    }

    psmove_disconnect(move);

    return 0;
}
Exemplo n.º 13
0
//-- public methods ----
int
main(int arg, char** args)
{
	if (!psmove_init(PSMOVE_CURRENT_VERSION)) {
		fprintf(stderr, "PS Move API init failed (wrong version?)\n");
		exit(1);
	}

    int i;
    int count = psmove_count_connected();

    printf("Connected controllers for calibration: %d\n", count);

    for (i=0; i<count; i++) 
	{
        PSMove *move = psmove_connect_by_id(i);

        if (move == NULL)
        {
            printf("Failed to open PS Move #%d\n", i);
            continue;
        }

		// Make sure accelerometer is calibrated
		// Otherwise we can't tell if the controller is sitting upright
		if (psmove_has_calibration(move))
		{
			if (psmove_connection_type(move) == Conn_Bluetooth)
			{
				float old_range = 0;
				enum eCalibrationState calibration_state = Calibration_MeasureBExtents;

				// Reset existing magnetometer calibration state
				psmove_reset_magnetometer_calibration(move);

				printf("Calibrating PS Move #%d\n", i);
				printf("[Step 1 of 2: Measuring extents of the magnetometer]\n");
				printf("Rotate the controller in all directions.\n");
				fflush(stdout);

				while (calibration_state == Calibration_MeasureBExtents)
				{
					if (psmove_poll(move))
					{
						unsigned int pressed, released;
						psmove_get_button_events(move, &pressed, &released);

						/* This call updates min/max values if exceeding previously stored values */
						psmove_get_magnetometer_3axisvector(move, NULL);

						/* Returns the minimum delta (max-min) across dimensions (x, y, z) */
						float range = psmove_get_magnetometer_calibration_range(move);

						/* Update the LEDs to indicate progress */
						int percentage = (int)(100.f * range / LED_RANGE_TARGET);
						if (percentage > 100)
						{
							percentage = 100;
						}
						else if (percentage < 0)
						{
							percentage = 0;
						}

						psmove_set_leds(
							move,
							(unsigned char)((255 * (100 - percentage)) / 100),
							(unsigned char)((255 * percentage) / 100),
							0);
						psmove_update_leds(move);

						if (pressed & Btn_MOVE)
						{
							psmove_set_leds(move, 0, 0, 0);
							psmove_update_leds(move);

							// Move on to the 
							calibration_state = Calibration_WaitForGravityAlignment;
						}
						else if (range > old_range)
						{
							old_range = range;
							printf("\rPress the MOVE button when value stops changing: %.1f", range);
							fflush(stdout);
						}
					}
				}

				printf("\n\n[Step 2 of 2: Measuring default magnetic field direction]\n");
				printf("Stand the controller on a level surface with the Move button facing you.\n");
				printf("This will be the default orientation of the move controller.\n");
				printf("Measurement will start once the controller is aligned with gravity and stable.\n");
				fflush(stdout);

				//TODO: Wait for accelerometer stabilization and button press
				// Sample the magnetometer field for several frames and average
				// Store as the default magnetometer direction
				while (calibration_state != Calibration_Complete)
				{
					int stable_start_time = psmove_util_get_ticks();
					PSMove_3AxisVector identity_pose_average_m_vector = *k_psmove_vector_zero;
					int sample_count = 0;

					while (calibration_state == Calibration_WaitForGravityAlignment)
					{
						if (psmove_poll(move))
						{
							if (is_move_stable_and_aligned_with_gravity(move))
							{
								int current_time = psmove_util_get_ticks();
								int stable_duration = (current_time - stable_start_time);

								if ((current_time - stable_start_time) >= STABILIZE_WAIT_TIME_MS)
								{
									calibration_state = Calibration_MeasureBDirection;
								}
								else
								{
									printf("\rStable for: %dms/%dms                        ", stable_duration, STABILIZE_WAIT_TIME_MS);
								}
							}
							else
							{
								stable_start_time = psmove_util_get_ticks();
								printf("\rMove Destabilized! Waiting for stabilization.");
							}
						}
					}

					printf("\n\nMove stabilized. Starting magnetometer sampling.\n");
					while (calibration_state == Calibration_MeasureBDirection)
					{
						if (psmove_poll(move))
						{
							if (is_move_stable_and_aligned_with_gravity(move))
							{
								PSMove_3AxisVector m;

								psmove_get_magnetometer_3axisvector(move, &m);
								psmove_3axisvector_normalize_with_default(&m, k_psmove_vector_zero);

								identity_pose_average_m_vector = psmove_3axisvector_add(&identity_pose_average_m_vector, &m);
								sample_count++;

								if (sample_count > DESIRED_MAGNETOMETER_SAMPLE_COUNT)
								{
									float N = (float)sample_count;

									// The average magnetometer direction was recorded while the controller
									// was in the cradle pose
									identity_pose_average_m_vector = psmove_3axisvector_divide_by_scalar_unsafe(&identity_pose_average_m_vector, N);

									psmove_set_magnetometer_calibration_direction(move, &identity_pose_average_m_vector);

									calibration_state = Calibration_Complete;
								}
								else
								{
									printf("\rMagnetometer Sample: %d/%d                   ", sample_count, DESIRED_MAGNETOMETER_SAMPLE_COUNT);
								}
							}
							else
							{
								calibration_state = Calibration_WaitForGravityAlignment;
								printf("\rMove Destabilized! Waiting for stabilization.\n");
							}
						}
					}
				}

				psmove_save_magnetometer_calibration(move);
			}
			else
			{
				printf("Ignoring non-Bluetooth PS Move #%d\n", i);
			}
		}
		else
		{			
			char *serial= psmove_get_serial(move);

			printf("\nController #%d has bad calibration file (accelerometer values won't be correct).\n", i);

			if (serial != NULL)
			{
				printf("Please delete %s.calibration and re-run \"psmove pair\" with the controller plugged into usb.", serial);
				free(serial);
			}
			else
			{
				printf("Please re-run \"psmove pair\" with the controller plugged into usb.");
			}
		}

        printf("\nFinished PS Move #%d\n", i);
        psmove_disconnect(move);
    }

    return 0;
}
Exemplo n.º 14
0
PSMoveCalibration *
psmove_calibration_new(PSMove *move)
{
    PSMove_Data_BTAddr addr;
    char *serial;
    int i;

    PSMoveCalibration *calibration =
        (PSMoveCalibration*)calloc(1, sizeof(PSMoveCalibration));

    calibration->move = move;

    if (psmove_connection_type(move) == Conn_USB) {
        _psmove_read_btaddrs(move, NULL, &addr);
        serial = _psmove_btaddr_to_string(addr);
    } else {
        serial = psmove_get_serial(move);
    }

    if (!serial) {
        psmove_CRITICAL("Could not determine serial from controller");
        free(calibration);
        return NULL;
    }

    for (i = 0; i<(int)strlen(serial); i++) {
        if (serial[i] == ':') {
            serial[i] = '_';
        }
    }

    size_t calibration_filename_length = strlen(serial) + strlen(PSMOVE_CALIBRATION_EXTENSION) + 1;
    char *calibration_filename = (char *)malloc(calibration_filename_length);
    strcpy(calibration_filename, serial);
    strcat(calibration_filename, PSMOVE_CALIBRATION_EXTENSION);

    calibration->filename = psmove_util_get_file_path(calibration_filename);
    calibration->system_filename = psmove_util_get_system_file_path(calibration_filename);

    free(calibration_filename);
    free(serial);

    /* Try to load the calibration data from disk, or from USB */
    psmove_calibration_load(calibration);
    if (!psmove_calibration_supported(calibration)) {
        if (psmove_connection_type(move) == Conn_USB) {
            psmove_DEBUG("Storing calibration from USB\n");
            psmove_calibration_read_from_usb(calibration);
            psmove_calibration_save(calibration);
        }
    }

    /* Pre-calculate values used for mapping input */
    if (psmove_calibration_supported(calibration)) {
        /* Accelerometer reading (high/low) for each axis */
        int axlow, axhigh, aylow, ayhigh, azlow, azhigh;
        psmove_calibration_get_usb_accel_values(calibration,
                &axlow, &axhigh, &aylow, &ayhigh, &azlow, &azhigh);

        /**
         *
         * Calculation of accelerometer mapping (as factor of gravity, 1g):
         *
         *                2 * (raw - low)
         *  calibrated = ----------------  - 1
         *                 (high - low)
         *
         * with:
         *
         *  raw .... Raw sensor reading
         *  low .... Raw reading at -1g
         *  high ... Raw reading at +1g
         *
         * Now define:
         *
         *            2
         *  f = --------------
         *       (high - low)
         *
         * And combine constants:
         *
         *  c = - (f * low) - 1
         *
         * Then we get:
         *
         *  calibrated = f * raw + c
         *
         **/

        /* Accelerometer factors "f" */
        calibration->ax = 2.f / (float)(axhigh - axlow);
        calibration->ay = 2.f / (float)(ayhigh - aylow);
        calibration->az = 2.f / (float)(azhigh - azlow);

        /* Accelerometer constants "c" */
        calibration->bx = - (calibration->ax * (float)axlow) - 1.f;
        calibration->by = - (calibration->ay * (float)aylow) - 1.f;
        calibration->bz = - (calibration->az * (float)azlow) - 1.f;

        /**
         * Calculation of gyroscope mapping (in radiant per second):
         *
         *                 raw
         *  calibrated = -------- * 2 PI
         *                rpm60
         *
         *           60 * rpm80
         *  rpm60 = ------------
         *               80
         *
         * with:
         *
         *  raw ..... Raw sensor reading
         *  rpm80 ... Sensor reading at 80 RPM (from calibration blob)
         *  rpm60 ... Sensor reading at 60 RPM (1 rotation per second)
         *
         * Or combined:
         *
         *                80 * raw * 2 PI
         *  calibrated = -----------------
         *                  60 * rpm80
         *
         * Now define:
         *
         *       2 * PI * 80
         *  f = -------------
         *        60 * rpm80
         *
         * Then we get:
         *
         *  calibrated = f * raw
         *
         **/

        int gx80, gy80, gz80;
        psmove_calibration_get_usb_gyro_values(calibration,
                &gx80, &gy80, &gz80);

        float factor = (float)(2.0 * M_PI * 80.0) / 60.0;
        calibration->gx = factor / (float)gx80;
        calibration->gy = factor / (float)gy80;
        calibration->gz = factor / (float)gz80;
    } else {
        /* No calibration data - pass-through input data */
        calibration->ax = 1.f;
        calibration->ay = 1.f;
        calibration->az = 1.f;

        calibration->bx = 0.f;
        calibration->by = 0.f;
        calibration->bz = 0.f;

        calibration->gx = 1.f;
        calibration->gy = 1.f;
        calibration->gz = 1.f;
    }

    return calibration;
}
Exemplo n.º 15
0
int main(int argc, char* argv[])
{
    PSMove *move;
    enum PSMove_Connection_Type ctype;
    int i;

    if (!psmove_init(PSMOVE_CURRENT_VERSION)) {
        fprintf(stderr, "PS Move API init failed (wrong version?)\n");
        exit(1);
    }

    i = psmove_count_connected();
    printf("Connected controllers: %d\n", i);

    move = psmove_connect();

    if (move == NULL) {
        printf("Could not connect to default Move controller.\n"
               "Please connect one via USB or Bluetooth.\n");
        exit(1);
    }

    char *serial = psmove_get_serial(move);
    printf("Serial: %s\n", serial);
    free(serial);

    ctype = psmove_connection_type(move);
    switch (ctype) {
        case Conn_USB:
            printf("Connected via USB.\n");
            break;
        case Conn_Bluetooth:
            printf("Connected via Bluetooth.\n");
            break;
        case Conn_Unknown:
            printf("Unknown connection type.\n");
            break;
    }

    for (i=0; i<10; i++) {
        psmove_set_leds(move, 0, 255*(i%3==0), 0);
        psmove_set_rumble(move, 255*(i%2));
        psmove_update_leds(move);
        psmove_usleep(10000 * (i % 10));
    }

    for (i=250; i>=0; i-=5) {
        psmove_set_leds(move, i, i, 0);
        psmove_set_rumble(move, 0);
        psmove_update_leds(move);
    }

    /* Enable rate limiting for LED updates */
    psmove_set_rate_limiting(move, 1);

    psmove_set_leds(move, 0, 0, 0);
    psmove_set_rumble(move, 0);
    psmove_update_leds(move);

    while (ctype != Conn_USB && !(psmove_get_buttons(move) & Btn_PS)) {
        int res = psmove_poll(move);
        if (res) {
            if (psmove_get_buttons(move) & Btn_TRIANGLE) {
                printf("Triangle pressed, with trigger value: %d\n",
                        psmove_get_trigger(move));
                psmove_set_rumble(move, psmove_get_trigger(move));
            } else {
                psmove_set_rumble(move, 0x00);
            }

            psmove_set_leds(move, 0, 0, psmove_get_trigger(move));

            int x, y, z;
            psmove_get_accelerometer(move, &x, &y, &z);
            printf("accel: %5d %5d %5d\n", x, y, z);
            psmove_get_gyroscope(move, &x, &y, &z);
            printf("gyro: %5d %5d %5d\n", x, y, z);
            psmove_get_magnetometer(move, &x, &y, &z);
            printf("magnetometer: %5d %5d %5d\n", x, y, z);
            printf("buttons: %x\n", psmove_get_buttons(move));

            int battery = psmove_get_battery(move);

            if (battery == Batt_CHARGING) {
                printf("battery charging\n");
            } else if (battery == Batt_CHARGING_DONE) {
                printf("battery fully charged (on charger)\n");
            } else if (battery >= Batt_MIN && battery <= Batt_MAX) {
                printf("battery level: %d / %d\n", battery, Batt_MAX);
            } else {
                printf("battery level: unknown (%x)\n", battery);
            }

            printf("raw temperature: %d\n", psmove_get_temperature(move));
            printf("celsius temperature: %f\n", psmove_get_temperature_in_celsius(move));

            psmove_update_leds(move);
        }
    }

    psmove_disconnect(move);
    psmove_shutdown();
    return 0;
}
Exemplo n.º 16
0
bool MoveHandler::connectControllers()
{
	std::stringstream string;
	enum PSMove_Connection_Type connectionType;

	logger->LogEvent("Initializeing controller(s)...");
	
	this->connections = psmove_count_connected();

	if(this->connections == 0)
	{
		logger->LogEvent("No controllers connected. \nAborting!");
		return false;
	}
	
	for(int i = 0; i < this->connections; i++)
	{
		PSMove* connection = nullptr;
		unsigned int buttons = 0;

		this->controllers.push_back(connection);
		this->buttons.push_back(buttons);
	}

	string << "Move connections: " << this->connections;
	logger->LogEvent(string.str());

	for(int i = 0; i < this->connections; i++)
	{
		string.str("");
		string << "Connecting to connection #" << i;
		logger->LogEvent(string.str());
		
		this->controllers[i] = psmove_connect_by_id(i);

		string.str("");
		if(this->controllers[i] == nullptr || this->controllers[i] == NULL)
		{
			string << "Could not connect to connection #" << i << "\nAborting!";		
			return false;
		}
		else
		{
			string << "Connected to connection #" << i;
		}
		logger->LogEvent(string.str());
		
		connectionType = psmove_connection_type(this->controllers[i]);
		string.str("");
		string << "Connection #" << i << " connection type: ";
		if(connectionType==Conn_Bluetooth)
		{
			string << "Bluetooth";
			this->BTController = this->controllers[i]; 
		}
		else
		{
			string << "USB";
			this->USBController = this->controllers[i];
		}	
		logger->LogEvent(string.str());
	}

    logger->LogEvent("Controller initialization finished");
    return true;
}