NASA Astrobee Robot Software  0.19.1
Flight software for the Astrobee robots operating inside the International Space Station.
speed_cam.h
Go to the documentation of this file.
1 /* Copyright (c) 2017, United States Government, as represented by the
2  * Administrator of the National Aeronautics and Space Administration.
3  *
4  * All rights reserved.
5  *
6  * The Astrobee platform is licensed under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations
16  * under the License.
17  */
18 
19 #ifndef SPEED_CAM_SPEED_CAM_H_
20 #define SPEED_CAM_SPEED_CAM_H_
21 
22 // Serial abstraction
23 #include <ff_serial/serial.h>
24 
25 // Mavlink protocol
26 #include <mavlink/v1.0/common/mavlink.h>
27 
28 // C++ STL includes
29 #include <functional>
30 #include <string>
31 #include <vector>
32 
33 // Conversions to SI units
34 #define MILLIMSS_TO_MSS (0.0010)
35 #define MILLIRADS_TO_RADS (0.0010)
36 
40 namespace speed_cam {
41 
42 // Callback types
43 typedef std::function<void(mavlink_raw_imu_t const&)> SpeedCamImuCallback;
44 typedef std::function<void(std::vector<uint8_t> const&, int32_t, int32_t)> SpeedCamCameraImageCallback;
45 typedef std::function<void(mavlink_optical_flow_t const&)> SpeedCamOpticalFlowCallback;
46 typedef std::function<void(mavlink_vision_speed_estimate_t const&)> SpeedCamSpeedCallback;
47 typedef std::function<void(mavlink_heartbeat_t const&)> SpeedCamStatusCallback;
48 typedef std::function<void(uint32_t)> SpeedCamVersionCallback;
49 typedef std::function<void(int32_t)> SpeedCamStateCallback;
50 
51 // Result for any speed camera action
53  RESULT_SUCCESS, // Everything happened well
54  RESULT_PORT_NOT_OPEN, // Serial port could not be opened
55  RESULT_PORT_WRITE_FAILURE // Couldn't write to the serial port
56 };
57 
58 // Bitmask for speedcam status
60  STATUS_NORMAL = 0x00,
63 };
64 
65 // Class to manage the speed camera
66 class SpeedCam {
67  public:
68  // Constructor
70  SpeedCamOpticalFlowCallback cb_optical_flow, SpeedCamSpeedCallback cb_speed,
71  SpeedCamStatusCallback cb_status, SpeedCamVersionCallback cb_version,
73 
74  // Initialize the serial port
75  SpeedCamResult Initialize(std::string const& port, uint32_t baud);
76 
77  // Get the operating state
78  void SetState(int32_t const& state);
79 
80  protected:
81  // Asynchronous callback with serial data
82  void ReadCallback(const uint8_t *buffer, size_t len);
83 
84  // Asynchronous callback for read timeout
85  void TimeoutCallback(void);
86 
87  private:
88  ff_serial::Serial serial_; // Serial port
89  SpeedCamImuCallback cb_imu_; // Imu data callback
90  SpeedCamCameraImageCallback cb_camera_image_; // Camera image callback
91  SpeedCamOpticalFlowCallback cb_optical_flow_; // Optical flow callback
92  SpeedCamSpeedCallback cb_speed_; // Twist callback
93  SpeedCamStatusCallback cb_status_; // System status callback
94  SpeedCamVersionCallback cb_version_; // Firmware version
95  SpeedCamStateCallback cb_state_; // State
96  uint8_t system_id_; // Source device
97  uint8_t comp_id_; // Sink device
98  size_t image_size_; // Image size
99  size_t image_packets_; // Image packets
100  int32_t image_payload_; // Payload size / transaction
101  int32_t image_width_; // Image width
102  int32_t image_height_; // Image height
103  std::vector<uint8_t> image_buffer_; // Image buffer
104  uint32_t sw_version_; // Firmware version
105 };
106 
107 } // namespace speed_cam
108 
109 #endif // SPEED_CAM_SPEED_CAM_H_
speed_cam::STATUS_NORMAL
@ STATUS_NORMAL
Definition: speed_cam.h:60
speed_cam::SpeedCamStateCallback
std::function< void(int32_t)> SpeedCamStateCallback
Definition: speed_cam.h:49
speed_cam::SpeedCamResult
SpeedCamResult
Definition: speed_cam.h:52
speed_cam::SpeedCamCameraImageCallback
std::function< void(std::vector< uint8_t > const &, int32_t, int32_t)> SpeedCamCameraImageCallback
Definition: speed_cam.h:44
speed_cam::RESULT_PORT_NOT_OPEN
@ RESULT_PORT_NOT_OPEN
Definition: speed_cam.h:54
speed_cam::SpeedCamSpeedCallback
std::function< void(mavlink_vision_speed_estimate_t const &)> SpeedCamSpeedCallback
Definition: speed_cam.h:46
speed_cam::SpeedCamStatus
SpeedCamStatus
Definition: speed_cam.h:59
speed_cam::SpeedCam::ReadCallback
void ReadCallback(const uint8_t *buffer, size_t len)
Definition: speed_cam.cc:71
speed_cam
Definition: speed_cam.h:40
speed_cam::SpeedCam::SpeedCam
SpeedCam(SpeedCamImuCallback cb_imu, SpeedCamCameraImageCallback cb_camera_image, SpeedCamOpticalFlowCallback cb_optical_flow, SpeedCamSpeedCallback cb_speed, SpeedCamStatusCallback cb_status, SpeedCamVersionCallback cb_version, SpeedCamStateCallback)
Definition: speed_cam.cc:24
speed_cam::SpeedCamImuCallback
std::function< void(mavlink_raw_imu_t const &)> SpeedCamImuCallback
Definition: speed_cam.h:43
ff_serial::Serial
Definition: serial.h:52
speed_cam::SpeedCam::TimeoutCallback
void TimeoutCallback(void)
Definition: speed_cam.cc:173
speed_cam::SpeedCam::Initialize
SpeedCamResult Initialize(std::string const &port, uint32_t baud)
Definition: speed_cam.cc:50
speed_cam::SpeedCam::SetState
void SetState(int32_t const &state)
Definition: speed_cam.cc:59
speed_cam::SpeedCamOpticalFlowCallback
std::function< void(mavlink_optical_flow_t const &)> SpeedCamOpticalFlowCallback
Definition: speed_cam.h:45
serial.h
speed_cam::SpeedCamStatusCallback
std::function< void(mavlink_heartbeat_t const &)> SpeedCamStatusCallback
Definition: speed_cam.h:47
speed_cam::STATUS_ANGULAR_SPEED_LIMIT_EXCEEDED
@ STATUS_ANGULAR_SPEED_LIMIT_EXCEEDED
Definition: speed_cam.h:62
speed_cam::STATUS_LINEAR_SPEED_LIMIT_EXCEEDED
@ STATUS_LINEAR_SPEED_LIMIT_EXCEEDED
Definition: speed_cam.h:61
state
uint8_t state
Definition: signal_lights.h:90
speed_cam::RESULT_PORT_WRITE_FAILURE
@ RESULT_PORT_WRITE_FAILURE
Definition: speed_cam.h:55
speed_cam::SpeedCamVersionCallback
std::function< void(uint32_t)> SpeedCamVersionCallback
Definition: speed_cam.h:48
speed_cam::SpeedCam
Definition: speed_cam.h:66
speed_cam::RESULT_SUCCESS
@ RESULT_SUCCESS
Definition: speed_cam.h:53