123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- #include "SIM_Gazebo.h"
- #include <stdio.h>
- #include <errno.h>
- #include <AP_HAL/AP_HAL.h>
- extern const AP_HAL::HAL& hal;
- namespace SITL {
- Gazebo::Gazebo(const char *frame_str) :
- Aircraft(frame_str),
- last_timestamp(0),
- socket_sitl{true}
- {
- fprintf(stdout, "Starting SITL Gazebo\n");
- }
- void Gazebo::set_interface_ports(const char* address, const int port_in, const int port_out)
- {
-
-
-
- if (!socket_sitl.bind("0.0.0.0", port_in)) {
- fprintf(stderr, "SITL: socket in bind failed on sim in : %d - %s\n", port_in, strerror(errno));
- fprintf(stderr, "Aborting launch...\n");
- exit(1);
- }
- printf("Bind %s:%d for SITL in\n", "127.0.0.1", port_in);
- socket_sitl.reuseaddress();
- socket_sitl.set_blocking(false);
- _gazebo_address = address;
- _gazebo_port = port_out;
- printf("Setting Gazebo interface to %s:%d \n", _gazebo_address, _gazebo_port);
- }
- void Gazebo::send_servos(const struct sitl_input &input)
- {
- servo_packet pkt;
-
-
- for (unsigned i = 0; i < 16; ++i)
- {
- pkt.motor_speed[i] = (input.servos[i]-1000) / 1000.0f;
- }
- socket_sitl.sendto(&pkt, sizeof(pkt), _gazebo_address, _gazebo_port);
- }
- void Gazebo::recv_fdm(const struct sitl_input &input)
- {
- fdm_packet pkt;
-
- while (socket_sitl.recv(&pkt, sizeof(pkt), 100) != sizeof(pkt)) {
- send_servos(input);
-
- if (get_wall_time_us() > last_wall_time_us + GAZEBO_TIMEOUT_US) {
- last_timestamp = 0;
- }
- }
- const double deltat = pkt.timestamp - last_timestamp;
- if (deltat < 0) {
- time_now_us += 1;
- return;
- }
-
- accel_body = Vector3f(static_cast<float>(pkt.imu_linear_acceleration_xyz[0]),
- static_cast<float>(pkt.imu_linear_acceleration_xyz[1]),
- static_cast<float>(pkt.imu_linear_acceleration_xyz[2]));
- gyro = Vector3f(static_cast<float>(pkt.imu_angular_velocity_rpy[0]),
- static_cast<float>(pkt.imu_angular_velocity_rpy[1]),
- static_cast<float>(pkt.imu_angular_velocity_rpy[2]));
-
- Quaternion quat(static_cast<float>(pkt.imu_orientation_quat[0]),
- static_cast<float>(pkt.imu_orientation_quat[1]),
- static_cast<float>(pkt.imu_orientation_quat[2]),
- static_cast<float>(pkt.imu_orientation_quat[3]));
- quat.rotation_matrix(dcm);
- velocity_ef = Vector3f(static_cast<float>(pkt.velocity_xyz[0]),
- static_cast<float>(pkt.velocity_xyz[1]),
- static_cast<float>(pkt.velocity_xyz[2]));
- position = Vector3f(static_cast<float>(pkt.position_xyz[0]),
- static_cast<float>(pkt.position_xyz[1]),
- static_cast<float>(pkt.position_xyz[2]));
-
- time_now_us += static_cast<uint64_t>(deltat * 1.0e6);
- if (deltat < 0.01 && deltat > 0) {
- adjust_frame_time(static_cast<float>(1.0/deltat));
- }
- last_timestamp = pkt.timestamp;
- }
- void Gazebo::drain_sockets()
- {
- const uint16_t buflen = 1024;
- char buf[buflen];
- ssize_t received;
- errno = 0;
- do {
- received = socket_sitl.recv(buf, buflen, 0);
- if (received < 0) {
- if (errno != EAGAIN && errno != EWOULDBLOCK && errno != 0) {
- fprintf(stderr, "error recv on socket in: %s \n",
- strerror(errno));
- }
- } else {
-
- }
- } while (received > 0);
- }
- void Gazebo::update(const struct sitl_input &input)
- {
- send_servos(input);
- recv_fdm(input);
- update_position();
- time_advance();
-
- update_mag_field_bf();
- drain_sockets();
- }
- }
|