9492de7c69483e0ced0348a02901b299f7774644
[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
10 /**
11  * Convert Strain gauge id number to a GPIO pin on the Mux
12  * @param id - The strain gauge id
13  * @returns - GPIO pin number
14  */
15 static int Strain_To_GPIO(StrainID id)
16 {
17         // Could use a lookup table; that would assume strict ordering of the enum in strain.h
18         //static int lookup[] = {GPIO0_30, GPIO1_28,GPIO0_31,GPIO1_16};
19         //if (id < STRAIN0 || id > STRAIN3)
20         //      Fatal("unknown strain id %d", id);
21         //return lookup[id];
22
23         switch (id)
24         {
25                 case STRAIN0:
26                         return GPIO0_30;
27                 case STRAIN1:
28                         return GPIO1_28;
29                 case STRAIN2:
30                         return GPIO0_31;
31                 case STRAIN3:
32                         return GPIO1_16;
33                 default:
34                         Fatal("Unknown StrainID %d", id);
35                         return -1; // Should never happen
36   }
37 }
38
39 /**
40  * Convert ADC Strain reading to a physically meaningful value
41  * @param reading - The ADC reading
42  * @returns Something more useful
43  */
44 static double Strain_Calibrated(int reading)
45 {
46         return (double)(reading);
47 }
48
49 /**
50  * Initialise a Strain gauge
51  * @param id - The strain gauge to initialise
52  * @param name - Name of the strain gauge; ignored
53  * @returns true if initialisation was successful
54  */
55 bool Strain_Init(const char * name, int id)
56 {
57         int gpio_num = Strain_To_GPIO(id);
58         GPIO_Export(gpio_num);
59         if (!GPIO_Set(gpio_num, false))
60                 Fatal("Couldn't set GPIO%d for strain sensor %d to LOW", gpio_num, id);
61
62         static bool init_adc = false;
63         if (!init_adc)
64         {
65                 init_adc = true;
66                 ADC_Export(STRAIN_ADC);
67         }
68         return true;
69 }
70
71 /**
72  * Read from a Strain gauge
73  * @param id - The strain gauge to read
74  * @param val - Will store the read value if successful
75  * @returns true on successful read
76  */
77 bool Strain_Read(int id, double * value)
78 {
79         static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // CRITICAL SECTION - Only one Strain gauge can be read at a time
80         pthread_mutex_lock(&mutex);
81
82         int gpio_num = Strain_To_GPIO(id);
83         
84         // Set the multiplexer
85         if (!GPIO_Set(gpio_num, true))
86                 Fatal("Couldn't set GPIO%d for strain sensor %d to HIGH (before reading)", gpio_num,id);
87
88         // Read the ADC
89         int tmp = 0;
90         bool result = ADC_Read(STRAIN_ADC, &tmp); // If this fails, it's not fatal
91         
92         //TODO: Callibrate?
93         *value = Strain_Calibrated(tmp);
94
95         // Unset the multiplexer
96         if (!GPIO_Set(gpio_num, false))
97                 Fatal("Couldn't set GPIO%d for strain sensor %d to LOW (after reading)", gpio_num, id);
98
99         pthread_mutex_unlock(&mutex);
100
101         return result;
102 }

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