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
SetCycleTime.cpp
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <unistd.h>
3
4 #include "../hubolib.h"
5 #include "../hubocfg.h" // Required for changing default I2C device.
6
7 using namespace HuboLib;
8
9 /*
10 Compile and link:
11 g++ SetCycleTime.cpp -L../ -lhubo -lpthread -lrt -o SetCycleTime.out
12 Run:
13 sudo ./SetCycleTime.out
14 Purpose:
15 The cycle time specifies how fast the IO chips are polled/set by the background thread.
16 It therefore determines the frequency of which the internal buffers are updated.
17 As we've learned from the oversampling-demos there is of cause the natural
18 limitation given to the frequency you can request. This limit is depending on the CPU
19 speed the scheduler of the OS and other threads running at highest priority and of cause
20 by the number of analog channels to be oversampled (including their oversampling count).
21
22 The fastest polling that could reasonable be achieved (without breaking the cycle time)
23 worked out to be 2ms (hence 500Hz) when latching 1 analog channel only. The absolute
24 maximum is 1ms referring to 1000Hz.
25 The default setting for the cycle time is 10ms (100Hz).
26 */
27
28 int main(void)
29 {
30 // 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"
31 #ifdef BPI
32 g_I2CConfig.m_sI2CDevice = "/dev/i2c-0";
33 #endif
34
35 // Initialize the library once in your program.
36 if (!Initialize())
37 {
38 printf ("Error: Initialize\n");
39 return 1;
40 }
41
42 // Let's use the default cycle time first.
43
44 printf ("Lower input 2 in order to switch to a higher cycle time of 1s.\n");
45
46 unsigned char allInputs = 0;
47 while (1)
48 {
49 // It doesn't matter how fast the main thread is polling.
50 // It would only access the buffers (but not directly the hardware) anyway!
51 Get_DI_Channels(allInputs);
52 printf ("Digital inputs (hex) = 0x%02X\n", allInputs);
53
54 usleep(10000);
55
56 // Break if input 2 is lowered.
57 if (~allInputs & 4)
58 break;
59 }
60 // Result: You'll see an almost immediate change of the input once triggered. This is why the
61 // buffer gets updated 100 times a second.
62
63
64 // Lower the cycle time to 1s.
65 Set_Cycle_Time(1000);
66
67 printf ("Lower input 1 in order to switch to a higher cycle time of 1s.\n");
68
69 while (1)
70 {
71 Get_DI_Channels(allInputs);
72 printf ("Digital inputs (hex) = 0x%02X\n", allInputs);
73
74 usleep(10000);
75
76 // Break if input 1 is lowered.
77 if (~allInputs & 2)
78 break;
79 }
80 // Result: Here the buffer only gets updated once a second. Hence access to it will return
81 // a former latched value. Also - if switching the inputs inbetween a cycle - you might not
82 // recognize an input change as polling is to slow for fast signals.
83
84 // Free library resources.
85 Uninitialize();
86
87 return 0;
88 }
HuboLib::I2C_Config::m_sI2CDevice
const char * m_sI2CDevice
Definition: hubocfg.h:48
main
int main(void)
Definition: SetCycleTime.cpp:28
HuboLib::Initialize
bool Initialize()
Initializes the library.
HuboLib::Set_Cycle_Time
bool Set_Cycle_Time(long cycleTime)
Sets the backgrounds threads polling interval in ms.
HuboLib::g_I2CConfig
I2C_Config g_I2CConfig
HuboLib::Uninitialize
void Uninitialize()
Releases any resources bound to the library.
HuboLib::Get_DI_Channels
bool Get_DI_Channels(unsigned char &value)
Retrieves the value of all digital inputs.
HuboLib
Definition: hubocfg.h:10