Hekateros  3.4.3
RoadNarrows Robotics Robot Arm Project
hekateros::HekUno Class Reference

Public Member Functions

 HekUno ()
 Default constructor.
 
virtual ~HekUno ()
 Destructor.
 
virtual int open (uint_t uHekHwVer, const std::string &dev=HekDevArduino, int baud=HekBaudRateArduino)
 Open all interfaces monitoring hardware. More...
 
virtual int close ()
 Close all interfaces to monitoring hardware. More...
 
virtual int scan ()
 Scan and initialize hardware. More...
 
virtual int cmdReadFwVersion (int &ver)
 Command to read firmware version. More...
 
virtual byte_t cmdReadLimits ()
 Command to read limit bit state. More...
 
virtual byte_t cmdReadAux ()
 Command to read auxilliary bit state. More...
 
virtual int cmdReadPin (int id)
 Command to read one I/O pin. More...
 
virtual int cmdWritePin (int id, int val)
 Command to write value to an I/O pin. More...
 
virtual int cmdConfigPin (int id, char dir)
 Command to configure direction of an I/O pin. More...
 
virtual int cmdSetAlarmLED (int val)
 Command to set Alarm LED. More...
 
virtual int cmdSetStatusLED (int val)
 Command to set Status LED. More...
 
virtual int cmdSetHaltedState ()
 Command to set monitoring state to halted. More...
 
virtual int cmdTestInterface ()
 Test serial interface command. More...
 
virtual int cmdNull ()
 Null command. More...
 

Protected Member Functions

int sendCommand (char *buf, size_t nBytes, uint_t timeout=50000)
 Send command to Arduino. More...
 
int recvResponse (char buf[], size_t count, uint_t timeout=100000)
 Receive response from Arduino. More...
 
byte_t unpackHex (char buf[])
 Unpack hex characters to byte. More...
 

Protected Attributes

int m_fd
 open arduino file descriptor
 
uint_t m_uHekHwVer
 hekateros hardware version
 
int m_nFwVer
 arduino firmware version
 
int m_nFwOpState
 firmware operational state
 
int m_nFwSeqNum
 expected test sequence number modulo 256
 

Detailed Description

Definition at line 63 of file hekUno.h.

Member Function Documentation

int HekUno::close ( )
virtual

Close all interfaces to monitoring hardware.

Returns
On success, HEK_OK is returned.
On error, the appropriate < 0 negated Hekateros Error Code is returned.

Definition at line 103 of file hekUno.cxx.

104 {
105  if( m_fd >= 0 )
106  {
107  ::close(m_fd);
108  m_fd = -1;
109  LOGDIAG3("Closed Arduino compatible device.");
110  }
111  m_uHekHwVer = 0;
112  m_nFwVer = 0;
113 }
int m_fd
open arduino file descriptor
Definition: hekUno.h:199
virtual int close()
Close all interfaces to monitoring hardware.
Definition: hekUno.cxx:103
uint_t m_uHekHwVer
hekateros hardware version
Definition: hekUno.h:200
int m_nFwVer
arduino firmware version
Definition: hekUno.h:201
int HekUno::cmdConfigPin ( int  id,
char  dir 
)
virtual

Command to configure direction of an I/O pin.

Parameters
idPin identifier.
dirPin direction 'i' or 'o'.
Returns
On success, HEK_OK is returned.
On error, the appropriate < 0 negated Hekateros Error Code is returned.

Definition at line 248 of file hekUno.cxx.

249 {
250  char cmd[16];
251  char rsp[16];
252  int c;
253  int n;
254 
255  if( dir == 'i' )
256  {
257  c = 0;
258  }
259  else if( dir == 'o' )
260  {
261  c = 1;
262  }
263  else
264  {
265  return -HEK_ECODE_BAD_VAL;
266  }
267 
268  sprintf(cmd, "!c %d %d\r", id, c);
269  sendCommand(cmd, strlen(cmd));
270 
271  n = recvResponse(rsp, sizeof(rsp));
272 
273  if( (n < 1) || (rsp[0] != '@') )
274  {
275  return -HEK_ECODE_NO_EXEC;
276  }
277  else
278  {
279  return HEK_OK;
280  }
281 }
int sendCommand(char *buf, size_t nBytes, uint_t timeout=50000)
Send command to Arduino.
Definition: hekUno.cxx:418
static const int HEK_OK
not an error, success
Definition: hekateros.h:70
static const int HEK_ECODE_NO_EXEC
cannot execute error
Definition: hekateros.h:84
int recvResponse(char buf[], size_t count, uint_t timeout=100000)
Receive response from Arduino.
Definition: hekUno.cxx:425
static const int HEK_ECODE_BAD_VAL
bad value general error
Definition: hekateros.h:75
int HekUno::cmdNull ( )
virtual

Null command.

Returns
On success, HEK_OK is returned.
On error, the appropriate < 0 negated Hekateros Error Code is returned.

Definition at line 405 of file hekUno.cxx.

406 {
407  char cmd[] = "\r";
408  char rsp[16];
409  int n;
410 
411  sendCommand(cmd, strlen(cmd));
412 
413  n = recvResponse(rsp, sizeof(rsp), 2000000);
414 
415  return HEK_OK;
416 }
int sendCommand(char *buf, size_t nBytes, uint_t timeout=50000)
Send command to Arduino.
Definition: hekUno.cxx:418
static const int HEK_OK
not an error, success
Definition: hekateros.h:70
int recvResponse(char buf[], size_t count, uint_t timeout=100000)
Receive response from Arduino.
Definition: hekUno.cxx:425
byte_t HekUno::cmdReadAux ( )
virtual

Command to read auxilliary bit state.

Auxillary bits are usually End Effector GPIO, but allows room for additional bits.

Returns
Returns auxilliary switches state as a bit packed byte.

Definition at line 188 of file hekUno.cxx.

189 {
190  char cmd[] = "!e\r";
191  char rsp[16];
192  int n;
193  byte_t val = 0;
194 
195  sendCommand(cmd, strlen(cmd));
196 
197  n = recvResponse(rsp, sizeof(rsp));
198 
199  if( n > 2 )
200  {
201  val = unpackHex(rsp+1);
202  }
203 
204  return val;
205 }
int sendCommand(char *buf, size_t nBytes, uint_t timeout=50000)
Send command to Arduino.
Definition: hekUno.cxx:418
int recvResponse(char buf[], size_t count, uint_t timeout=100000)
Receive response from Arduino.
Definition: hekUno.cxx:425
byte_t unpackHex(char buf[])
Unpack hex characters to byte.
Definition: hekUno.cxx:445
int HekUno::cmdReadFwVersion ( int &  ver)
virtual

Command to read firmware version.

param [out] ver Version number.

Returns
On success, HEK_OK is returned.
On error, the appropriate < 0 negated Hekateros Error Code is returned.

Definition at line 138 of file hekUno.cxx.

139 {
140  char cmd[] = "!v\r";
141  char rsp[16];
142  int n;
143 
144  ver = 0;
145 
146  sendCommand(cmd, strlen(cmd));
147 
148  n = recvResponse(rsp, sizeof(rsp), 200000);
149 
150  if( n > 1 )
151  {
152  ver = atoi(rsp+1);
153  return HEK_OK;
154  }
155  else
156  {
157  LOGERROR("Failed to read firmware version.");
158  return -HEK_ECODE_NO_EXEC;
159  }
160 }
int sendCommand(char *buf, size_t nBytes, uint_t timeout=50000)
Send command to Arduino.
Definition: hekUno.cxx:418
static const int HEK_OK
not an error, success
Definition: hekateros.h:70
static const int HEK_ECODE_NO_EXEC
cannot execute error
Definition: hekateros.h:84
int recvResponse(char buf[], size_t count, uint_t timeout=100000)
Receive response from Arduino.
Definition: hekUno.cxx:425
byte_t HekUno::cmdReadLimits ( )
virtual

Command to read limit bit state.

Returns
Returns limit switches state as a bit packed byte.

Definition at line 162 of file hekUno.cxx.

163 {
164  char cmd[] = "!o\r";
165  char rsp[16];
166  int n;
167  byte_t val = 0;
168 
169  sendCommand(cmd, strlen(cmd));
170 
171  n = recvResponse(rsp, sizeof(rsp));
172 
173  if( n > 2 )
174  {
175  val = unpackHex(rsp+1);
176  }
177  else
178  {
179  LOGERROR("Failed to read optical limits.");
180  return -HEK_ECODE_NO_EXEC;
181  }
182 
183 //fprintf(stderr, "received %x from uno.\n", val);
184 
185  return val;
186 }
int sendCommand(char *buf, size_t nBytes, uint_t timeout=50000)
Send command to Arduino.
Definition: hekUno.cxx:418
static const int HEK_ECODE_NO_EXEC
cannot execute error
Definition: hekateros.h:84
int recvResponse(char buf[], size_t count, uint_t timeout=100000)
Receive response from Arduino.
Definition: hekUno.cxx:425
byte_t unpackHex(char buf[])
Unpack hex characters to byte.
Definition: hekUno.cxx:445
int HekUno::cmdReadPin ( int  id)
virtual

Command to read one I/O pin.

Parameters
idPin identifier.
Returns
Return pin state 0 or 1.

Definition at line 207 of file hekUno.cxx.

208 {
209  char cmd[16];
210  char rsp[16];
211  int n;
212  int val;
213 
214  sprintf(cmd, "!r %d\r", id);
215  sendCommand(cmd, strlen(cmd));
216 
217  n = recvResponse(rsp, sizeof(rsp));
218 
219  if( n > 1 )
220  {
221  val = atoi(rsp);
222  }
223 
224  return val;
225 }
int sendCommand(char *buf, size_t nBytes, uint_t timeout=50000)
Send command to Arduino.
Definition: hekUno.cxx:418
int recvResponse(char buf[], size_t count, uint_t timeout=100000)
Receive response from Arduino.
Definition: hekUno.cxx:425
int HekUno::cmdSetAlarmLED ( int  val)
virtual

Command to set Alarm LED.

Parameters
valLED 0==off or 1==on value.
Returns
On success, HEK_OK is returned.
On error, the appropriate < 0 negated Hekateros Error Code is returned.

Definition at line 283 of file hekUno.cxx.

References HEK_VERSION.

284 {
285  char cmd[16];
286  char rsp[16];
287  int n;
288 
289  if( m_uHekHwVer < HEK_VERSION(1, 2, 0) )
290  {
291  return cmdWritePin(5, val);
292  }
293 
294  sprintf(cmd, "!l 1 %d\r", val? 1: 0);
295  sendCommand(cmd, strlen(cmd));
296 
297  n = recvResponse(rsp, sizeof(rsp));
298 
299  if( (n < 1) || (rsp[0] != '@') )
300  {
301  return -HEK_ECODE_NO_EXEC;
302  }
303  else
304  {
305  return HEK_OK;
306  }
307 }
int sendCommand(char *buf, size_t nBytes, uint_t timeout=50000)
Send command to Arduino.
Definition: hekUno.cxx:418
static const int HEK_OK
not an error, success
Definition: hekateros.h:70
static const int HEK_ECODE_NO_EXEC
cannot execute error
Definition: hekateros.h:84
virtual int cmdWritePin(int id, int val)
Command to write value to an I/O pin.
Definition: hekUno.cxx:227
uint_t m_uHekHwVer
hekateros hardware version
Definition: hekUno.h:200
int recvResponse(char buf[], size_t count, uint_t timeout=100000)
Receive response from Arduino.
Definition: hekUno.cxx:425
#define HEK_VERSION(major, minor, revision)
Convert version triplet to integer equivalent.
Definition: hekateros.h:185
int HekUno::cmdSetHaltedState ( )
virtual

Command to set monitoring state to halted.

Returns
On success, HEK_OK is returned.
On error, the appropriate < 0 negated Hekateros Error Code is returned.

Definition at line 335 of file hekUno.cxx.

References HEK_VERSION.

336 {
337  char cmd[] = "!h\r";
338  char rsp[16];
339  int n;
340 
341  if( m_uHekHwVer < HEK_VERSION(1, 2, 0) )
342  {
343  return HEK_OK;
344  }
345 
346  sendCommand(cmd, strlen(cmd));
347 
348  n = recvResponse(rsp, sizeof(rsp));
349 
350  if( (n < 1) || (rsp[0] != '@') )
351  {
352  return -HEK_ECODE_NO_EXEC;
353  }
354  else
355  {
356  return HEK_OK;
357  }
358 }
int sendCommand(char *buf, size_t nBytes, uint_t timeout=50000)
Send command to Arduino.
Definition: hekUno.cxx:418
static const int HEK_OK
not an error, success
Definition: hekateros.h:70
static const int HEK_ECODE_NO_EXEC
cannot execute error
Definition: hekateros.h:84
uint_t m_uHekHwVer
hekateros hardware version
Definition: hekUno.h:200
int recvResponse(char buf[], size_t count, uint_t timeout=100000)
Receive response from Arduino.
Definition: hekUno.cxx:425
#define HEK_VERSION(major, minor, revision)
Convert version triplet to integer equivalent.
Definition: hekateros.h:185
int HekUno::cmdSetStatusLED ( int  val)
virtual

Command to set Status LED.

Parameters
valLED 0==off or 1==on value.
Returns
On success, HEK_OK is returned.
On error, the appropriate < 0 negated Hekateros Error Code is returned.

Definition at line 309 of file hekUno.cxx.

References HEK_VERSION.

310 {
311  char cmd[16];
312  char rsp[16];
313  int n;
314 
315  if( m_uHekHwVer < HEK_VERSION(1, 2, 0) )
316  {
317  return HEK_OK;
318  }
319 
320  sprintf(cmd, "!l 2 %d\r", val? 1: 0);
321  sendCommand(cmd, strlen(cmd));
322 
323  n = recvResponse(rsp, sizeof(rsp));
324 
325  if( (n < 1) || (rsp[0] != '@') )
326  {
327  return -HEK_ECODE_NO_EXEC;
328  }
329  else
330  {
331  return HEK_OK;
332  }
333 }
int sendCommand(char *buf, size_t nBytes, uint_t timeout=50000)
Send command to Arduino.
Definition: hekUno.cxx:418
static const int HEK_OK
not an error, success
Definition: hekateros.h:70
static const int HEK_ECODE_NO_EXEC
cannot execute error
Definition: hekateros.h:84
uint_t m_uHekHwVer
hekateros hardware version
Definition: hekUno.h:200
int recvResponse(char buf[], size_t count, uint_t timeout=100000)
Receive response from Arduino.
Definition: hekUno.cxx:425
#define HEK_VERSION(major, minor, revision)
Convert version triplet to integer equivalent.
Definition: hekateros.h:185
int HekUno::cmdTestInterface ( )
virtual

Test serial interface command.

Returns
On success, HEK_OK is returned.
On error, the appropriate < 0 negated Hekateros Error Code is returned.

Definition at line 360 of file hekUno.cxx.

361 {
362  char cmd[] = "!t\r";
363  char rsp[16];
364  int n;
365  int nOpState;
366  int nSeqNum;
367 
368  if( m_nFwVer >= 3 )
369  {
370  sendCommand(cmd, strlen(cmd));
371 
372  n = recvResponse(rsp, sizeof(rsp));
373 
374  if( (n < 1) || (rsp[0] != '@') )
375  {
376  return -HEK_ECODE_NO_EXEC;
377  }
378 
379  n = sscanf(&rsp[1], "%d %d", &nOpState, &nSeqNum);
380 
381  if( n != 2 )
382  {
383  LOGERROR("Failed test of interface: bad response.");
384  return -HEK_ECODE_NO_EXEC;
385  }
386 
387  if( nOpState != m_nFwOpState )
388  {
389  fprintf(stderr, "ARDUINO TEST: New OpState=%d\n", m_nFwOpState);
390  }
391 
392  if( nSeqNum != m_nFwSeqNum )
393  {
394  fprintf(stderr, "ARDUINO TEST: Lost sync: Sequence number received=%d, "
395  "expected=%d\n", nSeqNum, m_nFwSeqNum);
396  }
397 
398  m_nFwOpState = nOpState;
399  m_nFwSeqNum = (nSeqNum + 1) & 0xff;
400  }
401 
402  return HEK_OK;
403 }
int sendCommand(char *buf, size_t nBytes, uint_t timeout=50000)
Send command to Arduino.
Definition: hekUno.cxx:418
static const int HEK_OK
not an error, success
Definition: hekateros.h:70
static const int HEK_ECODE_NO_EXEC
cannot execute error
Definition: hekateros.h:84
int m_nFwSeqNum
expected test sequence number modulo 256
Definition: hekUno.h:205
int recvResponse(char buf[], size_t count, uint_t timeout=100000)
Receive response from Arduino.
Definition: hekUno.cxx:425
int m_nFwOpState
firmware operational state
Definition: hekUno.h:204
int m_nFwVer
arduino firmware version
Definition: hekUno.h:201
int HekUno::cmdWritePin ( int  id,
int  val 
)
virtual

Command to write value to an I/O pin.

Parameters
idPin identifier.
valPin state 0 or 1.
Returns
On success, HEK_OK is returned.
On error, the appropriate < 0 negated Hekateros Error Code is returned.

Definition at line 227 of file hekUno.cxx.

228 {
229  char cmd[16];
230  char rsp[16];
231  int n;
232 
233  sprintf(cmd, "!w %d %d\r", id, val? 1: 0);
234  sendCommand(cmd, strlen(cmd));
235 
236  n = recvResponse(rsp, sizeof(rsp));
237 
238  if( (n < 1) || (rsp[0] != '@') )
239  {
240  return -HEK_ECODE_NO_EXEC;
241  }
242  else
243  {
244  return HEK_OK;
245  }
246 }
int sendCommand(char *buf, size_t nBytes, uint_t timeout=50000)
Send command to Arduino.
Definition: hekUno.cxx:418
static const int HEK_OK
not an error, success
Definition: hekateros.h:70
static const int HEK_ECODE_NO_EXEC
cannot execute error
Definition: hekateros.h:84
int recvResponse(char buf[], size_t count, uint_t timeout=100000)
Receive response from Arduino.
Definition: hekUno.cxx:425
int HekUno::open ( uint_t  uHekHwVer,
const std::string &  dev = HekDevArduino,
int  baud = HekBaudRateArduino 
)
virtual

Open all interfaces monitoring hardware.

Parameters
uHekHwVer

Definition at line 81 of file hekUno.cxx.

82 {
83  m_uHekHwVer = uHekHwVer;
84 
85  if( (m_fd = SerDevOpen(dev.c_str(), baud, 8, 'N', 1, 0, 0) ) == -1 )
86  {
87  LOGERROR("Failed to open Arduino digital I/O board");
88  return -HEK_ECODE_SYS;
89  }
90 
91  SerDevFIFOInputFlush(m_fd);
92 
93  // flush any residuals
94  cmdNull();
95 
96  //SerDevFIFOInputFlush(m_fd);
97 
98  LOGDIAG3("Opened Arduino compatible device: %s@%d", dev.c_str(), baud);
99 
100  return HEK_OK;
101 }
int m_fd
open arduino file descriptor
Definition: hekUno.h:199
static const int HEK_OK
not an error, success
Definition: hekateros.h:70
virtual int cmdNull()
Null command.
Definition: hekUno.cxx:405
static const int HEK_ECODE_SYS
system (errno) error
Definition: hekateros.h:73
uint_t m_uHekHwVer
hekateros hardware version
Definition: hekUno.h:200
int HekUno::recvResponse ( char  buf[],
size_t  count,
uint_t  timeout = 100000 
)
protected

Receive response from Arduino.

Parameters
bufReceiving character buffer.
countMaximum number of bytes to read.
timeoutReceive timeout (usec).
Returns
On success, HEK_OK is returned.
On error, the appropriate < 0 negated Hekateros Error Code is returned.

Definition at line 425 of file hekUno.cxx.

426 {
427  char c;
428  ssize_t nBytes=0;
429 
430  // grab actual response
431  nBytes = SerDevReadLine(m_fd, buf, count, (char *)"\n\r", timeout);
432 
433 #ifdef MON_DEBUG
434  fprintf(stderr, "DBG - received(%zd): ", nBytes);
435  for(ssize_t i=0; i<nBytes; ++i)
436  {
437  fprintf(stderr, "%x ", buf[i]);
438  }
439  fprintf(stderr, "\n");
440 #endif // MON_DEBUG
441 
442  return (int)nBytes;
443 }
int m_fd
open arduino file descriptor
Definition: hekUno.h:199
int HekUno::scan ( )
virtual

Scan and initialize hardware.

Returns
On success, HEK_OK is returned.
On error, the appropriate < 0 negated Hekateros Error Code is returned.

Definition at line 115 of file hekUno.cxx.

116 {
117  LOGDIAG3("Scanning Arduino digital I/O board capabilities.");
118 
119  //
120  // Read firmware version. Do this a few times to flush out any residual
121  // characters in arduino command pipeline.
122  //
123  for(int i=0; i<3; ++i)
124  {
126  cmdReadLimits();
127  if( m_nFwVer != 0 )
128  {
129  break;
130  }
131  }
132 
133  LOGDIAG3("Arduino firmware version %d.", m_nFwVer);
134 
135  return HEK_OK;
136 }
virtual byte_t cmdReadLimits()
Command to read limit bit state.
Definition: hekUno.cxx:162
static const int HEK_OK
not an error, success
Definition: hekateros.h:70
virtual int cmdReadFwVersion(int &ver)
Command to read firmware version.
Definition: hekUno.cxx:138
int m_nFwVer
arduino firmware version
Definition: hekUno.h:201
int HekUno::sendCommand ( char *  buf,
size_t  nBytes,
uint_t  timeout = 50000 
)
protected

Send command to Arduino.

Parameters
bufBuffer of bytes.
nBytesNumber of bytes in buffer to write.
timeoutSend timeout (usec).
Returns
On success, HEK_OK is returned.
On error, the appropriate < 0 negated Hekateros Error Code is returned.

Definition at line 418 of file hekUno.cxx.

419 {
420  SerDevWrite(m_fd, (byte_t *)buf, nBytes, timeout);
421  SerDevFIFOOutputDrain(m_fd);
422  return HEK_OK;
423 }
int m_fd
open arduino file descriptor
Definition: hekUno.h:199
static const int HEK_OK
not an error, success
Definition: hekateros.h:70
byte_t HekUno::unpackHex ( char  buf[])
protected

Unpack hex characters to byte.

Format: hh

Parameters
bufBuffer of hex characters
Returns
Returns unpacked byte.

Definition at line 445 of file hekUno.cxx.

446 {
447  byte_t val = 0;
448 
449  //
450  // Parse 2 character hex.
451  //
452  for(int i=0; i<2; ++i)
453  {
454  val *= 16;
455  if(buf[i] >= '0' && buf[i] <='9')
456  {
457  val += buf[i] - '0';
458  }
459 
460  else if (buf[i] >= 'A' && buf[i] <= 'F')
461  {
462  val += buf[i] -'A'+10;
463  }
464 
465  else if (buf[i] >= 'a' && buf[i] <= 'f')
466  {
467  val += buf[i] -'a'+10;
468  }
469  }
470 
471  return val;
472 }

The documentation for this class was generated from the following files: