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

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