Merge branch 'master' of https://github.com/szmoore/MCTX3420.git
[matches/MCTX3420.git] / BBB code / Actuator_SetValue_real2.c
1 #include "gpio.h"
2 #include "pwm.h"
3
4 void Actuator_SetValue(Actuator * a, double value)
5 {
6         // Set time stamp
7         struct timeval t;
8         gettimeofday(&t, NULL);
9
10         DataPoint d = {TIMEVAL_DIFF(t, g_options.start_time), value};
11         switch (a->id)
12         {
13                 case ACTUATOR_TEST0:                    //LED actuator test code, should blink onboard LED next to Ethernet port
14                         FILE *LEDHandle = NULL;         //code reference: http://learnbuildshare.wordpress.com/2013/05/19/beaglebone-black-controlling-user-leds-using-c/
15                         char *LEDBrightness = "/sys/class/leds/beaglebone\:green\:usr0/brightness";
16                         if(value == 1) {
17                                 if((LEDHandle = fopen(LEDBrightness, "r+")) != NULL) {
18                                         fwrite("1", sizeof(char), 1, LEDHandle);
19                                         fclose(LEDHandle);
20                                 }
21                         else if(value == 0) {
22                                 if((LEDHandle = fopen(LEDBrightness, "r+")) != NULL) {
23                                         fwrite("0", sizeof(char), 1, LEDHandle);
24                                         fclose(LEDHandle);
25                         }
26                         else perror("Pin value should be 1 or 0");
27                         break;
28                 case ACTUATOR_TEST1:
29                         // Quick actuator function for testing pins
30                         // GPIOPin can be either passed as an argument, or defined here (as pins won't change)
31                         // First way is better and more generalised
32                         int GPIOPin = 13;
33                         // Modify this to only export on first run, unexport on shutdown
34                         pinExport(setValue, GPIOString);
35                         pinDirection(GPIODirection, setValue);
36                         pinSet(value, GPIOValue, setValue);
37                         pinUnexport(setValue, GPIOString);
38                         break;
39                 case ACTUATOR_TEST2:
40                         if (pwminit == 0) {                                     //if inactive, start the pwm module
41                                 pwm_init();
42                         }
43                         if (pwmstart == 0) {
44                                 pwm_start();
45                                 pwm_set_period(FREQ);                           //50Hz defined in pwm header file
46                         }
47                         if(value >= 0 && value <= 1000) {
48                                 double duty = value/1000 * 100;         //convert pressure to duty percentage
49                                 pwm_set_duty((int)duty);                        //set duty percentage for actuator
50                         }
51         }
52         Log(LOGDEBUG, "Actuator %s set to %f", g_actuator_names[a->id], value);
53         // Record the value
54         Data_Save(&(a->data_file), &d, 1);
55 }

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