serial_query.cpp
Go to the documentation of this file.
1 #include <iostream>
2 
3 #include "create/serial_query.h"
4 #include "create/types.h"
5 
6 #define SENSORS_RESPONSE_LENGTH 20
7 
8 namespace create {
9 
10  SerialQuery::SerialQuery(boost::shared_ptr<Data> d) : Serial(d),
11  streamRecoveryTimer(io),
12  packetID(ID_BUMP_WHEELDROP),
13  packetByte(0),
14  packetData(0),
15  maxPacketID(ID_CAPACITY) {
16  }
17 
19  if (!started) {
21  started = true;
22  }
23  return true;
24  }
25 
27  static const uint8_t requestPacket[2] = { OC_SENSORS, ID_GROUP_0 };
28  // Prevents previous packet from corrupting next one
29  flushInput();
30  send(requestPacket, 2);
31  // Automatically resend request if no response is received
32  streamRecoveryTimer.expires_from_now(boost::posix_time::milliseconds(50));
33  streamRecoveryTimer.async_wait(boost::bind(&SerialQuery::restartSensorStream, this, _1));
34  }
35 
36  void SerialQuery::restartSensorStream(const boost::system::error_code& err) {
37  if (err != boost::asio::error::operation_aborted) {
38  if (packetID != ID_BUMP_WHEELDROP) {
40  }
42  }
43  }
44 
46  // Only works with POSIX support
47  tcflush(port.lowest_layer().native_handle(), TCIFLUSH);
48  }
49 
51  packetData |= (static_cast<uint16_t>(byteRead) << (8 * packetByte));
52 
53  if (packetByte > 0) {
54  --packetByte;
55  } else if (packetID < maxPacketID) {
56  // New packet
57  data->getPacket(packetID)->setDataToValidate(packetData);
58  packetData = 0;
59  ++packetID;
60  packetByte = data->getPacket(packetID)->nbytes - 1;
61  } else {
62  // Response finished
64  packetByte = 0;
65  packetData = 0;
68  }
69  }
70 } // namespace create
uint8_t byteRead
Definition: serial.h:62
boost::asio::serial_port port
Definition: serial.h:53
SerialQuery(boost::shared_ptr< Data > data)
void notifyDataReady()
Definition: serial.cpp:117
boost::shared_ptr< Data > data
Definition: serial.h:74
Definition: create.h:46
boost::asio::deadline_timer streamRecoveryTimer
Definition: serial_query.h:54
bool send(const uint8_t *bytes, const uint32_t numBytes)
Definition: serial.cpp:151
void restartSensorStream(const boost::system::error_code &err)
void processByte(uint8_t byteRead)
const uint8_t maxPacketID
Definition: serial_query.h:58
uint64_t corruptPackets
Definition: serial.h:76


libcreate
Author(s): Jacob Perron
autogenerated on Sat Jun 8 2019 17:58:17