Hardware Reference
In-Depth Information
openFrameworks
openFrameworks is used for this setup to create an application to create and transmit known data as a simulated
sensor network over a single XBee module connected to a computer. As in Chapter 3, a program is created in a
C++ compiler such as Code::Blocks and is made of at least three source files. A copy of the empty example found in
openFrameworks directory/apps/myApps can be used as a base for the first part of the sensor network code. You
need to modify the main.cpp file to set the drawing window to a smaller size by changing the ofSetupOpenGL function
call to create a 276×276-pixel window. Change the call to match the following line of code: ofSetupOpenGL(&window,
276, 276, OF_WINDOW); .
testapp.cpp handles data creation and packet construction, responds to flow control, and graphically draws
and indicates what data is being sent. The testapp.cpp code can be replaced with the code from Listing 8-1. The
example is made of seven different functions. Part 1 sets up the serial connection declared by the serial object in
testapp.h . The serial is connected to the location of the USB serial adapter (COM or TTY, depending on the system)
and is connected at 115200 baud. The setup function initializes a destination address of a broadcast for this example,
as well as flags needed for program control. Three unsigned byte arrays of 256 bytes are filled with data created by a
sine wave-generation equation. The sine waves are zeroed at a value of 127. The sine wave follows the form
y = a + b sin( k ( x - c )), where a is the vertical transformation, b is the amplitude, c sets the horizontal shift, and
k affects the period. The data generated will be drawn to the computer's screen and sent over the XBee module to be
eventuality displayed on an Android device.
Listing 8-1. testApp.cpp, Part 1 of 7
#include "testApp.h"
void testApp::setup(){
printf ("Start \n");
serial.setup("/dev/ttyUSB0", 115200); // change to match where the Arduino is connected
for (int i = 0; i < 256; i++){
graph[i] = 127 + (100 * sin((1*(PI/127))*(i-0))); // sine functions
graph1[i] = 127 + (75 * sin((2*(PI/127))*(i-10))); // normalized in a 256×256-value area
graph2[i] = 127 + (50 * sin((3*(PI/127))*(i-40)));
} // end data fill installation
for (int i = 0; i < 10; i++){
destADR[i] = 0x00; // set the 64-bit broadcast address
} // end address fill
destADR[6] = 0xFF; // set network broadcast address
destADR[7] = 0xFF;
destADR[8] = 0xFF;
destADR[9] = 0xFE;
point = 0; // zero data point indicator
counts = 0; // used to delay packet send timing
SensorsSent [0] = false; // packet flags
SensorsSent [1] = false;
SensorsSent [2] = false;
FirstPacketsent = false;
} // end testApp::setup()
The next function is the loop that runs constantly during program execution. The update function waits for a
set time to pass before trying to send the each of the sensor's data. The time is based upon the amount of times the
update function is run and will vary depending on the complexity of the code run. On average, the data is sent in
intervals of half a second. Each time a data packet is sent, the code waits for a reply of an “OK” or “BAD,” signifying
 
Search WWH ::




Custom Search