AnimatLab  2
Test
AnimatSerial.h
1 /*
2  AnimatSerial.cpp - Library for interfacing with Animat serial stream
3  Copyright (c) 2015 David Cofer, NeuroRobotic Technologies. All right reserved.
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 
20 #ifndef AnimatSerial_h
21 #define AnimatSerial_h
22 
23 #include "HardwareSerial.h"
24 
25 #define HEADER_SIZE 5
26 #define DATA_SIZE 6
27 #define FOOTER_SIZE 1
28 
29 #define MAX_ANIMAT_BUFFER 128
30 
31 #define PACKET_INFO_SIZE 6 //Size of the header, packet size, message id, and checksum in bytes
32 #define START_MESSAGE_INFO_BYTE 5
33 #define DATA_SIZE 6
34 
36 {
37 public:
38  union id_tag {
39  unsigned char bval[2];
40  unsigned int ival;
41  } id;
42 
43  union value_tag {
44  unsigned char bval[4];
45  float fval;
46  } value;
47 
48  //unsigned int m_id;
49  //float m_value;
50 
51  AnimatData() {
52  id.ival = 0;
53  value.fval = 0;
54  }
55 };
56 
57 /* the Commander will send out a frame at about 30hz, this class helps decipher the output. */
59 {
60  public:
61  AnimatSerial();
62  AnimatSerial(HardwareSerial *ss, unsigned int inTotal, unsigned int outTotal);
63  ~AnimatSerial();
64 
65  void begin(unsigned long baud);
66  int readMsgs(); // must be called regularly to clean out Serial buffer
67  void writeMsgs();
68  void writeAllMsgs();
69  void writeResendMsg();
70 
71  bool isChanged();
72  bool isChanged(unsigned int id);
73  bool getData(unsigned int index, AnimatData &data);
74  bool addData(unsigned int id, float val);
75 
76  unsigned int getInDataTotal() {return inDataTotal;}
77  unsigned int getOutDataTotal() {return outDataTotal;}
78 
79  void clearChanged();
80 
81  private:
82 
83  void setInDataValue(int id, float val);
84 
85  void clearInData();
86  void clearOutData();
87 
88  AnimatData *inData;
89  AnimatData *outData;
90  bool *changed;
91  bool dataChanged;
92 
93  unsigned int inDataTotal;
94  unsigned int outDataTotal;
95 
96  unsigned int inDataCount;
97  unsigned int outDataCount;
98 
99  // internal variables used for reading messages
100  unsigned char vals[MAX_ANIMAT_BUFFER]; // temporary values, moved after we confirm checksum
101  int index; // -1 = waiting for new packet
102  int checksum;
103  unsigned char status;
104 
105  int messageID;
106  int packetSize;
107 
108  union size_tag {
109  unsigned char bval[2];
110  unsigned int ival;
111  } size;
112 
113  union id_tag {
114  unsigned char bval[2];
115  unsigned int ival;
116  } id;
117 
118  union value_tag {
119  unsigned char bval[4];
120  float fval;
121  } value;
122 
123  HardwareSerial *stream;
124 };
125 
126 #endif