Sunday, July 19, 2020

Basics on the code PART 8

Today we are going to see the motion sensor sample, which is about the ps vita's orientation, speed, magnetometer...

To understand the functions we have to use the vitasdk documentation, for this specific sample we will use the motion section

Reminder: the threshold is the magnitude or intensity that must be exceeded for a certain reaction, phenomenon, result, or condition to occur or be manifested.

SAMPLE "motion"

motion: Prints accelerometer data.


#include <stdio.h>
#include <stdbool.h>
#include <psp2/kernel/processmgr.h>
#include <psp2/ctrl.h>
#include <psp2/motion.h>

#include "debugScreen.h"

#define printf psvDebugScreenPrintf


// main program

int main(){
    psvDebugScreenInit();         // needed for the prints to show on the vita screen.  
   
    SceCtrlData ctrl;                   // used to read the buttons (used when options are offered in the sample)

    float threshold;                     // used to have a base number for the sensor data to be displayed   
 
    bool is_sampling=false;       // used to know if we are sampling the data or not


// loop for sampling

    while(1){

// control questions to set up the sampling and to quit

        sceCtrlReadBufferPositive(0, &ctrl, 1);
        printf("\e[60;35HPress Start to quit\n");
        if(ctrl.buttons & SCE_CTRL_START)
            break;
        printf("\e[H");/*reset the cursor position to 0,0*/
       
        printf("Sampling:%3s (X:ON, O:OFF)\n",is_sampling?"ON":"OFF");
        if((ctrl.buttons & SCE_CTRL_CROSS) && !is_sampling)
            is_sampling=(sceMotionStartSampling()==0);
        if((ctrl.buttons & SCE_CTRL_CIRCLE) && is_sampling)
            is_sampling=(sceMotionStopSampling()!=0);

        printf("Deadband:%3s ([]: ON, /\\:OFF)\n",sceMotionGetDeadband()?"ON":"OFF");
        if(ctrl.buttons & SCE_CTRL_SQUARE)
            sceMotionSetDeadband(1);
        if(ctrl.buttons & SCE_CTRL_TRIANGLE)
            sceMotionSetDeadband(0);

        threshold = sceMotionGetAngleThreshold();
        printf("AngleThreshold:%+1.3f (Up:+=0.1,Down:-=0.1)\n",threshold);
        if(ctrl.buttons & SCE_CTRL_UP)
            sceMotionSetAngleThreshold(threshold+0.1f);
        if(ctrl.buttons & SCE_CTRL_DOWN)
            sceMotionSetAngleThreshold(threshold-0.1f);

        printf("TiltCorrection:%i (RIGHT:ON, LEFT:OFF)\n",sceMotionGetTiltCorrection());
        if(ctrl.buttons & SCE_CTRL_LEFT)
            sceMotionSetTiltCorrection(0);
        if(ctrl.buttons & SCE_CTRL_RIGHT)
            sceMotionSetTiltCorrection(1);

        printf("\n");
        /* no need to further if we are not sampling */
        if(!is_sampling)
            continue;
           
/* Here we read all the data from the motion sensor in the vita and use printf to display it on the screen */


        printf("Magnetometer:%3s (L:ON, R:OFF)\n",sceMotionGetMagnetometerState()?"ON":"OFF");
        if(ctrl.buttons & SCE_CTRL_LTRIGGER)
            sceMotionMagnetometerOn();
        if(ctrl.buttons & SCE_CTRL_RTRIGGER)
            sceMotionMagnetometerOff();

        printf("\nPress Select to calibrate as Origin\n");
        if(ctrl.buttons & SCE_CTRL_SELECT)
            sceMotionReset();

        SceMotionState state;
        sceMotionGetState(&state);
        printf("Acceleration <x:%+1.3f y:%+1.3f z:%+1.3f>     \n",state.acceleration.x, state.acceleration.y, state.acceleration.z);
        printf("Ang.Velocity <x:%+1.3f y:%+1.3f z:%+1.3f>     \n",state.angularVelocity.x, state.angularVelocity.y, state.angularVelocity.z);
        printf("Orientation  <x:%+1.3f y:%+1.3f z:%+1.3f>     \n",state.basicOrientation.x, state.basicOrientation.y, state.basicOrientation.z);
        printf("Device       <x:%+1.3f y:%+1.3f z:%+1.3f w:%+1.3f>\n",state.deviceQuat.x, state.deviceQuat.y, state.deviceQuat.z, state.deviceQuat.w);
        printf("Rotation.X   <x:%+1.3f y:%+1.3f z:%+1.3f w:%+1.3f>\n",state.rotationMatrix.x.x, state.rotationMatrix.x.y, state.rotationMatrix.x.z, state.rotationMatrix.x.w);
        printf("Rotation.Y   <x:%+1.3f y:%+1.3f z:%+1.3f w:%+1.3f>\n",state.rotationMatrix.y.x, state.rotationMatrix.y.y, state.rotationMatrix.y.z, state.rotationMatrix.y.w);
        printf("Rotation.Z   <x:%+1.3f y:%+1.3f z:%+1.3f w:%+1.3f>\n",state.rotationMatrix.z.x, state.rotationMatrix.z.y, state.rotationMatrix.z.z, state.rotationMatrix.z.w);
        printf("Rotation.W   <x:%+1.3f y:%+1.3f z:%+1.3f w:%+1.3f>\n",state.rotationMatrix.w.x, state.rotationMatrix.w.y, state.rotationMatrix.w.z, state.rotationMatrix.w.w);
        printf("NedMatrix.X  <x:%+1.3f y:%+1.3f z:%+1.3f w:%+1.3f>\n",state.nedMatrix.x.x, state.nedMatrix.x.y, state.nedMatrix.x.z, state.nedMatrix.x.w);
        printf("NedMatrix.Y  <x:%+1.3f y:%+1.3f z:%+1.3f w:%+1.3f>\n",state.nedMatrix.y.x, state.nedMatrix.y.y, state.nedMatrix.y.z, state.nedMatrix.y.w);
        printf("NedMatrix.Z  <x:%+1.3f y:%+1.3f z:%+1.3f w:%+1.3f>\n",state.nedMatrix.z.x, state.nedMatrix.z.y, state.nedMatrix.z.z, state.nedMatrix.z.w);
        printf("NedMatrix.W  <x:%+1.3f y:%+1.3f z:%+1.3f w:%+1.3f>\n",state.nedMatrix.w.x, state.nedMatrix.w.y, state.nedMatrix.w.z, state.nedMatrix.w.w);
       
        printf("\n");
       
        SceMotionSensorState sensor;
        sceMotionGetSensorState(&sensor, 1);
        printf("SensorCounter:%i                \n",sensor.counter);
        printf("accelerometer<x:%+1.3f y:%+1.3f z:%+1.3f>   \n",sensor.accelerometer.x, sensor.accelerometer.y, sensor.accelerometer.z);

        SceFVector3 basicOrientation;
        sceMotionGetBasicOrientation(&basicOrientation);
        printf("basicOrient. <x:%+1.3f y:%+1.3f z:%+1.3f>   \n",basicOrientation.x, basicOrientation.y, basicOrientation.z);
       
        /*        sceMotionRotateYaw(float radians);*/
    }


    sceKernelExitProcess(0);  // close the vita proccess runing, usually close the app
    return 0;
}


-----------

Some screenshots from the sample running on the vita:

Sampling Off

Sampling On



And that's all for this sample.

No comments:

Post a Comment