Make code compile
authorSam Moore <[email protected]>
Fri, 20 Sep 2013 05:22:52 +0000 (13:22 +0800)
committerSam Moore <[email protected]>
Fri, 20 Sep 2013 05:22:52 +0000 (13:22 +0800)
Still needs work; GPIO1_28 (60) doesn't appear to be triggered by the actuator.

server/Makefile
server/actuator.c
server/gpio.c
server/gpio.h
server/sensor.c

index 3838876..fd4d7ec 100644 (file)
@@ -2,7 +2,7 @@
 CXX = gcc
 FLAGS = -std=c99 -Wall -pedantic -g -I/usr/include/opencv -I/usr/include/opencv2/highgui -L/usr/lib
 LIB = -lfcgi -lssl -lcrypto -lpthread -lm -lopencv_highgui -lopencv_core -lopencv_ml -lopencv_imgproc
-OBJ = log.o control.o data.o fastcgi.o main.o sensor.o actuator.o image.o
+OBJ = log.o control.o data.o fastcgi.o main.o sensor.o actuator.o image.o gpio.o
 RM = rm -f
 
 BIN = server
index 1abaa97..4df06bc 100644 (file)
@@ -7,7 +7,7 @@
 #include "options.h"
 // Files containing GPIO and PWM definitions
 #include "gpio.h"
-#include "pwm.h"
+
 
 /** Array of Actuators (global to this file) initialised by Actuator_Init **/
 static Actuator g_actuators[NUMACTUATORS];
@@ -196,16 +196,16 @@ void Actuator_SetValue(Actuator * a, double value)
                        {
                                // Quick actuator function for testing pins
                                // GPIOPin can be passed as argument, but is just defined here for testing purposes
-                               int GPIOPin = 13;
                                // Modify this to only export on first run, only unexport on shutdown
-                               pinExport(setValue, GPIOString);
-                               pinDirection(GPIODirection, setValue);
-                               pinSet(value, GPIOValue, setValue);
-                               pinUnexport(setValue, GPIOString);
+                               pinExport(60);
+                               pinDirection(60, 1);
+                               pinSet(value, 60);
+                               pinUnexport(60);
                        }
                        break;
                case ACTUATOR_TEST2:
                        // PWM analogue actuator (currently generates one PWM signal with first PWM module)
+                       /*
                        {
                                if (pwminit == 0) {                                             // If inactive, start the pwm module
                                        pwm_init();
@@ -219,6 +219,7 @@ void Actuator_SetValue(Actuator * a, double value)
                                        pwm_set_duty((int)duty);                        // Set duty percentage for actuator (0-100%)
                                }
                        }
+                       */
                        break;
        }
 
index 65f1a51..6370df6 100644 (file)
@@ -1,11 +1,14 @@
 #include "gpio.h"
 
 void pinExport(int GPIOPin) {
-       FILE *myOutputHandle = NULL;
+       
        char GPIOString[4];
        char setValue[4];
        sprintf(GPIOString, "%d", GPIOPin);
-       if ((myOutputHandle = fopen(exportPath, "ab")) == NULL){
+
+       FILE * myOutputHandle = fopen(exportPath, "ab");
+       if (myOutputHandle == NULL)
+       {
                Log(LOGERR, "Unable to export GPIO pin %f\n", GPIOPin);
        }
        strcpy(setValue, GPIOString);
@@ -21,12 +24,16 @@ void pinDirection(int GPIOPin, int io) {
        if ((myOutputHandle = fopen(GPIODirection, "rb+")) == NULL){
                Log(LOGERR, "Unable to open direction handle for pin %f\n", GPIOPin);
        }
-       if (io == 1) {
+       if (io == 1) 
+       {
                strcpy(setValue,"out");
                fwrite(&setValue, sizeof(char), 3, myOutputHandle);
-       else if (io == 0) {
+       }
+       else if (io == 0) 
+       {
                strcpy(setValue,"in");
                fwrite(&setValue, sizeof(char), 2, myOutputHandle);
+       }
        else Log(LOGERR, "GPIO direction must be 1 or 0\n");
        fclose(myOutputHandle);
 }
@@ -69,9 +76,9 @@ int ADCRead(int adc_num)
        snprintf(adc_path, sizeof(adc_path), "%s%d", ADCPath, adc_num);         // Construct ADC path
        int sensor = open(adc_path, O_RDONLY);                                                          
        char buffer[64];                                                                                                        // I think ADCs are only 12 bits (0-4096), buffer can probably be smaller
-       int read = read(sensor, buffer, sizeof(buffer);
-       if (read != -1) {
-               buffer[read] = NULL;
+       int r = read(sensor, buffer, sizeof(buffer));
+       if (r != -1) {
+               buffer[r] = '\0';
                int value = atoi(buffer);
                double convert = (value/4096) * 1000;                                                   // Random conversion factor, will be different for each sensor (get from datasheets)
                // lseek(sensor, 0, 0); (I think this is uneeded as we are reopening the file on each sensor read; if sensor is read continously we'll need this
@@ -96,9 +103,10 @@ int pinRead(int GPIOPin)
        snprintf(GPIOValue, sizeof(GPIOValue), "%s%d%s", valuePath, GPIOPin, "/value"); //construct pin path
        int pin = open(GPIOValue, O_RDONLY);
        char ch;
-       lseek(fd, 0, SEEK_SET);
-       int read = read(pin, &ch, sizeof(ch);
-       if (read != -1) {
+       lseek(pin, 0, SEEK_SET);
+       int r = read(pin, &ch, sizeof(ch));
+       if (r != -1) 
+       {
                if (ch != '0') {
                        close(pin);
                        return 1;
@@ -107,7 +115,9 @@ int pinRead(int GPIOPin)
                        close(pin);
                        return 0;
                }
-       else {
+       }
+       else 
+       {
                Log(LOGERR, "Failed to get value from pin %f\n", GPIOPin);
                close(pin);
                return -1;
@@ -125,4 +135,4 @@ void pinUnexport(int GPIOPin) {
        strcpy(setValue, GPIOString);
        fwrite(&setValue, sizeof(char), 2, myOutputHandle);
        fclose(myOutputHandle);
-}
\ No newline at end of file
+}
index 1da8470..b4ded0c 100644 (file)
 #include <sched.h>
 #include <stdint.h>
 
-#define exportPath             "/sys/class/gpio/export";
-#define unexportPath   "/sys/class/gpio/unexport";
-#define valuePath              "/sys/class/gpio/gpio";
-#define directionPath  "/sys/class/gpio/gpio";
-#define ADCPath                "/sys/devices/platform/tsc/ain";
+#include "common.h"
+
+#define exportPath             "/sys/class/gpio/export"
+#define unexportPath   "/sys/class/gpio/unexport"
+#define valuePath              "/sys/class/gpio/gpio"
+#define directionPath  "/sys/class/gpio/gpio"
+#define ADCPath                "/sys/devices/platform/tsc/ain"
 
 void pinExport(int GPIOPin);
 void pinDirection(int GPIOPin, int io);
 void pinSet(double value, int GPIOPin);
 void pinUnexport(int GPIOPin);
 int pinRead(int GPIOPin);
-int ADCRead(int adc_num);
\ No newline at end of file
+int ADCRead(int adc_num);
index 3f84ed0..a3580b7 100644 (file)
@@ -171,11 +171,12 @@ bool Sensor_Read(Sensor * s, DataPoint * d)
                }
                case ANALOG_REALTEST:
                {
-                       d->value = ADCRead(0);  //ADC #0 on the Beaglebone
+                       //d->value = ADCRead(0);        //ADC #0 on the Beaglebone
                        break;
                }
                case ANALOG_FAIL0:
-                       d->value = (double)(rand() % 6) * -( rand() % 2) / ( rand() % 100 + 1);
+                       d->value = 250;
+                       //d->value = (double)(rand() % 6) * -( rand() % 2) / ( rand() % 100 + 1);
                        //Gives a value between -5 and 5
                        break;
                case DIGITAL_TEST0:
@@ -188,7 +189,7 @@ bool Sensor_Read(Sensor * s, DataPoint * d)
                {
                // Can pass pin as argument, just using 20 as an example here
                // Although since pins will be fixed, can just define it here if we need to
-                       d->value = pinRead(20); //Pin 20 on the Beaglebone
+                       //d->value = pinRead(20);       //Pin 20 on the Beaglebone
                        break;
                }
                case DIGITAL_FAIL0:

UCC git Repository :: git.ucc.asn.au