Update nginx/rsyslog config to add specific log for LOGWARN or above
[matches/MCTX3420.git] / BBB code / Actuator_SetValue_real.c
1 #include "pwm.h"
2
3 char pin_dir = "/sys/class/gpio/gpio";          //move these
4 int pwm_active = 0;
5
6 /** Sets a GPIO pin to the desired value
7 *       @param value - the value (1 or 0) to write to the pin
8 *       @param pin_num - the number of the pin (refer to electronics team)
9 */
10
11 void SetPin(int value, int pin_num) {
12         int pin;
13         char buffer[10];
14         char pin_path[40];
15         snprintf(pin_path, sizeof(pin_path), "%s%d%s", pin_dir, pin_num, "/value");     //create pin path
16         pin = open(pin_path, O_WRONLY);
17         if (pin < 0) perror("Failed to open pin");
18         if (value) {
19                 strncpy(buffer, "1", ARRAY_SIZE(buffer) - 1);
20         }
21         else {
22                 strncpy(buffer, "0", ARRAY_SIZE(buffer) - 1);
23         }
24         int write = write(pin, buffer, strlen(buffer));
25         if (write < 0) perror ("Failed to write to pin");
26         close(pin);
27 }
28
29 //TODO: Be able to write to multiple PWM modules - easy to change
30 //              but current unsure about how many PWM signals we need
31
32 /**
33  * Set an Actuator value
34  * @param a - The Actuator
35  * @param value - The value to set
36  */
37 void Actuator_SetValue(Actuator * a, double value)
38 {
39         // Set time stamp
40         struct timeval t;
41         gettimeofday(&t, NULL);
42
43         DataPoint d = {TIMEVAL_DIFF(t, g_options.start_time), value};
44         switch (a->id)
45         {
46                 case ACTUATOR_TEST0:                                            //Pressure regulator example - requires PWM, 0-1000kPa input
47                 {       
48                         if (pwm_active == 0) {                                  //if inactive, start the pwm module
49                                 pwm_init();
50                                 pwm_start();
51                                 pwm_set_period(FREQ);                           //50Hz defined in pwm header file
52                         }
53                         if(value >= 0 && value <= 700) {
54                                 double duty = value/1000 * 100;         //convert pressure to duty percentage
55                                 pwm_set_duty((int)duty);                        //set duty percentage for actuator
56                         }
57                 } break;
58                 case ACTUATOR_TEST1:
59                 {
60                         SetPin(value, 1);                                               //Digital switch example - "1" is the pin number, to be specified by electronics team
61                 } break;
62         }
63
64         Log(LOGDEBUG, "Actuator %s set to %f", g_actuator_names[a->id], value);
65
66         // Record the value
67         Data_Save(&(a->data_file), &d, 1);
68 }

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