123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- #include "SIM_SilentWings.h"
- #include <stdio.h>
- #include <errno.h>
- #include <AP_HAL/AP_HAL.h>
- extern const AP_HAL::HAL& hal;
- using namespace SITL;
- static const struct {
- const char *name;
- float value;
- bool save;
- } sim_defaults[] = {
- { "AHRS_EKF_TYPE", 10 },
- { "INS_GYR_CAL", 0 },
- { "EK2_ENABLE", 0 },
- { "ARSPD_ENABLE", 1 },
- { "ARSPD_USE", 1 },
- { "INS_ACC2OFFS_X", 0.001 },
- { "INS_ACC2OFFS_Y", 0.001 },
- { "INS_ACC2OFFS_Z", 0.001 },
- { "INS_ACC2SCAL_X", 1.001 },
- { "INS_ACC2SCAL_Y", 1.001 },
- { "INS_ACC2SCAL_Z", 1.001 },
- { "INS_ACCOFFS_X", 0.001 },
- { "INS_ACCOFFS_Y", 0.001 },
- { "INS_ACCOFFS_Z", 0.001 },
- { "INS_ACCSCAL_X", 1.001 },
- { "INS_ACCSCAL_Y", 1.001 },
- { "INS_ACCSCAL_Z", 1.001 },
- };
- SilentWings::SilentWings(const char *frame_str) :
- Aircraft(frame_str),
- last_data_time_ms(0),
- first_pkt_timestamp_ms(0),
- time_base_us(0),
- sock(true),
- home_initialized(false),
- inited_first_pkt_timestamp(false)
- {
-
-
-
-
- for (uint8_t i = 0; i < ARRAY_SIZE(sim_defaults); i++) {
- AP_Param::set_default_by_name(sim_defaults[i].name, sim_defaults[i].value);
- if (sim_defaults[i].save) {
- enum ap_var_type ptype;
- AP_Param *p = AP_Param::find(sim_defaults[i].name, &ptype);
- if (!p->configured()) {
- p->save();
- }
- }
- }
- }
- void SilentWings::set_interface_ports(const char* address, const int port_in, const int port_out)
- {
-
-
-
-
- if (!sock.bind("127.0.0.1", _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);
- sock.reuseaddress();
- sock.set_blocking(false);
- _sw_address = address;
-
- printf("Setting Silent Wings interface to %s:%d \n", _sw_address, _sw_port);
- }
- void SilentWings::send_servos(const struct sitl_input &input)
- {
- char *buf = nullptr;
-
-
-
- float joystick = 0.0f;
- float aileron = filtered_servo_angle(input, 0);
- float elevator = filtered_servo_angle(input, 1);
- float throttle = filtered_servo_range(input, 2);
- float rudder = filtered_servo_angle(input, 3);
- ssize_t buflen = asprintf(&buf,
- "JOY %f\n"
- "AIL %f\n"
- "ELE %f\n"
- "RUD %f\n"
- "THR %f\n",
- joystick, aileron, elevator, rudder, throttle) - 1;
- if (buflen < 0) {
- fprintf(stderr, "Fatal: Failed to allocate enough space for data\n"),
- exit(1);
- }
-
- ssize_t sent = sock.sendto(buf, buflen, _sw_address, _sw_port);
- free(buf);
- if (sent < 0) {
- fprintf(stderr, "Fatal: Failed to send on control socket\n"),
- exit(1);
- }
-
- if (sent < buflen) {
- fprintf(stderr, "Failed to send all bytes on control socket\n");
- }
- }
- bool SilentWings::recv_fdm(void)
- {
- fdm_packet tmp_pkt;
- memset(&pkt, 0, sizeof(pkt));
- ssize_t nread = sock.recv(&tmp_pkt, sizeof(pkt), 0);
-
-
- if (nread != sizeof(pkt)) {
- return false;
- }
-
- memcpy(&pkt, &tmp_pkt, sizeof(pkt));
-
- return true;
- }
- void SilentWings::process_packet()
- {
-
-
-
- if (inited_first_pkt_timestamp) {
- uint64_t tus = (pkt.timestamp - first_pkt_timestamp_ms) * 1.0e3f;
- time_now_us = time_base_us + tus;
- }
- else {
- first_pkt_timestamp_ms = pkt.timestamp;
- time_base_us = time_now_us;
- inited_first_pkt_timestamp = true;
- }
-
- dcm.from_euler(radians(pkt.roll), radians(pkt.pitch), radians(pkt.yaw));
- accel_body = Vector3f(pkt.ax * GRAVITY_MSS, pkt.ay * GRAVITY_MSS, pkt.az * GRAVITY_MSS);
- gyro = Vector3f(radians(pkt.d_roll), radians(pkt.d_pitch), radians(pkt.d_yaw));
-
- velocity_ef = dcm * Vector3f(pkt.vx, pkt.vy, pkt.vz);
-
- wind_ef = dcm * (Vector3f(pkt.vx, pkt.vy, pkt.vz) - Vector3f(pkt.vx_wind, pkt.vy_wind, pkt.vz_wind));
- airspeed = pkt.v_eas;
- airspeed_pitot = pkt.v_eas;
- Location curr_location;
- curr_location.lat = pkt.position_latitude * 1.0e7;
- curr_location.lng = pkt.position_longitude * 1.0e7;
- curr_location.alt = pkt.altitude_msl * 100.0f;
- ground_level = curr_location.alt * 0.01f - pkt.altitude_ground;
- Vector3f posdelta = home.get_distance_NED(curr_location);
- position.x = posdelta.x;
- position.y = posdelta.y;
- position.z = posdelta.z;
- update_position();
-
-
-
- if (curr_location.get_distance(location) > 4 || abs(curr_location.alt - location.alt)*0.01f > 2.0f || !home_initialized) {
- printf("SilentWings home reset dist=%f alt=%.1f/%.1f\n",
- curr_location.get_distance(location), curr_location.alt*0.01f, location.alt*0.01f);
-
- home.lat = curr_location.lat;
- home.lng = curr_location.lng;
-
-
- home.alt = 0;
- position.x = 0;
- position.y = 0;
- position.z = -curr_location.alt;
- home_initialized = true;
- update_position();
- }
-
-
-
-
- double deltat = (AP_HAL::millis() - last_data_time_ms) / 1000.0f;
-
- if (deltat < 0.01 && deltat > 0) {
- adjust_frame_time(1.0/deltat);
- }
-
- last_data_time_ms = AP_HAL::millis();
-
- report.data_count++;
- report.frame_count++;
-
- if (0) {
- printf("Delta: %f Time: %" PRIu64 "\n", deltat, time_now_us);
- printf("Accel.x %f\n", accel_body.x);
- printf("Accel.y %f\n", accel_body.y);
- printf("Accel.z %f\n", accel_body.z);
- printf("Gyro.x %f\n", gyro.x);
- printf("Gyro.y %f\n", gyro.y);
- printf("Gyro.z %f\n", gyro.z);
- printf("Pos.x %f\n", position.x);
- printf("Pos.y %f\n", position.y);
- printf("Pos.z %f\n", position.z);
- printf("Roll %f\n", pkt.roll);
- printf("Pitch %f\n", pkt.pitch);
- printf("Yaw %f\n", pkt.yaw);
- }
- }
- bool SilentWings::interim_update()
- {
- if (AP_HAL::millis() - last_data_time_ms > 200) {
-
- return false;
- }
- float delta_time = frame_time_us * 1e-6f;
- time_now_us += frame_time_us;
- extrapolate_sensors(delta_time);
- update_position();
- report.frame_count++;
- return true;
- }
- void SilentWings::update(const struct sitl_input &input)
- {
- if (recv_fdm()) {
- process_packet();
-
- send_servos(input);
- }
- else if (interim_update()) {
-
-
-
- send_servos(input);
- }
-
-
-
-
- if (!inited_first_pkt_timestamp){
- time_advance();
- }
- else {
- if (use_time_sync) {
- sync_frame_time();
- }
- }
-
- update_mag_field_bf();
-
- uint32_t now = AP_HAL::millis();
-
- if (report.last_report_ms == 0) {
- report.last_report_ms = now;
- printf("Resetting last report time to now\n");
- }
-
- if (now - report.last_report_ms > 5000) {
- float dt = (now - report.last_report_ms) * 1.0e-3f;
- printf("Data rate: %.1f FPS Frame rate: %.1f FPS\n",
- report.data_count/dt, report.frame_count/dt);
- report.last_report_ms = now;
- report.data_count = 0;
- report.frame_count = 0;
- }
- }
|