1 2 Previous Next 18 Replies Latest reply on Nov 7, 2016 12:43 PM by Intel Corporation

    Autostart on Edison

    Pawek

      Hello,

      I have to make my agent to start automaticlly.

      Do you know when Edison gets IP Adress?

      I have created .service file, because I found on the net hat rc.local doesnt exist on Edison:

       

      [Unit]

      Description= Agent  Service

      After=xdk-daemon.service

      Requires=xdk-daemon.service network.service network-online.target

       

      [Service]

      ExecStart=/home/root/agent/app

       

      [Install]

      WantedBy=multi-user.target

       

      But after reboot Edison it reports this garbage:

      1.PNG

      Here prof that my app is running:

      2.PNG

      When I run it manualy after login works fine:

      3.PNG

      I have to have my app in autostart.

       

       

      I have tried to modify .service file adding the following line:

       

      [Service]

      ExecStart=/home/root/agent/app

      Type=idle

       

      But it deosnt work either.

      Thanks for support.

       

      Paweł

        • 1. Re: Autostart on Edison
          Intel Corporation
          This message was posted on behalf of Intel Corporation

          Hello Pawek,

          I will try to help you. Could you please provide us more details about your application? Is your application based on a Python script? I ask this because I have seen that when starting a Python through a system service if you don't specify the "WorkingDirectory" it will not start. 

          Another thing you could try is to create a shell script that waits until you have an IP address and when you do runs the original script.

          I will be waiting for your response.
          -Peter.

          • 2. Re: Autostart on Edison
            Intel Corporation
            This message was posted on behalf of Intel Corporation

            Hello Pawek,

            Do you have any updates about this?

            -Peter.

            • 3. Re: Autostart on Edison
              Pawek

              Yes. I insert a artificial 10 sec delay and it works fine. But other problem occures. I get that errors:

               

              I get that error on both of my Edisons:

               

              root@Intel-Edison-SanDiego-01:~# [ 5759.863343] INFO: task Proximetry:428 blocked for more than 120 seconds.

              [ 5759.863432] "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message.

              [ 5879.846826] INFO: task Proximetry:428 blocked for more than 120 seconds.

              [ 5879.846914] "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message.

              [ 5999.830327] INFO: task Proximetry:428 blocked for more than 120 seconds.

              [ 5999.830416] "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message.

              [ 6119.813835] INFO: task Proximetry:428 blocked for more than 120 seconds.

              [ 6119.813925] "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message.

              [ 6239.797330] INFO: task Proximetry:428 blocked for more than 120 seconds.

              [ 6239.797418] "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message.

               

               

              root@Intel-Edison-Katowice-01:~# [ 5759.863343] INFO: task Proximetry:428 blocked for more than 120 seconds.

                    "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message.

                    INFO: task Proximetry:428 blocked for more than 120 seconds.

                    "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message.

                    INFO: task Proximetry:428 blocked for more than 120 seconds.

                   "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message.

                    INFO: task Proximetry:428 blocked for more than 120 seconds.

                    "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message.

                    INFO: task Proximetry:428 blocked for more than 120 seconds.

                    "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message.

               

              How to fix it?

               

              I would appriciate any help,

              Paweł

              • 4. Re: Autostart on Edison
                Intel Corporation
                This message was posted on behalf of Intel Corporation

                Hi Pawek,

                This error is known as kernel panic, this issue is usually caused by a hardware failure or a software bug in the operating system. Since this error message started to appear with the service we can assume that it is related to it. Nevertheless, I'm not sure if the application is causing it or if the service is the cause. Anyhow, it would be helpful to know more about the application (if possible), to determine if it could be affecting in this behavior.

                You mentioned that you have run the script manually after boot, do you see this error message? Or, is everything normal when running the application manually? 

                I found an article online with a similar issue: https://www.blackmoreops.com/2014/09/22/linux-kernel-panic-issue-fix-hung_task_timeout_secs-blocked-120-seconds-problem/. The writer used sysctl to solve the issue he was facing, the method he used might not be the solution for the behavior on your Edison (as sysctl on Edison works a little bit different) but it could be lead you to the right direction.

                We'll be waiting for your response.
                -Peter.

                • 5. Re: Autostart on Edison
                  Pawek

                  Hi Peter,

                   

                  I just confirm that this error occures when my app is started by service and when my app is started manually. So it have to be something else.

                   

                  I found this solutnion earlier, but it doenst help.

                   

                  My application send information about network (ip, mac, default gw etc [This code was tested many times on other linux platforms]), sensors read (temp, vibration, analog color, distance <sharp>, sound, light, button and led state) to our cloud where statistics, alerts and conf parameters are shown. This Edison also regarding to that sensors reads stops motor or execute another actions like changing led state or turn on buzzer.

                   

                  Thanks,

                  Paweł

                   

                  Ps.

                  When I run it way that it doesnt take over console (from service or by ./App& ) I am able to re-run my binary. Investigation shows that it hangs on recivfrom finction used as below:

                  int size = recvfrom(prox_sock, buffer, sizeof(buffer), MSG_DONTWAIT, NULL,  NULL);

                  • 6. Re: Autostart on Edison
                    Intel Corporation
                    This message was posted on behalf of Intel Corporation

                    I understand, let me see what I can find out about this. If I'm able to find something useful I'll post it here.

                    -Peter.

                    • 7. Re: Autostart on Edison
                      Pawek

                      Perer,

                       

                      I'm looking forward to hear from you soon.

                       

                      Thanks,

                      Paweł

                      • 8. Re: Autostart on Edison
                        Intel Corporation
                        This message was posted on behalf of Intel Corporation

                        Hello Paweł,

                        I searched more information about this specific issue and I found the following:

                        The error message is related to the fact that a task/process may be using all of the system resources causing the system (Edison in this case) to hang. Most likely the kernel panic is being caused by the application. When the running application waits for some event to occur it goes into a blocked state, then as it waits (blocks) for 120 seconds or more the kernel panic message is triggered.

                        It seems that the cause of this messages is an issue on the application and you might have to debug it.

                        We would like to know if you can share the application with us to see if we notice anything that might be triggering this behavior. Also, you could try to set /proc/sys/kernel/hung_task_timeout_secs to 0 (to disable the messages) or increase /proc/sys/kernel/hung_task_timeout_secs to more than 120 seconds and see if that makes any difference.

                        Let me know.
                        -Peter.

                        • 9. Re: Autostart on Edison
                          Pawek

                          Hi,

                          this is top log after about 4h working of my app, it seems ok:

                           

                          Mem: 219092K used, 763976K free, 0K shrd, 33160K buff, 60244K cached
                          CPU:  3% usr  62% sys  0% nic  33% idle  0% io  0% irq  0% sirq
                          Load average: 1.95 2.04 2.04 2/137 441
                            PID  PPID USER    STAT  VSZ %VSZ %CPU COMMAND
                            437    2 root    RW      0  0%  30% [kworker/u4:2]
                            423  401 root    D    2776  0%  25% ./Proximetry    <-------------------------------------------
                            438    2 root    SW      0  0%  11% [kworker/u4:3]
                            342    1 root    S    19896  2%  0% /usr/bin/redis-server /etc/redis/redis
                            441  401 root    R    2892  0%  0% top
                            126    2 root    SW      0  0%  0% [dhd_watchdog_th]
                            64    2 root    SW      0  0%  0% [kworker/0:2]
                            350  302 root    S    111m  12%  0% /usr/bin/node /opt/xdk-daemon/current/
                            249    1 pulse    S <  109m  11%  0% /usr/bin/pulseaudio --system --resampl
                            318  293 root    S    80208  8%  0% node /usr/lib/edison_config_tools/edis
                            302  297 root    S    76996  8%  0% /usr/bin/node /opt/xdk-daemon/main.js
                            173    1 systemd- S    12092  1%  0% /lib/systemd/systemd-timesyncd
                            138    1 root    S    9552  1%  0% /lib/systemd/systemd-udevd
                            115    1 root    S    8680  1%  0% /lib/systemd/systemd-journald
                            192    1 root    S    6348  1%  0% /usr/sbin/wpa_supplicant -u -c/etc/wpa
                            316  315 root    S    5264  1%  0% (sd-pam)
                            196    1 root    S    5128  1%  0% /usr/sbin/ofonod -n
                            250    1 root    S    4684  0%  0% /usr/lib/bluez5/bluetooth/bluetoothd -
                            273    1 root    S    4536  0%  0% /usr/sbin/mosquitto -c /etc/mosquitto/
                              1    0 root    S    4480  0%  0% {systemd} /sbin/init
                          

                          Or am I wrong?

                           

                          Only one that this applicaion make different way then my other linux devices is sensors reading. Rest is copy-pasted between other platforma (read of ip and so on).

                           

                          Maybe there is issue?

                           

                          int Steps;
                          bool Direction;
                          bool hold_request = false;
                          uint32_t now, then;
                          extern bool should_run;
                          
                          
                          void prepare_gpios_and_aio(void)
                          {
                            /* Initialize GPIOs*/
                            buzzer = mraa_gpio_init(GPIO_BUZZER_PIN);
                            button = mraa_gpio_init(GPIO_BUTTON_PIN);
                            led_red = mraa_gpio_init(GPIO_RED_LED_PIN);
                          
                          
                            /* Initialize AIOs*/
                            temp = mraa_aio_init(AIO_TEMP_PIN);
                            light = mraa_aio_init(AIO_LIGHT_PIN);
                            sound = mraa_aio_init(AIO_SOUND_PIN);
                            distance = mraa_aio_init(AIO_DIST_PIN);
                            red = mraa_aio_init(AIO_RED);
                            vibro = mraa_aio_init(AIO_VIBRO);
                          
                          
                            stepper_gpio1 = mraa_gpio_init(GPIO_STEPPER1_PIN);
                            stepper_gpio2 = mraa_gpio_init(GPIO_STEPPER2_PIN);
                            stepper_gpio3 = mraa_gpio_init(GPIO_STEPPER3_PIN);
                            stepper_gpio4 = mraa_gpio_init(GPIO_STEPPER4_PIN);
                          
                          
                            /*Because we need more 5V outputs*/
                            additional_power_source1 = mraa_gpio_init(GPIO_PS1);
                            additional_power_source2 = mraa_gpio_init(GPIO_PS2);
                          
                          
                            /* Check if all went good*/
                            if (!check_init())
                            {
                            PROX_LOG(
                            "Unable to initialize all sensors. Please check your connections.\n");
                            exit(EXIT_FAILURE);
                            }
                          
                          
                            /* Set mode (all to strong) and direction for GPIOs*/
                            mraa_gpio_mode(button, MRAA_GPIO_STRONG);
                            mraa_gpio_dir(button, MRAA_GPIO_IN);
                          
                          
                            mraa_gpio_mode(buzzer, MRAA_GPIO_STRONG);
                            mraa_gpio_dir(buzzer, MRAA_GPIO_OUT);
                          
                          
                            mraa_gpio_mode(led_red, MRAA_GPIO_STRONG);
                            mraa_gpio_dir(led_red, MRAA_GPIO_OUT);
                          
                          
                            mraa_gpio_mode(stepper_gpio1, MRAA_GPIO_STRONG);
                            mraa_gpio_dir(stepper_gpio1, MRAA_GPIO_OUT);
                            mraa_gpio_mode(stepper_gpio2, MRAA_GPIO_STRONG);
                            mraa_gpio_dir(stepper_gpio2, MRAA_GPIO_OUT);
                            mraa_gpio_mode(stepper_gpio3, MRAA_GPIO_STRONG);
                            mraa_gpio_dir(stepper_gpio3, MRAA_GPIO_OUT);
                            mraa_gpio_mode(stepper_gpio4, MRAA_GPIO_STRONG);
                            mraa_gpio_dir(stepper_gpio4, MRAA_GPIO_OUT);
                          
                          
                            mraa_gpio_mode(additional_power_source1, MRAA_GPIO_STRONG);
                            mraa_gpio_dir(additional_power_source1, MRAA_GPIO_OUT);
                            mraa_gpio_write(additional_power_source1,1);
                          
                          
                            mraa_gpio_mode(additional_power_source2, MRAA_GPIO_STRONG);
                            mraa_gpio_dir(additional_power_source2, MRAA_GPIO_OUT);
                            mraa_gpio_write(additional_power_source2,1);
                          }
                          
                          
                          int read_from_gpio(mraa_gpio_context sensor)
                          {
                            return mraa_gpio_read(sensor);
                          }
                          
                          
                          int read_from_aio(mraa_aio_context sensor)
                          {
                            return mraa_aio_read(sensor);
                          }
                          
                          
                          int get_temp_in_celcius(void) // [! IMPORTANT] VCC 3.3 -> 5 on board
                          {
                            const int B = 4255; /* B-const value form datasheet for our sensor*/
                          
                          
                            int adc_read = 0;
                            adc_read = mraa_aio_read(temp); /* Raw read from aio*/
                          
                          
                            float resistance = 1023.0 / ((float) adc_read) - 1.0; /* Calculate resistance*/
                            resistance *= 100000.0;
                          
                          
                            float temperature = 1.0 / (log(resistance / 100000.0) / B + 1 / 298.15)
                            - 273.15; /* Magic. Also from datasheet*/
                          
                          
                            /**
                            *  For more info please reference to;
                            *
                            *  http://www.seeedstudio.com/wiki/File:Grove_-_Temperature_sensor_v1.1.pdf
                            *  * * * * * * * * * * * * * * * * * * */
                          
                          
                            return (int) temperature;
                          }
                          
                          
                          int get_distance(void)
                          {
                            // [!IMPORTANT] Works fine only between 10cm to 80cm according to datasheet
                            // https://www.sparkfun.com/datasheets/Components/GP2Y0A21YK.pdf
                            int volts, sum = 0;
                            int i;
                          // int  resolution=(1 << mraa_aio_get_bit(distance));
                            for (i = 0; i < 5; i++)
                            {
                            volts = mraa_aio_read(distance);
                             sum += volts;
                            }
                            volts = sum / 5;
                          
                          
                            float val = 3027.4 / volts;
                            val = pow(val, 1.2134); // to cm
                            if(val < 10)
                            return 10; // otherwise i reports garbage like 1900 cm
                            if(val > 80)
                            return 80;
                            return (int) val;
                          }
                          
                          
                          int get_color(int color)
                          {
                            int val_color;
                            switch (color)
                            {
                            case COLOR_RED:
                            val_color = read_from_aio(red);
                            //float val_f = mraa_aio_read_float(red);
                          // printf("As int: %d\n",val_color);
                          // printf("as float: %f\n",val_f);
                          // printf("i: %d    f: %f\n",val_color*5/1023,val_f*5/1023);
                            break;
                            case COLOR_BLUE:
                            //color =  read_from_aio(blue); //
                            val_color = 0; //
                            break; // Out of AIOS for other colors
                            case COLOR_GREEN: //
                            // val_color =  read_from_aio(green);/
                            val_color = 0;
                            break;
                            default:
                            printf("Unsupported color: %s \n", __FUNCTION__);
                            val_color = -1;
                            break;
                            }
                            if( val_color > 255)
                            val_color = 255; // TODO: It isnt best solutnio, but it works for now
                            return val_color;
                          }
                          
                          
                          bool check_init()
                          {
                            bool are_initialized = true;
                          
                          
                            /* There must be better way!*/
                            if (button == NULL)
                            are_initialized = false;
                          
                          
                            if (buzzer == NULL)
                            are_initialized = false;
                          
                          
                            if (led_red == NULL)
                            are_initialized = false;
                          
                          
                            if (temp == NULL)
                            are_initialized = false;
                          
                          
                            if (light == NULL)
                            are_initialized = false;
                          
                          
                            if (sound == NULL)
                            are_initialized = false;
                          
                          
                            if (distance == NULL)
                            are_initialized = false;
                          
                          
                            if (red == NULL)
                            are_initialized = false;
                          
                          
                            if (stepper_gpio1 == NULL || stepper_gpio2 == NULL || stepper_gpio3 == NULL
                            || stepper_gpio4 == NULL)
                            are_initialized = false;
                          
                          
                            return are_initialized;
                          }
                          
                          
                          void stepper(int xw)
                          {
                            int x;
                            for (x = 0; x < xw; x = x + 1)
                            {
                          
                          
                            switch (Steps)
                            {
                            case 0:
                            mraa_gpio_write(stepper_gpio1, LOW);
                            mraa_gpio_write(stepper_gpio2, LOW);
                            mraa_gpio_write(stepper_gpio3, LOW);
                            mraa_gpio_write(stepper_gpio4, HIGH);
                            break;
                            case 1:
                            mraa_gpio_write(stepper_gpio1, LOW);
                            mraa_gpio_write(stepper_gpio2, LOW);
                            mraa_gpio_write(stepper_gpio3, HIGH);
                            mraa_gpio_write(stepper_gpio4, HIGH);
                            break;
                            case 2:
                            mraa_gpio_write(stepper_gpio1, LOW);
                            mraa_gpio_write(stepper_gpio2, LOW);
                            mraa_gpio_write(stepper_gpio3, HIGH);
                            mraa_gpio_write(stepper_gpio4, LOW);
                            break;
                            case 3:
                            mraa_gpio_write(stepper_gpio1, LOW);
                            mraa_gpio_write(stepper_gpio2, HIGH);
                            mraa_gpio_write(stepper_gpio3, HIGH);
                            mraa_gpio_write(stepper_gpio4, LOW);
                            break;
                            case 4:
                            mraa_gpio_write(stepper_gpio1, LOW);
                            mraa_gpio_write(stepper_gpio2, HIGH);
                            mraa_gpio_write(stepper_gpio3, LOW);
                            mraa_gpio_write(stepper_gpio4, LOW);
                            break;
                            case 5:
                            mraa_gpio_write(stepper_gpio1, HIGH);
                            mraa_gpio_write(stepper_gpio2, HIGH);
                            mraa_gpio_write(stepper_gpio3, LOW);
                            mraa_gpio_write(stepper_gpio4, LOW);
                            break;
                            case 6:
                            mraa_gpio_write(stepper_gpio1, HIGH);
                            mraa_gpio_write(stepper_gpio2, LOW);
                            mraa_gpio_write(stepper_gpio3, LOW);
                            mraa_gpio_write(stepper_gpio4, LOW);
                            break;
                            case 7:
                            mraa_gpio_write(stepper_gpio1, HIGH);
                            mraa_gpio_write(stepper_gpio2, LOW);
                            mraa_gpio_write(stepper_gpio3, LOW);
                            mraa_gpio_write(stepper_gpio4, HIGH);
                            break;
                            default:
                            mraa_gpio_write(stepper_gpio1, LOW);
                            mraa_gpio_write(stepper_gpio2, LOW);
                            mraa_gpio_write(stepper_gpio3, LOW);
                            mraa_gpio_write(stepper_gpio4, LOW);
                            break;
                            }
                          
                          
                            SetDirection();
                            }
                          }
                          
                          
                          void SetDirection()
                          {
                            if (Direction == 1)
                            Steps = Steps + 1;
                            if (Direction == 0)
                            Steps = Steps - 1;
                            if (Steps > 7)
                            Steps = 0;
                            if (Steps < 0)
                            Steps = 7;
                          }
                          
                          
                          static int guard = 0;
                          void hold_motor(int how_long)
                          {
                            //printf("hold poczatek\n");
                            if (hold_request)
                            {
                            //printf("hold spelniony\n");
                            if (!guard)
                            {
                            then = prox_time_ms();
                            now = prox_time_ms();
                            guard = 1;
                            }
                          
                          
                            how_long *= 1000; // s2ms
                          
                          
                            if (now - then < how_long)
                            {
                            should_run = false;
                            now = prox_time_ms();
                            mraa_gpio_write(led_red, 1);
                            }
                            else
                            {
                            should_run = true;
                            guard = 0;
                            hold_request = false;
                            mraa_gpio_write(led_red, 0);
                            }
                          
                          
                            }
                          }
                          

                           

                           

                          Its worth noticing that settings are saved to file when for example reporting interval to cloud changes, but always to the same file.

                          This kernel panic shows up after about 15h or somethig.

                           

                           

                          Thanks,

                          Paweł

                          • 10. Re: Autostart on Edison
                            Intel Corporation
                            This message was posted on behalf of Intel Corporation

                            I understand, please let me see if I can find something out.

                            -Peter.

                            • 11. Re: Autostart on Edison
                              Intel Corporation
                              This message was posted on behalf of Intel Corporation

                              I've been trying to compile your code and I noticed that there are some lines missing (the includes, defines, etc.). I'm able to guess most of them but some others like COLOR_BLUE, COLOR_GREEN and some others I just don't know what they are supposed to be. Could you please share these lines as well?

                              I'll be waiting for your reply.
                              -Peter.

                              • 12. Re: Autostart on Edison
                                Pawek

                                Hi,

                                //#include "sensor.h"

                                #include <math.h>

                                #include <stdbool.h>

                                //#include "prox_config.h"

                                //#include "prox_core.h"

                                #include <unistd.h>

                                #include <stdlib.h>

                                *GPIOs*/

                                #define GPIO_BUTTON_PIN 2

                                #define GPIO_RED_LED_PIN 4

                                #define GPIO_BUZZER_PIN 5

                                #define GPIO_PS1 8

                                #define GPIO_PS2 9

                                 

                                #define GPIO_STEPPER1_PIN 10

                                #define GPIO_STEPPER2_PIN 11

                                #define GPIO_STEPPER3_PIN 12

                                #define GPIO_STEPPER4_PIN 13

                                 

                                /*AIOs*/

                                #define AIO_TEMP_PIN 0

                                #define AIO_SOUND_PIN 1

                                #define AIO_LIGHT_PIN 2

                                #define AIO_DIST_PIN 5

                                #define AIO_RED 4

                                #define AIO_VIBRO 3

                                 

                                #define ADC_REF 5 /* 5V */

                                 

                                #define LOW 0

                                #define HIGH 1

                                 

                                #define COLOR_RED 1

                                #define COLOR_GREEN 2

                                #define COLOR_BLUE 3

                                 

                                 

                                 

                                Thanks,

                                Paweł

                                • 13. Re: Autostart on Edison
                                  Pawek

                                  I found that after that Kernel panic top shows that

                                   

                                  Mem: 221360K used, 761708K free, 0K shrd, 34960K buff, 60252K cached

                                  CPU:   0% usr  99% sys   0% nic   0% idle   0% io   0% irq   0% sirq

                                   

                                  usr is used in 99%.

                                   

                                  Maybe this is problem? What may cause that?

                                   

                                  Best,

                                  Paweł

                                  • 14. Re: Autostart on Edison
                                    Intel Corporation
                                    This message was posted on behalf of Intel Corporation

                                    Hi Pawek,

                                     

                                    I'm trying to run your application, from your previous posts I put together the following code:

                                     

                                     

                                    //#include "sensor.h"
                                    #include <math.h>
                                    #include <stdbool.h>
                                    //#include "prox_config.h"
                                    //#include "prox_core.h"
                                    #include <unistd.h>
                                    #include <stdlib.h>
                                    //I added these lines, they were missing
                                    #include "mraa/gpio.h"
                                    #include "mraa/aio.h"
                                    ///////////////////////////////////////
                                    /*GPIOs*/
                                    #define GPIO_BUTTON_PIN 2
                                    #define GPIO_RED_LED_PIN 4
                                    #define GPIO_BUZZER_PIN 5
                                    #define GPIO_PS1 8
                                    #define GPIO_PS2 9
                                    
                                    #define GPIO_STEPPER1_PIN 10
                                    #define GPIO_STEPPER2_PIN 11
                                    #define GPIO_STEPPER3_PIN 12
                                    #define GPIO_STEPPER4_PIN 13
                                    
                                    /*AIOs*/
                                    #define AIO_TEMP_PIN 0
                                    #define AIO_SOUND_PIN 1
                                    #define AIO_LIGHT_PIN 2
                                    #define AIO_DIST_PIN 5
                                    #define AIO_RED 4
                                    #define AIO_VIBRO 3
                                    
                                    #define ADC_REF 5 /* 5V */
                                    
                                    #define LOW 0
                                    #define HIGH 1
                                    
                                    #define COLOR_RED 1
                                    #define COLOR_GREEN 2
                                    #define COLOR_BLUE 3
                                    
                                    
                                    //I added these lines, they were missing
                                    mraa_aio_context temp, light, sound, distance, red, vibro;
                                    mraa_gpio_context buzzer, button, led_red, stepper_gpio1, stepper_gpio2, stepper_gpio3, stepper_gpio4, additional_power_source1, additional_power_source2;
                                    ///////////////////////////////////////
                                    
                                    
                                    int Steps;  
                                    bool Direction;  
                                    bool hold_request = false;  
                                    uint32_t now, then;  
                                    extern bool should_run;  
                                      
                                    void prepare_gpios_and_aio(void)  
                                    {  
                                      /* Initialize GPIOs*/  
                                      buzzer = mraa_gpio_init(GPIO_BUZZER_PIN);  
                                      button = mraa_gpio_init(GPIO_BUTTON_PIN);  
                                      led_red = mraa_gpio_init(GPIO_RED_LED_PIN);  
                                      
                                      
                                      /* Initialize AIOs*/  
                                      temp = mraa_aio_init(AIO_TEMP_PIN);  
                                      light = mraa_aio_init(AIO_LIGHT_PIN);  
                                      sound = mraa_aio_init(AIO_SOUND_PIN);  
                                      distance = mraa_aio_init(AIO_DIST_PIN);  
                                      red = mraa_aio_init(AIO_RED);  
                                      vibro = mraa_aio_init(AIO_VIBRO);  
                                      
                                      
                                      stepper_gpio1 = mraa_gpio_init(GPIO_STEPPER1_PIN);  
                                      stepper_gpio2 = mraa_gpio_init(GPIO_STEPPER2_PIN);  
                                      stepper_gpio3 = mraa_gpio_init(GPIO_STEPPER3_PIN);  
                                      stepper_gpio4 = mraa_gpio_init(GPIO_STEPPER4_PIN);  
                                      
                                      
                                      /*Because we need more 5V outputs*/  
                                      additional_power_source1 = mraa_gpio_init(GPIO_PS1);  
                                      additional_power_source2 = mraa_gpio_init(GPIO_PS2);  
                                      
                                      
                                      /* Check if all went good*/  
                                      if (!check_init())  
                                      {  
                                      PROX_LOG(  
                                      "Unable to initialize all sensors. Please check your connections.\n");  
                                      exit(EXIT_FAILURE);  
                                      }  
                                      
                                      
                                      /* Set mode (all to strong) and direction for GPIOs*/  
                                      mraa_gpio_mode(button, MRAA_GPIO_STRONG);  
                                      mraa_gpio_dir(button, MRAA_GPIO_IN);  
                                      
                                      
                                      mraa_gpio_mode(buzzer, MRAA_GPIO_STRONG);  
                                      mraa_gpio_dir(buzzer, MRAA_GPIO_OUT);  
                                      
                                      
                                      mraa_gpio_mode(led_red, MRAA_GPIO_STRONG);  
                                      mraa_gpio_dir(led_red, MRAA_GPIO_OUT);  
                                      
                                      
                                      mraa_gpio_mode(stepper_gpio1, MRAA_GPIO_STRONG);  
                                      mraa_gpio_dir(stepper_gpio1, MRAA_GPIO_OUT);  
                                      mraa_gpio_mode(stepper_gpio2, MRAA_GPIO_STRONG);  
                                      mraa_gpio_dir(stepper_gpio2, MRAA_GPIO_OUT);  
                                      mraa_gpio_mode(stepper_gpio3, MRAA_GPIO_STRONG);  
                                      mraa_gpio_dir(stepper_gpio3, MRAA_GPIO_OUT);  
                                      mraa_gpio_mode(stepper_gpio4, MRAA_GPIO_STRONG);  
                                      mraa_gpio_dir(stepper_gpio4, MRAA_GPIO_OUT);  
                                      
                                      
                                      mraa_gpio_mode(additional_power_source1, MRAA_GPIO_STRONG);  
                                      mraa_gpio_dir(additional_power_source1, MRAA_GPIO_OUT);  
                                      mraa_gpio_write(additional_power_source1,1);  
                                      
                                      
                                      mraa_gpio_mode(additional_power_source2, MRAA_GPIO_STRONG);  
                                      mraa_gpio_dir(additional_power_source2, MRAA_GPIO_OUT);  
                                      mraa_gpio_write(additional_power_source2,1);  
                                    }  
                                      
                                      
                                    int read_from_gpio(mraa_gpio_context sensor)  
                                    {  
                                      return mraa_gpio_read(sensor);  
                                    }  
                                      
                                      
                                    int read_from_aio(mraa_aio_context sensor)  
                                    {  
                                      return mraa_aio_read(sensor);  
                                    }  
                                      
                                      
                                    int get_temp_in_celcius(void) // [! IMPORTANT] VCC 3.3 -> 5 on board  
                                    {  
                                      const int B = 4255; /* B-const value form datasheet for our sensor*/  
                                      
                                      
                                      int adc_read = 0;  
                                      adc_read = mraa_aio_read(temp); /* Raw read from aio*/  
                                      
                                      
                                      float resistance = 1023.0 / ((float) adc_read) - 1.0; /* Calculate resistance*/  
                                      resistance *= 100000.0;  
                                      
                                      
                                      float temperature = 1.0 / (log(resistance / 100000.0) / B + 1 / 298.15)  
                                      - 273.15; /* Magic. Also from datasheet*/  
                                      
                                      
                                      /** 
                                      *  For more info please reference to; 
                                      * 
                                      *  http://www.seeedstudio.com/wiki/File:Grove_-_Temperature_sensor_v1.1.pdf 
                                      *  * * * * * * * * * * * * * * * * * * */  
                                      
                                      
                                      return (int) temperature;  
                                    }  
                                      
                                      
                                    int get_distance(void)  
                                    {  
                                      // [!IMPORTANT] Works fine only between 10cm to 80cm according to datasheet  
                                      // https://www.sparkfun.com/datasheets/Components/GP2Y0A21YK.pdf  
                                      int volts, sum = 0;  
                                      int i;  
                                    // int  resolution=(1 << mraa_aio_get_bit(distance));  
                                      for (i = 0; i < 5; i++)  
                                      {  
                                      volts = mraa_aio_read(distance);  
                                       sum += volts;  
                                      }  
                                      volts = sum / 5;  
                                      
                                      
                                      float val = 3027.4 / volts;  
                                      val = pow(val, 1.2134); // to cm  
                                      if(val < 10)  
                                      return 10; // otherwise i reports garbage like 1900 cm  
                                      if(val > 80)  
                                      return 80;  
                                      return (int) val;  
                                    }  
                                      
                                      
                                    int get_color(int color)  
                                    {  
                                      int val_color;  
                                      switch (color)  
                                      {  
                                      case COLOR_RED:  
                                      val_color = read_from_aio(red);  
                                      //float val_f = mraa_aio_read_float(red);  
                                    // printf("As int: %d\n",val_color);  
                                    // printf("as float: %f\n",val_f);  
                                    // printf("i: %d    f: %f\n",val_color*5/1023,val_f*5/1023);  
                                      break;  
                                      case COLOR_BLUE:  
                                      //color =  read_from_aio(blue); //  
                                      val_color = 0; //  
                                      break; // Out of AIOS for other colors  
                                      case COLOR_GREEN: //  
                                      // val_color =  read_from_aio(green);/  
                                      val_color = 0;  
                                      break;  
                                      default:  
                                      printf("Unsupported color: %s \n", __FUNCTION__);  
                                      val_color = -1;  
                                      break;  
                                      }  
                                      if( val_color > 255)  
                                      val_color = 255; // TODO: It isnt best solutnio, but it works for now  
                                      return val_color;  
                                    }  
                                      
                                      
                                    bool check_init()  
                                    {  
                                      bool are_initialized = true;  
                                      
                                      
                                      /* There must be better way!*/  
                                      if (button == NULL)  
                                      are_initialized = false;  
                                      
                                      
                                      if (buzzer == NULL)  
                                      are_initialized = false;  
                                      
                                      
                                      if (led_red == NULL)  
                                      are_initialized = false;  
                                      
                                      
                                      if (temp == NULL)  
                                      are_initialized = false;  
                                      
                                      
                                      if (light == NULL)  
                                      are_initialized = false;  
                                      
                                      
                                      if (sound == NULL)  
                                      are_initialized = false;  
                                      
                                      
                                      if (distance == NULL)  
                                      are_initialized = false;  
                                      
                                      
                                      if (red == NULL)  
                                      are_initialized = false;  
                                      
                                      
                                      if (stepper_gpio1 == NULL || stepper_gpio2 == NULL || stepper_gpio3 == NULL  
                                      || stepper_gpio4 == NULL)  
                                      are_initialized = false;  
                                      
                                      
                                      return are_initialized;  
                                    }
                                      
                                      
                                    void stepper(int xw)  
                                    {  
                                      int x;  
                                      for (x = 0; x < xw; x = x + 1)  
                                      {  
                                      
                                      
                                      switch (Steps)  
                                      {  
                                      case 0:  
                                      mraa_gpio_write(stepper_gpio1, LOW);  
                                      mraa_gpio_write(stepper_gpio2, LOW);  
                                      mraa_gpio_write(stepper_gpio3, LOW);  
                                      mraa_gpio_write(stepper_gpio4, HIGH);  
                                      break;  
                                      case 1:  
                                      mraa_gpio_write(stepper_gpio1, LOW);  
                                      mraa_gpio_write(stepper_gpio2, LOW);  
                                      mraa_gpio_write(stepper_gpio3, HIGH);  
                                      mraa_gpio_write(stepper_gpio4, HIGH);  
                                      break;  
                                      case 2:  
                                      mraa_gpio_write(stepper_gpio1, LOW);  
                                      mraa_gpio_write(stepper_gpio2, LOW);  
                                      mraa_gpio_write(stepper_gpio3, HIGH);  
                                      mraa_gpio_write(stepper_gpio4, LOW);  
                                      break;  
                                      case 3:  
                                      mraa_gpio_write(stepper_gpio1, LOW);  
                                      mraa_gpio_write(stepper_gpio2, HIGH);  
                                      mraa_gpio_write(stepper_gpio3, HIGH);  
                                      mraa_gpio_write(stepper_gpio4, LOW);  
                                      break;  
                                      case 4:  
                                      mraa_gpio_write(stepper_gpio1, LOW);  
                                      mraa_gpio_write(stepper_gpio2, HIGH);  
                                      mraa_gpio_write(stepper_gpio3, LOW);  
                                      mraa_gpio_write(stepper_gpio4, LOW);  
                                      break;  
                                      case 5:  
                                      mraa_gpio_write(stepper_gpio1, HIGH);  
                                      mraa_gpio_write(stepper_gpio2, HIGH);  
                                      mraa_gpio_write(stepper_gpio3, LOW);  
                                      mraa_gpio_write(stepper_gpio4, LOW);  
                                      break;  
                                      case 6:  
                                      mraa_gpio_write(stepper_gpio1, HIGH);  
                                      mraa_gpio_write(stepper_gpio2, LOW);  
                                      mraa_gpio_write(stepper_gpio3, LOW);  
                                      mraa_gpio_write(stepper_gpio4, LOW);  
                                      break;  
                                      case 7:  
                                      mraa_gpio_write(stepper_gpio1, HIGH);  
                                      mraa_gpio_write(stepper_gpio2, LOW);  
                                      mraa_gpio_write(stepper_gpio3, LOW);  
                                      mraa_gpio_write(stepper_gpio4, HIGH);  
                                      break;  
                                      default:  
                                      mraa_gpio_write(stepper_gpio1, LOW);  
                                      mraa_gpio_write(stepper_gpio2, LOW);  
                                      mraa_gpio_write(stepper_gpio3, LOW);  
                                      mraa_gpio_write(stepper_gpio4, LOW);  
                                      break;  
                                      }  
                                      
                                      
                                      SetDirection();  
                                      }  
                                    }  
                                      
                                      
                                    void SetDirection()  
                                    {  
                                      if (Direction == 1)  
                                      Steps = Steps + 1;  
                                      if (Direction == 0)  
                                      Steps = Steps - 1;  
                                      if (Steps > 7)  
                                      Steps = 0;  
                                      if (Steps < 0)  
                                      Steps = 7;  
                                    }
                                      
                                      
                                    static int guard = 0;  
                                    void hold_motor(int how_long)  
                                    {  
                                      //printf("hold poczatek\n");  
                                      if (hold_request)  
                                      {  
                                      //printf("hold spelniony\n");  
                                      if (!guard)  
                                      {  
                                      then = prox_time_ms();  
                                      now = prox_time_ms();  
                                      guard = 1;  
                                      }  
                                      
                                      
                                      how_long *= 1000; // s2ms  
                                      
                                      
                                      if (now - then < how_long)  
                                      {  
                                      should_run = false;  
                                      now = prox_time_ms();  
                                      mraa_gpio_write(led_red, 1);  
                                      }  
                                      else  
                                      {  
                                      should_run = true;  
                                      guard = 0;  
                                      hold_request = false;  
                                      mraa_gpio_write(led_red, 0);  
                                      }  
                                      
                                      
                                      }  
                                    }  
                                    

                                     

                                     

                                    But I'm still getting some issues, this is what I get from the compilation:

                                     

                                    a.c:217:6: error: conflicting types for 'check_init'
                                     bool check_init()
                                          ^
                                    a.c:79:8: note: previous implicit declaration of 'check_init' was here
                                       if (!check_init())
                                            ^
                                    a.c:335:6: warning: conflicting types for 'SetDirection'
                                     void SetDirection()
                                          ^
                                    a.c:330:3: note: previous implicit declaration of 'SetDirection' was here
                                       SetDirection();
                                       ^
                                    

                                     

                                    I tried to make some changes to remove this errors but that just brought different errors. So, is this the code you are using? If so, how are you compiling your code? I'm not able to do it because of the errors I posted above.

                                     

                                    Let me know.
                                    -Peter

                                    1 2 Previous Next