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
SlaveCycleTimeDivider.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"
6
7 using namespace HuboLib;
8
9 /*
10 Compile and link:
11 g++ SlaveCycleTimeDivider.cpp -L../ -lhubo -lpthread -lrt -o SlaveCycleTimeDivider.out
12 Run:
13 sudo ./SlaveCycleTimeDivider.out
14 Purpose:
15 While the Hubo master module does get updated with the frequency as specified by a call to SetCycleTime()
16 Hubo slaves can get updated slower by setting the cycle tick divider to a value greater than 1.
17 By default the cycle tick devider is set to 1 resulting in the same update speed for the slaves.
18 Note - while this example only demonstrates the behaviour for the digital outputs - it applies to the
19 digital inputs too!
20 */
21
22 int main(void)
23 {
24 // 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"
25 #ifdef BPI
26 g_I2CConfig.m_sI2CDevice = "/dev/i2c-0";
27 #endif
28
29 // Let slaves update only every 5th call of a cycle tick.
30 g_I2CConfig.m_MCP23017SlaveCycleTickDivider = 5;
31
32 // The slave would follow immediately.
33 // g_I2CConfig.m_MCP23017SlaveCycleTickDivider = 1;
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 the master update every 10th second.
43 // Therefore the update interval for slaves evaluates to only 2 Hz (divider is set to 5)!
44 Set_Cycle_Time(100);
45
46 // Makes every thing a little "faster" but still keeps the delay between master and slave.
47 // Set_Cycle_Time(10);
48
49 int slaveNo = 0;
50
51 for (int i=0; i<10; i++)
52 {
53 // Set all channels.
54 printf ("Setting all channels\n");
55 Set_DO_Channels(0xFF);
56 Set_Slave_DO_Channels(slaveNo, 0xFF);
57 usleep (2000000);
58
59 // Reset all channels.
60 printf ("Clearing all channels\n");
61 Set_DO_Channels(0x00);
62 Set_Slave_DO_Channels(slaveNo, 0x00);
63 usleep (2000000);
64 }
65 // Conclusion: You will notice that the slave will be delayed when performing its operation.
66 // While the master (nearly) immediately switches his outputs, the slave will be delayed as for
67 // it's devide value.
68 // Decreasing the Cycle time would of cause also decrease the delay - it would however, still
69 // remain. Setting the divider to 1 would have the effect that the slave immediately follws
70 // the actions of the master.
71
72 // If you omit this "wait" then it might happen that the last call to Set_Slave_DO_Channels(0xFF) is not
73 // sent to the hardware anymore since the Uninitialize() call below has terminated before!
74 usleep (1000000);
75
76 // Free library resources.
77 Uninitialize();
78
79 return 0;
80 }
HuboLib::I2C_Config::m_sI2CDevice
const char * m_sI2CDevice
Definition: hubocfg.h:48
HuboLib::I2C_Config::m_MCP23017SlaveCycleTickDivider
unsigned short m_MCP23017SlaveCycleTickDivider
Definition: hubocfg.h:47
HuboLib::Initialize
bool Initialize()
Initializes the library.
HuboLib::Set_DO_Channels
bool Set_DO_Channels(unsigned char value)
Requests the background thread to update all 8 bits of the digital output to the value specified...
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::Set_Slave_DO_Channels
bool Set_Slave_DO_Channels(int slaveNo, unsigned char value)
Requests the background thread to update all 8 bits of the digital output to the value specified for ...
main
int main(void)
Definition: SlaveCycleTimeDivider.cpp:22
HuboLib
Definition: hubocfg.h:10