ric_interface.cpp
Go to the documentation of this file.
1 
3 
4 
5 namespace ric_interface
6 {
8  {
9 
10  }
11 
12  /* open connection to serial port */
13  /* if conncetion fails, exception will be thrown */
14  void RicInterface::connect(std::string port)
15  {
16  comm_.connect(port, 500000);
17  }
18 
20  {
22  }
23 
25  {
26  sendKeepAlive();
29  }
30 
32  {
35  {
36  if (got_keepalive_) //connected
37  {
38  is_board_alive_ = true;
39  got_keepalive_ = false;
40  //fprintf(stderr,"board alive ! \n");
41  }
42  else
43  {
44  is_board_alive_ = false;
45  //fprintf(stderr,"board dead ! \n");
46  }
48  }
49  }
50 
52  {
53  int pkg_type = comm_.read(pkg_buff_);
54  if (pkg_type > 0)
55  {
56  //fprintf(stderr, "pkg_type: %d\n", pkg_type);
57  switch (pkg_type)
58  {
59  case (uint8_t) protocol::Type::KEEP_ALIVE:
60  {
61  protocol::keepalive ka_pkg;
63  if (ka_pkg.type == (uint8_t)protocol::Type::KEEP_ALIVE)
64  {
65  //fprintf(stderr, "got keep alive\n");
66  got_keepalive_ = true;
67  }
68  break;
69  }
70  case (uint8_t) protocol::Type::LOGGER:
71  {
72  protocol::logger logger_pkg;
74  if (logger_pkg.type == (uint8_t)protocol::Type::LOGGER)
75  {
76  sensors_state_.logger = logger_pkg;
77  //fprintf(stderr, "logger msg: %s, code: %d\n", logger_pkg.msg, logger_pkg.value);
78  }
79 
80  break;
81  }
83  {
84  protocol::ultrasonic ultrasonic_pkg;
85  Communicator::fromBytes(pkg_buff_, sizeof(protocol::ultrasonic), ultrasonic_pkg);
86  if (ultrasonic_pkg.type == (uint8_t)protocol::Type::ULTRASONIC)
87  {
88  sensors_state_.ultrasonic = ultrasonic_pkg;
89  //fprintf(stderr, "ultrasonic: %d\n", ultrasonic_pkg.distance_mm);
90  }
91  break;
92  }
93  case (int)protocol::Type::IMU:
94  {
95  protocol::imu imu_pkg;
97  if (imu_pkg.type == (uint8_t)protocol::Type::IMU)
98  {
99  sensors_state_.imu = imu_pkg;
100  /*fprintf(stderr, "imu: roll: %f, pitch: %f, yaw: %f \n", sensors_state_.imu.roll_rad * 180 / M_PI,
101  sensors_state_.imu.pitch_rad * 180 / M_PI,
102  sensors_state_.imu.yaw_rad * 180 / M_PI);*/
103  }
104  break;
105  }
106  case (int)protocol::Type::LASER:
107  {
108  protocol::laser laser_pkg;
110  if (laser_pkg.type == (uint8_t)protocol::Type::LASER)
111  {
112  sensors_state_.laser = laser_pkg;
113  //fprintf(stderr, "laser dist: %d\n", sensors_state_.laser.distance_mm);
114  }
115  break;
116  }
117  case (int)protocol::Type::GPS:
118  {
119  protocol::gps gps_pkg;
121  if (gps_pkg.type == (uint8_t)protocol::Type::GPS)
122  {
123  sensors_state_.gps = gps_pkg;
124  //fprintf(stderr,"gps lat: %f, lon: %f\n", sensors_state_.gps.lat, sensors_state_.gps.lon);
125  }
126  break;
127  }
129  {
130  protocol::emergency_alarm emrg_pkg;
132  if (emrg_pkg.type == (uint8_t)protocol::Type::EMERGENCY_ALARM)
133  {
134  sensors_state_.emrgcy_alarm = emrg_pkg;
135  //fprintf(stderr,"alarm state: %i\n", sensors_state_.emrgcy_alarm.is_on);
136  }
137  break;
138  }
139  }
140  clearBuffer();
141  }
142  }
143 
145  {
148  {
149  //puts("sending ka");
150  protocol::keepalive ka_pkg;
151  comm_.write(ka_pkg, sizeof(protocol::keepalive));
153  }
154  }
155 
156  void RicInterface::writeCmd(const protocol::actuator &actu_pkg, size_t size, protocol::Type type)
157  {
158  comm_.write(actu_pkg, size);
159  }
160 }
byte pkg_buff_[protocol::MAX_PKG_SIZE]
Definition: ric_interface.h:29
void reset()
Definition: timer.cpp:7
const uint16_t MAX_PKG_SIZE
Definition: protocol.h:22
bool write(const protocol::package &pkg, size_t pkg_size)
Definition: communicator.h:102
protocol::ultrasonic ultrasonic
Definition: sensors_state.h:15
void connect(std::string port)
void connect(std::string port, int baudrate)
Definition: communicator.h:32
void startTimer(int micro_secs)
Definition: timer.cpp:26
bool isFinished()
Definition: timer.cpp:36
protocol::emergency_alarm emrgcy_alarm
Definition: sensors_state.h:19
static void fromBytes(byte buff[], size_t pkg_size, protocol::package &pkg)
Definition: communicator.h:92
void writeCmd(const protocol::actuator &actu_pkg, size_t size, protocol::Type type)


ric_interface
Author(s):
autogenerated on Wed Jan 3 2018 03:48:20