Hubo Library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
TempController.cpp
Go to the documentation of this file.
1 /*
2 Compile and link:
3  g++ TemController.cpp CSC5262.cpp -L../ -lhubo -lpthread -lrt -o TemController
4 Run:
5  sudo ./TemController
6 Purpose:
7  The demo uses a Raspberry Pi to illustrate:
8  - a 2-way controller that reads a MCP9701 temperature on analog input channel 0 and switches RCSocket (31/1) and relay 7
9  when the temperature is out of limits TEMP_ON and TEMP_OFF.
10  - a 2-way controller that reads the darkness of a foto resistor on analog input channel 1 and switches relay 6
11  when the the digits are out of limits LIGHT_ON and LIGHT_OFF.
12 */
13 
14 #include <stdio.h>
15 #include <assert.h>
16 #include <pthread.h>
17 
18 #include "../hubolib.h"
19 #include "../hubocfg.h"
20 #include "CSC5262.h"
21 
22 using namespace std;
23 using namespace HuboLib;
24 using namespace BCM2835;
25 
26 
27 // Global initialization.
28 bool InitializeApplication ();
29 
30 // Initialize Hubo hardware.
32 
33 // Helper for temporary priority changes.
34 bool SaveThreadPriority ();
35 void BoostThreadPriority ();
36 void RestoreThreadPriority ();
37 sched_param g_param;
38 int g_policy = 0;
39 pthread_t g_threadHandle = 0;
40 
41 // RCSocket to control remote switches.
42 #define FAMILY_CODE (31)
43 #define TEMP_CONTROLLER_SOCKET (1)
44 #define REED_CONTACT_SOCKET (2)
45 CSC5262 rcSwitch (350, 17, true);
46 
47 // Temperature control
48 // Sensor: MCP9701(A) on analog input 0
49 // Actuator: RCSocket 31/1, Relay 7
50 void TemperatureControl ();
51 bool GetTemperature (int channel, double& temperature);
52 #define TEMP_ON (27.0)
53 #define TEMP_OFF (29.0)
54 #define TEMP_SENSOR_AI_CHANNEL (0)
55 #define TEMP_CONTROLLER_RELAY (7)
56 
57 // Light control
58 // Sensor: foto resistor on voltage devider (of 5V) on analog input 1
59 // Actuator: Relay 6
60 void BrightnessControl ();
61 bool GetDarkness (int channel, unsigned short& brightness);
62 #define LIGHT_OFF (2000)
63 #define LIGHT_ON (2500)
64 #define LIGHT_SENSOR_AI_CHANNEL (1)
65 #define LIGHT_CONTROLLER_RELAY (6)
66 
67 
68 int main(int argc, char *argv[])
69 {
70  // Initialization.
71  if (!InitializeApplication())
72  {
73  printf ("Failed to initialize. Are you running the program as sudoer?\n");
74  return -1;
75  }
76 
77  while (1)
78  {
79  // Update brightsness controller.
81 
82  // Update temperature controller.
84 
85  // Sleep some time.
86  Delay_MicroSeconds(1000L*1000L);
87  }
88 
89  // Even if we should never get here...
90  Uninitialize();
91 
92  return 0;
93 }
94 
96 {
97  unsigned short darkness = 0;
98  if (!GetDarkness (LIGHT_SENSOR_AI_CHANNEL, darkness))
99  return;
100 
101  // printf ("Darkness = %d digit.\n", darkness);
102 
103  if (darkness > LIGHT_ON)
104  {
105  // Turn relay on.
107  }
108  else
109  if (darkness < LIGHT_OFF)
110  {
111  // Turn relay off.
113  }
114  else
115  printf ("-> no change.\n");
116 }
117 
118 bool GetDarkness (int channel, unsigned short& darkness)
119 {
120  unsigned long adcCount;
121  double volt;
122 
123  if (!Get_AI_Channel (channel, adcCount, volt))
124  return false;
125 
126  darkness = (unsigned short) adcCount;
127 
128  return true;
129 }
130 
132 {
133  double temperature = 0.0;
134  if (!GetTemperature (TEMP_SENSOR_AI_CHANNEL, temperature))
135  return;
136 
137  printf ("Temperature = %lf deg. C ", temperature);
138 
140 
141  if (temperature < TEMP_ON)
142  {
143  // Turn heating rc switch on.
144  printf ("-> heating on.\n");
146 
147  // Turn relay on.
149  }
150  else
151  if (temperature > TEMP_OFF)
152  {
153  // Turn heating rc switch off.
154  printf ("-> heating off.\n");
156 
157  // Turn relay off.
159  }
160  else
161  printf ("-> no change.\n");
162 
164 }
165 
166 bool GetTemperature (int channel, double& temperature)
167 {
168  unsigned long adcCount;
169  double volt;
170 
171  if (!Get_AI_Channel (channel, adcCount, volt))
172  return false;
173 
174  temperature = Get_MCP9701_Temperature(volt);
175 
176  return true;
177 }
178 
180 {
181  // Try reading thread scheduling information.
182  if (!SaveThreadPriority())
183  {
184  printf ("Failed to save scheduling parameter. Are you running the program as sudoer?\n");
185  return false;
186  }
187 
188  // If this call doesn't return true then we must not use the GPIO part of Hubo library!
189  printf ("Initializing GPIO.\n");
190  if (!IsGPIOInitialized ())
191  {
192  printf ("GPIO not properly initialized. Are you running the program as sudoer?\n");
193  return false;
194  }
195 
196  // Initialize Hubo hardware and expect at least one slave board installed.
197  if (!InitializeHuboHardware ())
198  {
199  printf ("Hubo hardware containing at least one cascaded slave could not be found.\n");
200  return false;
201  }
202 
203  return true;
204 }
205 
207 {
208  // If required - set the I2C device to work with. The Raspberry Pi uses "/dev/i2c-1" which is default, the Banana Pi uses "/dev/i2c-0"
209  #ifdef BPI
210  g_I2CConfig.m_sI2CDevice = "/dev/i2c-0";
211  #endif
212 
213  printf ("Initializing Hubo hardware.\n");
214 
215  // Initialize the library once in your program.
216  Initialize();
217  Set_Cycle_Time(20);
218 
219  // Wait until all background buffers are filled.
220 
221  // Define all channels to be read (set to 1) from the ADC.
222  unsigned short overSampling[MAX_MCP3x08_CHANNELS] = { 1, 1, 1, 1, 1, 1, 1, 1 };
223  Set_MCP3x08_Oversampling (overSampling);
224 
225  // Wait as long until we have valid values in the buffer.
226  printf ("Waiting for MCP3x08 buffered values... ");
228  printf ("received.\n");
229 
230  printf ("Waiting for MCP23017 buffered values... ");
232  printf ("received.\n");
233 
234  return true;
235 }
236 
238 {
239  printf ("Saving thread parameters.\n");
240 
241  // Thread handle.
242  g_threadHandle = pthread_self();
243 
244  // Save current scheduling parameters of thread.
245  int ret = pthread_getschedparam (g_threadHandle, &g_policy, &g_param);
246 
247  assert (ret == 0);
248  return ret == 0;
249 }
250 
252 {
253  // Scheduling params.
254  sched_param param = g_param;
255 
256  // Set scheduling parameters of thread to real time values (FIFO scheduling type and max prio).
257  param.sched_priority = sched_get_priority_max(SCHED_FIFO); // New max priority for new scheduling concept.
258  int ret = pthread_setschedparam(g_threadHandle, SCHED_FIFO, &param);
259  assert (ret == 0);
260 }
261 
263 {
264  // Restore scheduling parameters of thread.
265  int ret = pthread_setschedparam(g_threadHandle, g_policy, &g_param);
266  assert (ret == 0);
267 }
const char * m_sI2CDevice
Definition: hubocfg.h:48
int main(int argc, char *argv[])
bool Wait_For_MCP3x08_Buffered_Values()
Waits until the input buffer of the MCP3x08 are initialized from the hardware.
void Delay_MicroSeconds(unsigned long delay_micros)
delays the execution of the calling thread for the given number of micro seconds. ...
#define MAX_MCP3x08_CHANNELS
Definition: hubolib.h:39
#define FAMILY_CODE
bool IsGPIOInitialized()
Returns the status of the initialisation of the GPIO part of the library.
bool InitializeApplication()
#define TEMP_CONTROLLER_RELAY
#define TEMP_ON
bool Set_DO_Channel(int channel, bool bValue)
Requests the background thread to set one of the digital outputs to the value specified.
Definition: CSC5262.h:5
bool Initialize()
Initializes the library.
#define LIGHT_OFF
#define TEMP_OFF
#define TEMP_SENSOR_AI_CHANNEL
#define LIGHT_CONTROLLER_RELAY
bool GetTemperature(int channel, double &temperature)
bool Set_Cycle_Time(long cycleTime)
Sets the backgrounds threads polling interval in ms.
#define TEMP_CONTROLLER_SOCKET
sched_param g_param
#define LIGHT_SENSOR_AI_CHANNEL
void TemperatureControl()
bool SaveThreadPriority()
bool InitializeHuboHardware()
void RestoreThreadPriority()
void BoostThreadPriority()
I2C_Config g_I2CConfig
#define LIGHT_ON
bool Get_AI_Channel(int channel, unsigned long &count, double &volt)
Get the buffered and oversampled data from the MCP3x08.
CSC5262 rcSwitch(350, 17, true)
void BrightnessControl()
bool Set_MCP3x08_Oversampling(unsigned short overSampling[MAX_MCP3x08_CHANNELS])
Specifies the ADC channels to be sampled as well as the number they get oversampled.
void Uninitialize()
Releases any resources bound to the library.
pthread_t g_threadHandle
int g_policy
bool Wait_For_MCP23017_Buffered_Values()
Waits until input and output buffers of the MCP23017 master are initialized from the hardware...
double Get_MCP9701_Temperature(double volt)
Converts a voltage value of an MCP9701 temperature sensor into the equivalent temperature.
bool SwitchSocket(unsigned char SystemCode, unsigned char Receiver, bool bTurnOn, int repeatedSuccess, int totalTransmittion)
Definition: CSC5262.cpp:128
bool GetDarkness(int channel, unsigned short &brightness)