-
Notifications
You must be signed in to change notification settings - Fork 1
/
btinterface.h
222 lines (184 loc) · 6.56 KB
/
btinterface.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/***********************************************************************
Copyright 2011 Carnegie Mellon University and Intel Corporation
Author: Mike Vande Weghe <vandeweg@cmu.edu>
This file is part of owd.
owd is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at your
option) any later version.
owd is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************/
#include <CANbus.hh>
#include <CANdefs.hh>
CANbus *btinterface_bus = NULL;
#define mbxID (0)
#define BASE_ID (0)
#define ADDR2NODE(x) ((((x) >> 5) & 0x001F) - BASE_ID)
#define NODE2ADDR(x) (((mbxID + BASE_ID) << 5) | ((x) + BASE_ID))
#define GROUPID(n) (((mbxID + BASE_ID) << 5) | (0x0400 + (n)))
#ifndef FALSE
#define FALSE (0)
#endif
#ifndef TRUE
#define TRUE (1)
#endif
#define MODE_IDLE 0
#define MODE_TORQUE 2
#define MODE_PID 3
#define MODE_VELOCITY 4
#define MODE_TRAPEZOID 5
enum {
ROLE_TATER,
ROLE_GIMBALS,
ROLE_SAFETY,
ROLE_WRAPTOR,
ROLE_TRIGGER,
ROLE_BHAND,
ROLE_FORCE
};
long jointPosition[32];
void setPropertySlow(int b, int id, int prop, bool verify, long value) {
if (! btinterface_bus) {
throw "Must allocate a CANbus object for btinterface_bus pointer";
}
btinterface_bus->set_property_rt(id,prop,value,verify,15000);
usleep(1000);
}
int canReadMsg(int bus, int *id, int *len, unsigned char *data, int block) {
if (! btinterface_bus) {
throw "Must allocate a CANbus object for btinterface_bus pointer";
}
int32_t usecs;
if (block) {
usecs=10000000;
} else {
usecs=2000;
}
return btinterface_bus->read_rt(id,data,len,usecs);
}
int canSendMsg(int bus, int id, int len, unsigned char *data, int block) {
if (! btinterface_bus) {
throw "Must allocate a CANbus object for btinterface_bus pointer";
}
int32_t usecs;
if (block) {
usecs=10000000;
} else {
usecs=2000;
}
return btinterface_bus->send_rt(id,data,len,usecs);
}
void mvprintw(int line, int pos, const char *format, ...) {
printf("\n%s",format);
}
int wakePuck(int bus, int who)
{
setPropertySlow(bus, who, 5, FALSE, STATUS_READY); // Must use '5' for STAT
usleep(1000000); // Wait 500ms for puck to initialize
return(0);
}
void initPropertyDefs(int firmwareVersion){
btinterface_bus->initPropertyDefs(firmwareVersion);
}
int parseMessage(
/* Input */
int id /** The message ID */,
int len /** The data payload length */,
unsigned char *messageData /** Pointer to the message data payload */,
/* Output */
int *node /** The controller node ID of the received message */,
int *property /** The property this message applies to */,
long *value /** The value of the property being processed */)
{
int i;
int dataHeader;
*node = ADDR2NODE(id);
if (*node == -1)
fprintf(stderr,"msgID:%x ",id);
dataHeader = ((messageData[0] >> 6) & 0x0002) | ((id & 0x041F) == 0x0403) | ((id & 0x41F) == 0x0407);
//messageData[0] &= 0x7F;
//fprintf(stderr,"Entering parsemessage");
switch (dataHeader)
{
case 3: /* Data is a packed 22-bit position, SET */
*value = 0x00000000;
*value |= ( (long)messageData[0] << 16) & 0x003F0000;
*value |= ( (long)messageData[1] << 8 ) & 0x0000FF00;
*value |= ( (long)messageData[2] ) & 0x000000FF;
if (*value & 0x00200000) /* If negative */
*value |= 0xFFC00000; /* Sign-extend */
if((id & 0x041F) == 0x0403)
*property = P;
else
*property = JP;
jointPosition[*node] = 0;
if(len > 3){
jointPosition[*node] |= ( (long)messageData[3] << 16) & 0x003F0000;
jointPosition[*node] |= ( (long)messageData[4] << 8 ) & 0x0000FF00;
jointPosition[*node] |= ( (long)messageData[5] ) & 0x000000FF;
if (jointPosition[*node] & 0x00200000) /* If negative */
jointPosition[*node] |= 0xFFC00000; /* Sign-extend */
}
//fprintf(stderr,"Received packed set property: %d from node: %d value:%d",*property,*node,*value);
break;
case 2: /* Data is normal, SET */
*property = messageData[0] & 0x7F;
//fprintf(stderr, "Received property: %d", *property);
/* Store the value, second byte of message is zero (for DSP word alignment) */
*value = messageData[len-1] & 0x80 ? -1L : 0;
for (i = len-1; i >= 2; i--)
*value = *value << 8 | messageData[i];
//fprintf(stderr, "Received normal set property: %d from node: %d value:%d", *property, *node, *value);
//fprintf(stderr,"parsemessage after %d",value);
break;
case 0: /* Assume firmware request (GET) */
*property = -(messageData[0] & 0x7F); /* A negative (or zero) property means GET */
*value = 0;
//fprintf(stderr, "Received normal get property: %d from node: %d value:%d", *property, *node, *value);
break;
default:
fprintf(stderr, "<Illegal Message Header> %d\n", dataHeader);
return(1);
}
//if (*property != 8) fprintf(stderr,"Value in parsemessage is: %d",*value);
return (0);
}
int getProperty(int bus, int id, int property, long *reply)
{
int err;
unsigned char data[8];
int len_in;
int id_in;
int property_in;
// Compile the packet
data[0] = (unsigned char)property;
// Send the packet
err = canSendMsg(bus, NODE2ADDR(id), 1, data, TRUE);
if (err) {
fprintf(stderr,"getProperty(): canSendMsg error = %d\n", err);
return (1);
}
// Wait for 1 reply
err = canReadMsg(bus, &id_in, &len_in, data, TRUE);
if(!err) {
// Parse the reply
err = parseMessage(id_in, len_in, data, &id_in, &property_in, reply);
// Check that the id and property match
if((id == id_in) && (property == property_in)) {
return(0);
} else {
fprintf(stderr, "getProperty(): returned id or property do not match");
fprintf(stderr, "asked for %d from %d but got %d from %d\n",
property, id, property_in, id_in);
return(1);
}
} else {
fprintf(stderr,"getProperty(): canReadMsg error = %d\n", err);
return(1);
}
}