Skip to content

Initialization Scripts

Vasco Craveiro Costa edited this page Jul 6, 2015 · 4 revisions

In order to provide a visual report of the system status to the drone's operators, we connected two LEDs to GPIO4 and GPIO5 (pins 16 and 18 of the header), through a buffer transistor. One of this LEDs (the one on GPIO4) starts blinking as soon as the system boots, while the second one only starts blinking when the controller is running. To configure the LEDs outputs and start the first blinking, we use the following C "script", which we put on path /home/pi/scripts/c:

#include <wiringPi.h>

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <signal.h>

#define exit_on_error(s,m) if(s<0){perror(m); exit(1);}
#define statusLedPin    4
#define controllerLed   5


void handlerSIGINT(){
    digitalWrite(statusLedPin,0);
    exit(0);
}

void executeStatus(){
    wiringPiSetup();

    pinMode(statusLedPin, OUTPUT);
    pinMode(controllerLed, OUTPUT);

    digitalWrite(controllerLed,LOW);

    while(1){
        digitalWrite(statusLedPin,0);
        delay(250);

        digitalWrite(statusLedPin,1);
        delay(250);
    }
}

int main(){
    uid_t uid=getuid();
    if (uid==0) {
        signal(SIGINT,handlerSIGINT);
        int pid = fork();

        exit_on_error(pid,"[SystemStatusLeds] Fork failed on system status deamon initialization!\n");

        // PID==0 --> child process.
        if (pid == 0) {
            executeStatus();
        }else {
            printf("[SystemStatusLeds] System status deamon initialized with sucess on PID: %i\n",pid);
        }

        exit(0);
    }else{
        printf("[SystemStatusLeds] Not enough permissions to run this program. Try as sudo!\n");
        exit_on_error(uid, "[SystemStatusLeds] Not enough permissions to run this program. Try as sudo!\n");
    }
}

To compile the above file, use the following command (considering that it is named SystemStatus.c):

gcc SystemStatus.c -o SystemStatus -lwiringPi

Last, but not least, call the script by adding it to /etc/rc.local, after the invocation of the createAdHocNetwork function implemented previously:

/home/pi/scripts/c/SystemStatus
EXIT_CODE=$?
if [ $EXIT_CODE -eq 0 ];then
    printf "[rc.local] Status LED Deamon Started with sucess\n"
else
    printf "[rc.local] Error initializing status LED Deamon\n"
fi

###Reference