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


libcreate
Author(s): Jacob Perron
autogenerated on Sat May 8 2021 03:02:37