Simple Compass
응답
15. 7. 7 오후 12:56
I was toying with the XDK Toolbox Examples and I wanted to show visually magnetic north on my XDK.

I simply used the PTB_printToolboxData Example, configured it for using the Compass:
change File PTB_printToolboxData_cc.c, Line 40 to
static PTB_printToolboxData_t sensorToPrint_m = PTB_PRINT_COMPASS; /**< variable which indicates which virtual sensor data will be printed */

Then I looked at the relevant Compass File in the PCD_printCompassData Folder:
PCD_printCompassData_cc.c
First I added the support for LED:
#include "led.h"
#include "xdk_board.h"


Then I defined 3 local variables, each for one of the LED lights I wanted to use
static LED_handle_tp HMI_redLedHandle = (LED_handle_tp) NULL; /**< variable to store red led handle */
static LED_handle_tp HMI_yellowLedHandle = (LED_handle_tp) NULL; /**< variable to store yellow led handle */
static LED_handle_tp HMI_orangeLedHandle = (LED_handle_tp) NULL; /**< variable to store orange led handle */

NB: for the NULL constant to work you also need stdio.h, maybe there is a simpler solution, I don't know

Then I used the existing function PCD_init (around line 99) to add the code to initialize the LEDs, which looks like so:
    HMI_redLedHandle = LED_create(gpioRedLed_Handle, GPIO_STATE_OFF);
    HMI_orangeLedHandle = LED_create(gpioOrangeLed_Handle, GPIO_STATE_OFF);
    HMI_yellowLedHandle = LED_create(gpioYellowLed_Handle, GPIO_STATE_OFF);

This will initialize the LEDs and connect them to our predefined variables.
Now I want to use the middle (=orange) LED on when facing north, the left (=red) LED on when pointing west ( which would be 180-360°) and the right (=yellow) when pointing east (0-180°).

I searched through the code, to find the place where the sensor value is put out to console, which is in function PCD_printCompassData (around line 47):
    if (orientationReturnStatus == SENSOR_SUCCESS)
    {
        /* Print compass sensor information */
        printf("Compass Data : %3.2f\n\r",compassData.heading);

after these lines you can add your logic depending on the sensor value
            if(compassData.heading > 359.5 || compassData.heading < 0.5)
            {
                LED_setState(HMI_redLedHandle, LED_SET_OFF);
                LED_setState(HMI_orangeLedHandle, LED_SET_ON);
                LED_setState(HMI_yellowLedHandle, LED_SET_OFF);
            }
            else if(compassData.heading >= 180.0)
            {
                LED_setState(HMI_redLedHandle, LED_SET_ON);
                LED_setState(HMI_orangeLedHandle, LED_SET_OFF);
                LED_setState(HMI_yellowLedHandle, LED_SET_OFF);
            }
            else if (compassData.heading >= 0.5 && compassData.heading < 180.0)
            {
                LED_setState(HMI_redLedHandle, LED_SET_OFF);
                LED_setState(HMI_orangeLedHandle, LED_SET_OFF);
                LED_setState(HMI_yellowLedHandle, LED_SET_ON);
            }
Thats it! Now you can find out where on your SDK is "up".
Just flash it, and try to find the yellow LED light up.

NB: I just dirty-hacked this, please dont flame me, but give me advice how I can clean up this example more... thanks!

NB2: If you want to toy around more, there is also compass.pitch and compass.roll to use.
0 (0 투표)
Simple Compass
응답
15. 7. 17 오후 8:20 as a reply to Kornelius Nägele.
Thanks for sharing Kornelius!

Has anyone else tried implementing this?

P.S. We are wanting to implement a insert code function to make sharing code easier in the future. If anyone has any other suggestions to improve the communty please share this in the "improvement suggestions" tab.

Best wishes,
William
0 (0 투표)
Simple Compass
응답
15. 7. 22 오전 7:01 as a reply to William Stevenson.
Yeah it could be easily reproduced here - thanks Kornelius!
More such tutorials would be really appreciated =)
0 (0 투표)
Simple Compass
응답
15. 8. 21 오후 3:57 as a reply to T. Pirk.
Hi,
just started playing with my XDK and managed to reproduce - really nice!
Thanks a lot.

Best
Markus
0 (0 투표)
Simple Compass
응답
15. 12. 7 오후 4:37 as a reply to Markus Weinberger.
Hi,
I've a problem to repoduce it....I get problems in PCD_printCompassData_cc.c at this line:

/* API documentation is in the configuration header */
static void PCD_printCompassData(void *pvParameters)
{
    sensor_errorType_e orientationReturnStatus;

The failure code is following:
14:34:59 **** Incremental Build of configuration Default for project Kompass ****
mingw32-make debug
mingw32-make: *** No rule to make target 'debug'.  Stop.

Thanks for help
Christian
0 (0 투표)
Simple Compass
응답
15. 12. 8 오전 11:43 as a reply to Christian Fiedler.
Hi Christian,

there seems to be an error in your projects configuration. (The error you posted is not about a specific line. If a line has a red underline in the editor, it doesn't necessarily mean that it's a compiler error.)

How did you create the project? Did you open the PTB_printToolboxData example from the welcome screen in your XDK-Workbench? If not, you should try again this way as it creates a correctly configured project that you can start with.

To fix the existing project, please open the projects properties, select the option "C/C++ Build" on the left and make sure that the "Build directory" is set to the "make" folder of your project and that that folder contains a Makefile.

Please let me know if these steps were helpful.

Kind regards,
​Manuel
0 (0 투표)
RE: Simple Compass Updated
응답
17. 11. 30 오후 12:31 as a reply to Manuel Cerny.

Hi There,

I updated the project to the present environment. It is in the CompassData.c in the SendVirtualSensorDataOverUSb project.

In the header section:

/* Support header files for the LED. */
#include "BSP_BoardType.h"
#include "BCDS_BSP_LED.h


I introduced a new variable

Orientation_EulerData_T compassValue;

The new program part are in the processCompassData function around the prinf(). The code is below:

    if (RETCODE_OK == orientationReturnStatus)
    {
        /* Print compass sensor information */
        /* Heading means rotation around the z axis. */
        if (compassValue.heading != compassData.heading) {
            compassValue.heading = compassData.heading;
        printf("Compass Data : %3.2f Compass Value : %3.2f " , compassData.heading, compassValue.heading);
        if (compassData.heading >359.5 || compassData.heading <0.5){
             Retcode_T retVal = RETCODE_OK;
                 /* Red LED switch OFF. */
                 retVal = BSP_LED_Switch((uint32_t) BSP_XDK_LED_R, (uint32_t) BSP_LED_COMMAND_OFF);
                        if (RETCODE_OK == retVal)
                        {
                            /* Orange LED switch ON. */
                            retVal = BSP_LED_Switch((uint32_t) BSP_XDK_LED_O, (uint32_t) BSP_LED_COMMAND_ON);
                        }
                        if (RETCODE_OK == retVal)
                        {
                            /* Yellow LED switch OFF. */
                            retVal = BSP_LED_Switch((uint32_t) BSP_XDK_LED_Y, (uint32_t) BSP_LED_COMMAND_OFF);
                        }
                        if (RETCODE_OK == retVal)
                        {
                            printf("  It pointed to NORTH \n\r");
                        }
        } else if (compassData.heading >= 180.0){
            Retcode_T retVal = RETCODE_OK;
                             /* Red LED switch ON. */
                             retVal = BSP_LED_Switch((uint32_t) BSP_XDK_LED_R, (uint32_t) BSP_LED_COMMAND_ON);
                                    if (RETCODE_OK == retVal)
                                    {
                                        /* Orange LED switch OFF. */
                                        retVal = BSP_LED_Switch((uint32_t) BSP_XDK_LED_O, (uint32_t) BSP_LED_COMMAND_OFF);
                                    }
                                    if (RETCODE_OK == retVal)
                                    {
                                        /* Yellow LED switch OFF. */
                                        retVal = BSP_LED_Switch((uint32_t) BSP_XDK_LED_Y, (uint32_t) BSP_LED_COMMAND_OFF);
                                    }
                                    if (RETCODE_OK == retVal)
                                    {
                                        printf("  It pointed to WEST \n\r");
                                    }
        } else if (compassData.heading >= 0.5 || compassData.heading <180.0){
            Retcode_T retVal = RETCODE_OK;
                             /* Red LED switch OFF. */
                             retVal = BSP_LED_Switch((uint32_t) BSP_XDK_LED_R, (uint32_t) BSP_LED_COMMAND_OFF);
                                    if (RETCODE_OK == retVal)
                                    {
                                        /* Orange LED switch OFF. */
                                        retVal = BSP_LED_Switch((uint32_t) BSP_XDK_LED_O, (uint32_t) BSP_LED_COMMAND_OFF);
                                    }
                                    if (RETCODE_OK == retVal)
                                    {
                                        /* Yellow LED switch ON. */
                                        retVal = BSP_LED_Switch((uint32_t) BSP_XDK_LED_Y, (uint32_t) BSP_LED_COMMAND_ON);
                                    }
                                    if (RETCODE_OK == retVal)
                                    {
                                        printf("  It pointed to EAST \n\r");
                                    }
        }
    }
    }


The code stops the repetition if the values are the same (compassValue.heading and compassData.heading) to avoid the long printout. I have some timing bug, because it does not stop immediately. In the console there a print out about the directionas well.

Kind regards,

Béla

0 (0 투표)