Merge branch 'master' of github:szmoore/MCTX3420 into report
[matches/MCTX3420.git] / server / sensors / strain.c
1 #include "strain.h"
2
3 #include "../log.h"
4 #include "../bbb_pin.h"
5
6 #include <pthread.h>
7
8 #define STRAIN_ADC ADC0
9 // TODO: Choose this
10 #define STRAIN_GPIO 45
11
12 /**
13  * Convert Strain gauge id number to a GPIO pin on the Mux
14  * @param id - The strain gauge id
15  * @returns - GPIO pin number
16  */
17 static int Strain_To_GPIO(StrainID id)
18 {
19         // Could use a lookup table; that would assume strict ordering of the enum in strain.h
20         //static int lookup[] = {GPIO0_30, GPIO1_28,GPIO0_31,GPIO1_16};
21         //if (id < STRAIN0 || id > STRAIN3)
22         //      Fatal("unknown strain id %d", id);
23         //return lookup[id];
24
25         switch (id)
26         {
27                 case STRAIN0:
28                         return 44;
29                 case STRAIN1:
30                         return 26;
31                 case STRAIN2:
32                         return 46;
33                 case STRAIN3:
34                         return 65;
35                 default:
36                         Fatal("Unknown StrainID %d", id);
37                         return -1; // Should never happen
38   }
39 }
40
41 /**
42  * Convert ADC Strain reading to a physically meaningful value
43  * @param reading - The ADC reading
44  * @returns Something more useful
45  */
46 static double Strain_Calibrated(int reading)
47 {
48         return (double)(reading);
49 }
50
51 /**
52  * Initialise a Strain gauge
53  * @param id - The strain gauge to initialise
54  * @param name - Name of the strain gauge; ignored
55  * @returns true if initialisation was successful
56  */
57 bool Strain_Init(const char * name, int id)
58 {
59         int gpio_num = Strain_To_GPIO(id);
60         GPIO_Export(gpio_num);
61         if (!GPIO_Set(gpio_num, false))
62                 Fatal("Couldn't set GPIO%d for strain sensor %d to LOW", gpio_num, id);
63
64         static int init = 0;
65         if (++init == 1)
66         {
67                 GPIO_Export(STRAIN_GPIO);
68                 GPIO_Set(STRAIN_GPIO, true);
69                 ADC_Export(STRAIN_ADC);
70         }
71         return true;
72 }
73
74 bool Strain_Cleanup(int id)
75 {
76         static int killed = 0;
77         if (++killed == 4)
78         {
79
80                 GPIO_Set(STRAIN_GPIO, false);
81                 ADC_Unexport(STRAIN_ADC);
82         }
83
84         int gpio_num = Strain_To_GPIO(id);
85         GPIO_Unexport(gpio_num);
86         return true;
87 }
88
89 bool Strain_Sanity(int id, double value)
90 {
91         return true;
92 }
93
94 /**
95  * Read from a Strain gauge
96  * @param id - The strain gauge to read
97  * @param val - Will store the read value if successful
98  * @returns true on successful read
99  */
100 bool Strain_Read(int id, double * value)
101 {
102         static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // CRITICAL SECTION - Only one Strain gauge can be read at a time
103         pthread_mutex_lock(&mutex);
104
105         int gpio_num = Strain_To_GPIO(id);
106         
107         // Set the multiplexer
108         if (!GPIO_Set(gpio_num, true))
109                 Fatal("Couldn't set GPIO%d for strain sensor %d to HIGH (before reading)", gpio_num,id);
110
111         // Read the ADC
112         int tmp = 0;
113         bool result = ADC_Read(STRAIN_ADC, &tmp); // If this fails, it's not fatal
114         
115         //TODO: Callibrate?
116         *value = Strain_Calibrated(tmp);
117
118         // Unset the multiplexer
119         if (!GPIO_Set(gpio_num, false))
120                 Fatal("Couldn't set GPIO%d for strain sensor %d to LOW (after reading)", gpio_num, id);
121
122         pthread_mutex_unlock(&mutex);
123
124         return result;
125 }

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