Line data Source code
1 : /*
2 : This program is free software: you can redistribute it and/or modify
3 : it under the terms of the GNU General Public License as published by
4 : the Free Software Foundation, either version 3 of the License, or
5 : (at your option) any later version.
6 :
7 : This program is distributed in the hope that it will be useful,
8 : but WITHOUT ANY WARRANTY; without even the implied warranty of
9 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 : GNU General Public License for more details.
11 :
12 : You should have received a copy of the GNU General Public License
13 : along with this program. If not, see <http://www.gnu.org/licenses/>.
14 : */
15 : /*
16 : simulator connection for morse simulator http://morse-simulator.github.io/
17 : */
18 :
19 : #pragma once
20 :
21 : #include "SIM_config.h"
22 :
23 : #if AP_SIM_MORSE_ENABLED
24 :
25 : #include <AP_HAL/utility/Socket_native.h>
26 : #include "SIM_Aircraft.h"
27 :
28 : namespace SITL {
29 :
30 : /*
31 : simulation interface
32 : */
33 : class Morse : public Aircraft {
34 : public:
35 : Morse(const char *frame_str);
36 :
37 : /* update model by one time step */
38 : void update(const struct sitl_input &input) override;
39 :
40 : /* static object creator */
41 0 : static Aircraft *create(const char *frame_str) {
42 0 : return NEW_NOTHROW Morse(frame_str);
43 : }
44 :
45 : private:
46 :
47 : // loopback to convert inbound Morse lidar data into inbound mavlink msgs
48 : const char *mavlink_loopback_address = "127.0.0.1";
49 : const uint16_t mavlink_loopback_port = 5762;
50 : SocketAPM_native mav_socket { false };
51 : struct {
52 : // socket to telem2 on aircraft
53 : bool connected;
54 : mavlink_message_t rxmsg;
55 : mavlink_status_t status;
56 : uint8_t seq;
57 : } mavlink {};
58 :
59 : void send_report(void);
60 : uint32_t send_report_last_ms;
61 :
62 : const char *morse_ip = "127.0.0.1";
63 :
64 : // assume sensors are streamed on port 60000
65 : uint16_t morse_sensors_port = 60000;
66 :
67 : // assume we control vehicle on port 60001
68 : uint16_t morse_control_port = 60001;
69 :
70 : enum {
71 : OUTPUT_ROVER_REGULAR=1,
72 : OUTPUT_ROVER_SKID=2,
73 : OUTPUT_QUAD=3,
74 : OUTPUT_PWM=4
75 : } output_type;
76 :
77 : bool connect_sockets(void);
78 : bool parse_sensors(const char *json);
79 : bool sensors_receive(void);
80 : void output_rover_regular(const struct sitl_input &input);
81 : void output_rover_skid(const struct sitl_input &input);
82 : void output_quad(const struct sitl_input &input);
83 : void output_pwm(const struct sitl_input &input);
84 : void report_FPS();
85 :
86 : // buffer for parsing pose data in JSON format
87 : uint8_t sensor_buffer[50000];
88 : uint32_t sensor_buffer_len;
89 :
90 : SocketAPM_native *sensors_sock;
91 : SocketAPM_native *control_sock;
92 :
93 : uint32_t no_data_counter;
94 : uint32_t connect_counter;
95 :
96 : double initial_time_s;
97 : double last_time_s;
98 : double extrapolated_s;
99 : double average_frame_time_s;
100 :
101 : uint64_t socket_frame_counter;
102 : uint64_t last_socket_frame_counter;
103 : uint64_t frame_counter;
104 : double last_frame_count_s;
105 :
106 : enum data_type {
107 : DATA_FLOAT,
108 : DATA_DOUBLE,
109 : DATA_VECTOR3F,
110 : DATA_VECTOR3F_ARRAY,
111 : DATA_FLOAT_ARRAY,
112 : };
113 :
114 : struct {
115 : double timestamp;
116 : struct {
117 : Vector3f angular_velocity;
118 : Vector3f linear_acceleration;
119 : Vector3f magnetic_field;
120 : } imu;
121 : struct {
122 : float x, y, z;
123 : } gps;
124 : struct {
125 : float roll, pitch, yaw;
126 : } pose;
127 : struct {
128 : Vector3f world_linear_velocity;
129 : } velocity;
130 : struct {
131 : struct vector3f_array points;
132 : struct float_array ranges;
133 : } scanner;
134 : } state, last_state;
135 :
136 : // table to aid parsing of JSON sensor data
137 : struct keytable {
138 : const char *section;
139 : const char *key;
140 : void *ptr;
141 : enum data_type type;
142 : } keytable[13] = {
143 : { "", "timestamp", &state.timestamp, DATA_DOUBLE },
144 : { ".imu", "angular_velocity", &state.imu.angular_velocity, DATA_VECTOR3F },
145 : { ".imu", "linear_acceleration", &state.imu.linear_acceleration, DATA_VECTOR3F },
146 : { ".imu", "magnetic_field", &state.imu.magnetic_field, DATA_VECTOR3F },
147 : { ".gps", "x", &state.gps.x, DATA_FLOAT },
148 : { ".gps", "y", &state.gps.y, DATA_FLOAT },
149 : { ".gps", "z", &state.gps.z, DATA_FLOAT },
150 : { ".pose", "roll", &state.pose.roll, DATA_FLOAT },
151 : { ".pose", "pitch", &state.pose.pitch, DATA_FLOAT },
152 : { ".pose", "yaw", &state.pose.yaw, DATA_FLOAT },
153 : { ".velocity", "world_linear_velocity", &state.velocity.world_linear_velocity, DATA_VECTOR3F },
154 : { ".scan", "point_list", &state.scanner.points, DATA_VECTOR3F_ARRAY },
155 : { ".scan", "range_list", &state.scanner.ranges, DATA_FLOAT_ARRAY },
156 : };
157 : };
158 :
159 :
160 : } // namespace SITL
161 :
162 :
163 : #endif // AP_SIM_MORSE_ENABLED
|