A simple wireless protocol to let my examples communicate each other. ssWi stands for Shared Slotted Wireless protocol

Dependents:   rover_car rover_pc supervisor watering_unit ... more

This library aims at implementing a simple communication protocol among nodes, abstracting from the hardware. The name ssWi stands for Shared Slotted Wireless. Wireless is part of the name, even though the library abstracts from the hardware, as the first version was entirely focused on the XBee modules and then the name has not been changed.

The communication channel is represented by ssWiChannel, an abstract class which models the channel that the transceivers access to. The concrete classes must implement the functions: init, read and write. The protocol automatically sends and receives data through the selected channel, exploiting the operting system timers. Addresses are not considered as the communication lays on broadcast transmissions.

The protocol provides the ssWiPort abstraction which is like memory areas shared among all the connected nodes. Reading from one port lets the node retrieve the last written value from the other nodes. Writing on one port means sending such value to other nodes.

Objects instantiated from ssWiSocket are the interface for allowing nodes to access the protocol ports.

/media/uploads/mariob/scheme.png

TODO:

  • improve the parsing of the received messages
  • communication tests with many nodes (so far, only 2 nodes have been tested)
Committer:
mariob
Date:
Mon Sep 24 21:15:42 2012 +0000
Revision:
9:b5b5d0533fa6
Parent:
8:354a0e3087c1
Child:
10:9d5e02dea309
bho

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mariob 4:dbbf82c966c2 1 #include "ssWiChannel.hpp"
mariob 4:dbbf82c966c2 2 #include "ssWiPort.hpp"
mariob 4:dbbf82c966c2 3 #include "ssWi.hpp"
mariob 4:dbbf82c966c2 4
mariob 4:dbbf82c966c2 5 #include "mbed.h"
mariob 4:dbbf82c966c2 6 #include "rtos.h"
mariob 4:dbbf82c966c2 7
mariob 4:dbbf82c966c2 8 #include <map>
mariob 4:dbbf82c966c2 9
mariob 4:dbbf82c966c2 10
mariob 4:dbbf82c966c2 11 #define INTERNAL_BUFFER_SIZE 100
mariob 4:dbbf82c966c2 12
mariob 4:dbbf82c966c2 13
mariob 4:dbbf82c966c2 14 Thread* sender;
mariob 4:dbbf82c966c2 15 Thread* receiver;
mariob 4:dbbf82c966c2 16
mariob 4:dbbf82c966c2 17 ssWiChannel* channel = NULL;
mariob 4:dbbf82c966c2 18 Mutex mutexChannel;
mariob 4:dbbf82c966c2 19
mariob 4:dbbf82c966c2 20 std::map<int, ssWiPort> ports;
mariob 4:dbbf82c966c2 21
mariob 4:dbbf82c966c2 22 int TXRate;
mariob 4:dbbf82c966c2 23 int RXRate;
mariob 4:dbbf82c966c2 24
mariob 4:dbbf82c966c2 25
mariob 4:dbbf82c966c2 26 void threadSender (void const* arg);
mariob 4:dbbf82c966c2 27 void threadReceiver (void const* arg);
mariob 4:dbbf82c966c2 28
mariob 4:dbbf82c966c2 29
mariob 4:dbbf82c966c2 30 bool ssWi_init (ssWiChannel* c, int rateTX, int rateRX)
mariob 4:dbbf82c966c2 31 {
mariob 4:dbbf82c966c2 32 if (channel!=NULL)
mariob 4:dbbf82c966c2 33 return false;
mariob 8:354a0e3087c1 34 TXRate = 1000.0/rateTX;
mariob 8:354a0e3087c1 35 RXRate = 1000.0/rateRX;
mariob 4:dbbf82c966c2 36 mutexChannel.lock();
mariob 4:dbbf82c966c2 37 channel = c;
mariob 4:dbbf82c966c2 38 sender = new Thread(threadSender);
mariob 4:dbbf82c966c2 39 receiver = new Thread(threadReceiver);
mariob 4:dbbf82c966c2 40 mutexChannel.unlock();
mariob 4:dbbf82c966c2 41 return true;
mariob 4:dbbf82c966c2 42 }
mariob 4:dbbf82c966c2 43
mariob 4:dbbf82c966c2 44 bool ssWi_isActive (PortID port)
mariob 4:dbbf82c966c2 45 {
mariob 4:dbbf82c966c2 46 return channel!=NULL && ports.find(port)!=ports.end();
mariob 4:dbbf82c966c2 47 }
mariob 4:dbbf82c966c2 48
mariob 4:dbbf82c966c2 49 bool ssWi_setPort (PortID port)
mariob 4:dbbf82c966c2 50 {
mariob 4:dbbf82c966c2 51 if (channel==NULL)
mariob 4:dbbf82c966c2 52 return false;
mariob 4:dbbf82c966c2 53 ports[port];
mariob 4:dbbf82c966c2 54 return true;
mariob 4:dbbf82c966c2 55 }
mariob 4:dbbf82c966c2 56
mariob 4:dbbf82c966c2 57 bool ssWi_unsetPort (PortID port)
mariob 4:dbbf82c966c2 58 {
mariob 4:dbbf82c966c2 59 if (!ssWi_isActive(port))
mariob 4:dbbf82c966c2 60 return false;
mariob 4:dbbf82c966c2 61 ports.erase(port);
mariob 4:dbbf82c966c2 62 return true;
mariob 4:dbbf82c966c2 63 }
mariob 4:dbbf82c966c2 64
mariob 9:b5b5d0533fa6 65
mariob 4:dbbf82c966c2 66 void threadSender (void const* arg)
mariob 4:dbbf82c966c2 67 {
mariob 9:b5b5d0533fa6 68 char buffer[50];
mariob 4:dbbf82c966c2 69 while(true) {
mariob 9:b5b5d0533fa6 70 int n = 3;
mariob 9:b5b5d0533fa6 71 int numFrames = 0;
mariob 9:b5b5d0533fa6 72 for (std::map<int, ssWiPort>::iterator it = ports.begin(); it!=ports.end(); it++) {
mariob 4:dbbf82c966c2 73 if ((*it).second.isModified()) {
mariob 4:dbbf82c966c2 74 buffer[n++] = (*it).first;
mariob 4:dbbf82c966c2 75 PortValue tmp = (*it).second.getTXValue();
mariob 5:0b0ca40aeb81 76 memcpy(&buffer[n], &tmp, sizeof(PortValue));
mariob 5:0b0ca40aeb81 77 n += sizeof(PortValue);
mariob 9:b5b5d0533fa6 78 //printf("Write[%d] = %d\n\r", (*it).first, tmp);
mariob 9:b5b5d0533fa6 79 numFrames++;
mariob 4:dbbf82c966c2 80 }
mariob 4:dbbf82c966c2 81 }
mariob 9:b5b5d0533fa6 82 if (numFrames>0) {
mariob 9:b5b5d0533fa6 83 buffer[0] = START_0;
mariob 9:b5b5d0533fa6 84 buffer[1] = START_1;
mariob 9:b5b5d0533fa6 85 buffer[2] = START_2;
mariob 4:dbbf82c966c2 86 mutexChannel.lock();
mariob 4:dbbf82c966c2 87 channel->write(buffer, n);
mariob 4:dbbf82c966c2 88 mutexChannel.unlock();
mariob 9:b5b5d0533fa6 89
mariob 9:b5b5d0533fa6 90 printf("W(%d): ", n);
mariob 8:354a0e3087c1 91 for(int k=0; k<n; k++)
mariob 8:354a0e3087c1 92 printf("%d ", buffer[k]);
mariob 8:354a0e3087c1 93 printf("\n\r");
mariob 4:dbbf82c966c2 94 }
mariob 4:dbbf82c966c2 95 Thread::wait(TXRate);
mariob 4:dbbf82c966c2 96 }
mariob 4:dbbf82c966c2 97 }
mariob 4:dbbf82c966c2 98
mariob 4:dbbf82c966c2 99 void threadReceiver (void const* arg)
mariob 4:dbbf82c966c2 100 {
mariob 9:b5b5d0533fa6 101 char buffer[50];
mariob 9:b5b5d0533fa6 102
mariob 4:dbbf82c966c2 103 while(true) {
mariob 4:dbbf82c966c2 104 mutexChannel.lock();
mariob 5:0b0ca40aeb81 105 int n = channel->read(buffer);
mariob 4:dbbf82c966c2 106 mutexChannel.unlock();
mariob 9:b5b5d0533fa6 107
mariob 8:354a0e3087c1 108 if (n>0) {
mariob 9:b5b5d0533fa6 109 printf("R(%d): ", n);
mariob 8:354a0e3087c1 110 for(int k=0; k<n; k++)
mariob 8:354a0e3087c1 111 printf("%d ", buffer[k]);
mariob 8:354a0e3087c1 112 printf("\n\r");
mariob 8:354a0e3087c1 113 }
mariob 4:dbbf82c966c2 114
mariob 9:b5b5d0533fa6 115 bool alreadyIn = false;
mariob 9:b5b5d0533fa6 116 for (int i=0; i<(n-2);) {
mariob 9:b5b5d0533fa6 117 bool found = false;
mariob 9:b5b5d0533fa6 118 if (buffer[i]==START_0 && buffer[i+1]==START_1 && buffer[i+2]==START_2) {
mariob 9:b5b5d0533fa6 119 found = true;
mariob 9:b5b5d0533fa6 120 alreadyIn = true;
mariob 9:b5b5d0533fa6 121 i += 3;
mariob 4:dbbf82c966c2 122 }
mariob 9:b5b5d0533fa6 123 if ((found || alreadyIn) && (n-i)>=(sizeof(PortID)+sizeof(PortValue))) {
mariob 9:b5b5d0533fa6 124 PortID port = buffer[i++];
mariob 9:b5b5d0533fa6 125 PortValue value = 0;
mariob 9:b5b5d0533fa6 126 memcpy(&value, &buffer[i], sizeof(PortValue));
mariob 9:b5b5d0533fa6 127 printf("Read[%d] = %d\n\r", port, value);
mariob 9:b5b5d0533fa6 128 i += sizeof(PortValue);
mariob 9:b5b5d0533fa6 129 if (ports.find(port)!=ports.end())
mariob 9:b5b5d0533fa6 130 ports[port].setRXValue(value);
mariob 9:b5b5d0533fa6 131 } else
mariob 9:b5b5d0533fa6 132 i++;
mariob 4:dbbf82c966c2 133 }
mariob 4:dbbf82c966c2 134 Thread::wait(RXRate);
mariob 4:dbbf82c966c2 135 }
mariob 4:dbbf82c966c2 136 }