Added WaspMote --> Spacebrew Bridge class

This commit is contained in:
dviid
2013-12-01 23:55:00 +01:00
parent e02f8a3d7e
commit cd7fa5c908
10 changed files with 240 additions and 60 deletions
+125
View File
@@ -0,0 +1,125 @@
//
// Bridge.cpp
// waspmote_spacebrew_bridge
//
// Created by dviid on 12/1/13.
//
//
#include "Bridge.h"
#define wasp_signature "usbserial"
#define wasp_baud 115200
bool Bridge::setup_spacebrew() {
_sb_connection.connect(SPACEBREW_SERVER, "CRITTICAL INFRASTRUCTURE"); // blocking ??
sleep(5);
return _sb_connection.isConnected();
}
bool Bridge::setup_serial() {
//_serial.enumerateDevices();
vector <ofSerialDeviceInfo> list = _serial.getDeviceList();
for(int i = 0; i < list.size(); i++) {
if (list[i].getDeviceName().find(wasp_signature) != std::string::npos) {
_serial.setup(list[i].getDevicePath(), wasp_baud);
return true;
}
}
return false;
}
void Bridge::update(){
static vector<WaspFrame> r;
r.clear();
if(_serial.available() >= SERIAL_BUFFER_LEN) {
_serial.readBytes(_buffer, SERIAL_BUFFER_LEN);
_payload += string(_buffer, _buffer + SERIAL_BUFFER_LEN);
_wp.ASCII_process(_payload, r, false);
//LOOK up MAP !!!!!!!!!!!!!!
for(int i = 0; i < r.size(); i++) {
WaspFrame wf = r[i];
map<string, Publisher*>::iterator it = _map.find(wf.mote_id);
if(it == _map.end()) {
Publisher* p = new Publisher(wf.mote_id, &_sb_connection);
p->data(wf.adc_data, wf.battery_level, wf.frame_seq);
_map[wf.mote_id] = p;
} else {
Publisher* p = it->second;
p->data(wf.adc_data, wf.battery_level, wf.frame_seq);
}
}
}
}
Publisher::Publisher(string id, Spacebrew::Connection* sbc) : _id(id), _sb_connection(sbc), _current_seq(0), _is_publish_ready(false)
{
cout << "NEW PUBLISHER -- " << id << endl;
_ci_data_id = _id + "-DATA";
_batt_data_id = _id + "-BATT";
setup_spacebrew_publisher();
}
void Publisher::setup_spacebrew_publisher()
{
if(!_is_publish_ready && _sb_connection->isConnected()) {
// ADD PUBLISHER INFO -- 2 publishers per waspmote (DATA, BATT)
_sb_connection->addPublish(_batt_data_id, "range");
_sb_connection->addPublish(_ci_data_id, "range");
_is_publish_ready = true;
}
}
void Publisher::data(int ci_data, int batt_data, int seq)
{
// if(seq <= _current_seq) return;
// else _current_seq= seq;
_current_seq= seq;
cout << "Publisher: " << _id << endl;
cout << seq << " - " << ci_data << " - " << batt_data << endl;
if(_sb_connection->isConnected()) {
if (!_is_publish_ready) {
setup_spacebrew_publisher();
sleep(1);
}
_sb_connection->sendRange(_ci_data_id, ci_data);
_sb_connection->sendRange(_batt_data_id, batt_data);
}
if (_ci_data.size() >= SERIAL_BUFFER_LEN) _ci_data.pop_front();
if (_batt_data.size() >= SERIAL_BUFFER_LEN) _batt_data.pop_front();
_ci_data.push_back(ci_data);
_batt_data.push_back(batt_data);
}
+84
View File
@@ -0,0 +1,84 @@
//
// Bridge.h
// waspmote_spacebrew_bridge
//
// Created by dviid on 12/1/13.
//
//
#ifndef __waspmote_spacebrew_bridge__Bridge__
#define __waspmote_spacebrew_bridge__Bridge__
#include <iostream>
#include "ofxSpacebrew.h"
#include "WaspFrameParser.h"
static const int MAX_CACHED_DATA = 50;
static const int SERIAL_BUFFER_LEN = 136;
static const std::string SPACEBREW_SERVER = "sandbox.spacebrew.cc";
//static const std::string SPACEBREW_SERVER = "localhost";
class Publisher
{
public:
Publisher(string id, Spacebrew::Connection* sbc);
void setup_spacebrew_publisher();
void data(int ci_data, int batt_data, int seq);
string _id;
string _ci_data_id;
string _batt_data_id;
// cache
deque<int> _ci_data;
deque<int> _batt_data;
int _current_seq;
// spacebrew connection
Spacebrew::Connection* _sb_connection;
bool _is_publish_ready;
// - todo -
};
class Bridge
{
public:
Bridge(){;}
bool setup_spacebrew();
bool setup_serial();
void update(); // threadable
// wasp frame cb
void data(WaspFrame* wf);
Publisher* newPublisher(string id);
// serial interface
ofSerial _serial;
bool _serial_set;
unsigned char _buffer[SERIAL_BUFFER_LEN];
string _payload;
WaspFrameParser _wp;
// spacebrew interface
// map of spacebrew publishers
map<string, Publisher*> _map;
Spacebrew::Connection _sb_connection;
};
#endif /* defined(__waspmote_spacebrew_bridge__Bridge__) */
@@ -70,7 +70,7 @@ void WaspFrameParser::ASCII_token_frame(string &input, vector<string> &output)
}
}
void WaspFrameParser::ASCII_process(string &input, vector<WaspFrame> &output)
void WaspFrameParser::ASCII_process(string &input, vector<WaspFrame> &output, bool emit)
{
vector<string> r;
WaspFrameParser::ASCII_split_frame(input, r);
@@ -90,12 +90,16 @@ void WaspFrameParser::ASCII_process(string &input, vector<WaspFrame> &output)
wp->mote_id = t[2];
wp->frame_seq = atoi(t[3].c_str());
wp->mac = t[4];
wp->adc_data = atoi(WaspFrameParser::ASCII_parse("CI0:", t[5]).c_str());
wp->adc_data = atoi(WaspFrameParser::ASCII_parse(wp->mote_id + ":", t[5]).c_str());
wp->battery_level = atoi(WaspFrameParser::ASCII_parse("BAT:", t[6]).c_str());
//cout << (*wp) << endl;
output.push_back(*wp);
if(emit) {
ofNotifyEvent(_frame_event, wp);
}
}
@@ -13,6 +13,9 @@
#include <string>
#include <vector>
#include "ofEvents.h"
using namespace std;
#define MIN_FRAME_ENTRIES 7
@@ -38,13 +41,16 @@ class WaspFrameParser {
public:
WaspFrameParser(){;}
static void ASCII_split_frame(string &input, vector<string> &output);
static void ASCII_token_frame(string &input, vector<string> &output);
static void ASCII_process(string &input, vector<WaspFrame> &output);
static string ASCII_parse(string id, string input);
void ASCII_process(string &input, vector<WaspFrame> &output, bool emit);
ofEvent<WaspFrame*> _frame_event;
};
#endif /* defined(__waspmote_gateway_parser__WaspFrameParser__) */
+1 -1
View File
@@ -3,7 +3,7 @@
//========================================================================
int main( ){
ofSetupOpenGL(1024,768,OF_WINDOW); // <-------- setup the GL context
ofSetupOpenGL(200,200,OF_WINDOW); // <-------- setup the GL context
// this kicks off the running of my app
// can be OF_WINDOW or OF_FULLSCREEN
+3 -36
View File
@@ -8,50 +8,17 @@
#define wasp_baud 115200
bool testApp::setup_serial() {
//_serial.enumerateDevices();
vector <ofSerialDeviceInfo> list = _serial.getDeviceList();
for(int i = 0; i < list.size(); i++) {
if (list[i].getDeviceName().find(wasp_signature) != std::string::npos) {
_serial.setup(list[i].getDevicePath(), wasp_baud);
return true;
}
}
return false;
}
string wp_ascii_split(string input)
{
}
//--------------------------------------------------------------
void testApp::setup(){
_serial_set = setup_serial();
_bridge.setup_serial();
_bridge.setup_spacebrew();
}
//--------------------------------------------------------------
void testApp::update(){
static vector<WaspFrame> r;
r.clear();
if(_serial.available() >= BUFFER_LEN) {
_serial.readBytes(_buffer, BUFFER_LEN);
_payload += string(_buffer, _buffer + BUFFER_LEN);
WaspFrameParser::ASCII_process(_payload, r);
for(int i = 0; i < r.size(); i++) {
ofLog() << r[i] << endl;
}
}
_bridge.update();
}
//--------------------------------------------------------------
+3 -14
View File
@@ -3,7 +3,8 @@
#include "ofMain.h"
#include "OfSerial.h"
#define BUFFER_LEN 136
#include "Bridge.h"
class testApp : public ofBaseApp{
@@ -22,17 +23,5 @@ class testApp : public ofBaseApp{
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
bool setup_serial();
string wp_ascii_split(string input);
public:
ofSerial _serial;
bool _serial_set;
unsigned char _buffer[BUFFER_LEN];
string _payload;
Bridge _bridge;
};