Hubo Library

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

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.
31 bool InitializeHuboHardware ();
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.
80 BrightnessControl();
81
82 // Update temperature controller.
83 TemperatureControl();
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
95 void BrightnessControl ()
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.
106 Set_DO_Channel(LIGHT_CONTROLLER_RELAY, true);
107 }
108 else
109 if (darkness < LIGHT_OFF)
110 {
111 // Turn relay off.
112 Set_DO_Channel(LIGHT_CONTROLLER_RELAY, false);
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
131 void TemperatureControl ()
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
139 BoostThreadPriority ();
140
141 if (temperature < TEMP_ON)
142 {
143 // Turn heating rc switch on.
144 printf ("-> heating on.\n");
145 rcSwitch.SwitchSocket(FAMILY_CODE, TEMP_CONTROLLER_SOCKET, true, 4, 10);
146
147 // Turn relay on.
148 Set_DO_Channel(TEMP_CONTROLLER_RELAY, true);
149 }
150 else
151 if (temperature > TEMP_OFF)
152 {
153 // Turn heating rc switch off.
154 printf ("-> heating off.\n");
155 rcSwitch.SwitchSocket(FAMILY_CODE, TEMP_CONTROLLER_SOCKET, false, 4, 10);
156
157 // Turn relay off.
158 Set_DO_Channel(TEMP_CONTROLLER_RELAY, false);
159 }
160 else
161 printf ("-> no change.\n");
162
163 RestoreThreadPriority ();
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
179 bool InitializeApplication()
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
206 bool InitializeHuboHardware()
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... ");
227 Wait_For_MCP3x08_Buffered_Values();
228 printf ("received.\n");
229
230 printf ("Waiting for MCP23017 buffered values... ");
231 Wait_For_MCP23017_Buffered_Values ();
232 printf ("received.\n");
233
234 return true;
235 }
236
237 bool SaveThreadPriority()
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
251 void BoostThreadPriority()
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
262 void RestoreThreadPriority()
263 {
264 // Restore scheduling parameters of thread.
265 int ret = pthread_setschedparam(g_threadHandle, g_policy, &g_param);
266 assert (ret == 0);
267 }
HuboLib::I2C_Config::m_sI2CDevice
const char * m_sI2CDevice
Definition: hubocfg.h:48
CSC5262.h
main
int main(int argc, char *argv[])
Definition: TempController.cpp:68
HuboLib::Wait_For_MCP3x08_Buffered_Values
bool Wait_For_MCP3x08_Buffered_Values()
Waits until the input buffer of the MCP3x08 are initialized from the hardware.
HuboLib::Delay_MicroSeconds
void Delay_MicroSeconds(unsigned long delay_micros)
delays the execution of the calling thread for the given number of micro seconds. ...
MAX_MCP3x08_CHANNELS
#define MAX_MCP3x08_CHANNELS
Definition: hubolib.h:39
FAMILY_CODE
#define FAMILY_CODE
Definition: TempController.cpp:42
BCM2835::IsGPIOInitialized
bool IsGPIOInitialized()
Returns the status of the initialisation of the GPIO part of the library.
InitializeApplication
bool InitializeApplication()
Definition: TempController.cpp:179
TEMP_CONTROLLER_RELAY
#define TEMP_CONTROLLER_RELAY
Definition: TempController.cpp:55
TEMP_ON
#define TEMP_ON
Definition: TempController.cpp:52
HuboLib::Set_DO_Channel
bool Set_DO_Channel(int channel, bool bValue)
Requests the background thread to set one of the digital outputs to the value specified.
CSC5262
Definition: CSC5262.h:5
HuboLib::Initialize
bool Initialize()
Initializes the library.
LIGHT_OFF
#define LIGHT_OFF
Definition: TempController.cpp:62
std
TEMP_OFF
#define TEMP_OFF
Definition: TempController.cpp:53
TEMP_SENSOR_AI_CHANNEL
#define TEMP_SENSOR_AI_CHANNEL
Definition: TempController.cpp:54
LIGHT_CONTROLLER_RELAY
#define LIGHT_CONTROLLER_RELAY
Definition: TempController.cpp:65
GetTemperature
bool GetTemperature(int channel, double &temperature)
Definition: TempController.cpp:166
HuboLib::Set_Cycle_Time
bool Set_Cycle_Time(long cycleTime)
Sets the backgrounds threads polling interval in ms.
TEMP_CONTROLLER_SOCKET
#define TEMP_CONTROLLER_SOCKET
Definition: TempController.cpp:43
g_param
sched_param g_param
Definition: TempController.cpp:37
LIGHT_SENSOR_AI_CHANNEL
#define LIGHT_SENSOR_AI_CHANNEL
Definition: TempController.cpp:64
TemperatureControl
void TemperatureControl()
Definition: TempController.cpp:131
SaveThreadPriority
bool SaveThreadPriority()
Definition: TempController.cpp:237
InitializeHuboHardware
bool InitializeHuboHardware()
Definition: TempController.cpp:206
RestoreThreadPriority
void RestoreThreadPriority()
Definition: TempController.cpp:262
BoostThreadPriority
void BoostThreadPriority()
Definition: TempController.cpp:251
HuboLib::g_I2CConfig
I2C_Config g_I2CConfig
LIGHT_ON
#define LIGHT_ON
Definition: TempController.cpp:63
HuboLib::Get_AI_Channel
bool Get_AI_Channel(int channel, unsigned long &count, double &volt)
Get the buffered and oversampled data from the MCP3x08.
rcSwitch
CSC5262 rcSwitch(350, 17, true)
BrightnessControl
void BrightnessControl()
Definition: TempController.cpp:95
HuboLib::Set_MCP3x08_Oversampling
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.
HuboLib::Uninitialize
void Uninitialize()
Releases any resources bound to the library.
g_threadHandle
pthread_t g_threadHandle
Definition: TempController.cpp:39
g_policy
int g_policy
Definition: TempController.cpp:38
HuboLib::Wait_For_MCP23017_Buffered_Values
bool Wait_For_MCP23017_Buffered_Values()
Waits until input and output buffers of the MCP23017 master are initialized from the hardware...
BCM2835
Definition: hubocfg.h:71
HuboLib::Get_MCP9701_Temperature
double Get_MCP9701_Temperature(double volt)
Converts a voltage value of an MCP9701 temperature sensor into the equivalent temperature.
CSC5262::SwitchSocket
bool SwitchSocket(unsigned char SystemCode, unsigned char Receiver, bool bTurnOn, int repeatedSuccess, int totalTransmittion)
Definition: CSC5262.cpp:128
GetDarkness
bool GetDarkness(int channel, unsigned short &brightness)
Definition: TempController.cpp:118
HuboLib
Definition: hubocfg.h:10