Skip to content

Commit

Permalink
Fix bug in how timeout happens while monitoring HIL and SITL communic…
Browse files Browse the repository at this point in the history
…ations. Add diagram explaining how AirSim is loaded and initialized in Unreal.
  • Loading branch information
lovettchris committed May 16, 2017
1 parent dbf901e commit 9998b74
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 15 deletions.
1 change: 1 addition & 0 deletions AirLib/AirLib.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<ClInclude Include="include\common\common_utils\ScheduledExecutor.hpp" />
<ClInclude Include="include\common\common_utils\sincos.hpp" />
<ClInclude Include="include\common\common_utils\StrictMode.hpp" />
<ClInclude Include="include\common\common_utils\Timer.hpp" />
<ClInclude Include="include\common\common_utils\type_utils.hpp" />
<ClInclude Include="include\common\common_utils\Utils.hpp" />
<ClInclude Include="include\common\DelayLine.hpp" />
Expand Down
3 changes: 3 additions & 0 deletions AirLib/AirLib.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,9 @@
<ClInclude Include="include\vehicles\configs\Px4MultiRotor.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\common\common_utils\Timer.hpp">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\controllers\DroneControllerBase.cpp">
Expand Down
47 changes: 47 additions & 0 deletions AirLib/include/common/common_utils/Timer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#ifndef CommonUtils_Timer_hpp
#define CommonUtils_Timer_hpp
#include <chrono>

namespace common_utils {
class Timer {
public:
Timer() {
started_ = false;
}
void start() {
started_ = true;
start_ = now();
}
void stop() {
started_ = false;
end_ = now();
}
double seconds() {
return static_cast<double>(end() - start_) / 1000000.0;
}
double milliseconds() {
return static_cast<double>(end() - start_) / 1000.0;
}
double microseconds() {
return static_cast<double>(end() - start_);
}
bool started() {
return started_;
}
private:
int64_t now() {
return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
}
int64_t end() {
if (started_) {
// not stopped yet, so return "elapsed time so far".
end_ = now();
}
return end_;
}
int64_t start_;
int64_t end_;
bool started_;
};
}
#endif
31 changes: 17 additions & 14 deletions AirLib/src/controllers/MavLinkDroneController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "MavLinkNode.hpp"
#include "MavLinkVideoStream.hpp"
#include "MavLinkVehicle.hpp"
#include "common/common_utils/Timer.hpp"

//sensors
#include "sensors/barometer/BarometerBase.hpp"
Expand All @@ -28,13 +29,15 @@ namespace msr { namespace airlib {


using namespace mavlinkcom;
using namespace common_utils;

static const int pixhawkVendorId = 9900; ///< Vendor ID for Pixhawk board (V2 and V1) and PX4 Flow
static const int pixhawkFMUV4ProductId = 18; ///< Product ID for Pixhawk V2 board
static const int pixhawkFMUV2ProductId = 17; ///< Product ID for Pixhawk V2 board
static const int pixhawkFMUV2OldBootloaderProductId = 22; ///< Product ID for Bootloader on older Pixhawk V2 boards
static const int pixhawkFMUV1ProductId = 16; ///< Product ID for PX4 FMU V1 board
static const int RotorControlsCount = 8;
static const int messageReceivedTimeout = 10; ///< Seconds

class MavLinkLogViewerLog : public MavLinkLog
{
Expand Down Expand Up @@ -103,8 +106,8 @@ struct MavLinkDroneController::impl {
bool is_offboard_mode_;
bool is_simulation_mode_;
PidController thrust_controller_;
int hil_message_check_;
int sitl_message_check_;
Timer hil_message_timer_;
Timer sitl_message_timer_;

void initialize(const ConnectionInfo& connection_info, const SensorCollection* sensors, bool is_simulation)
{
Expand Down Expand Up @@ -270,9 +273,7 @@ struct MavLinkDroneController::impl {
}

void connect()
{
hil_message_check_ = 0;
sitl_message_check_ = 0;
{
createMavConnection(connection_info_);
initializeMavSubscriptions();
}
Expand Down Expand Up @@ -537,8 +538,6 @@ struct MavLinkDroneController::impl {
Utils::setValue(rotor_controls_, 0.0f);
was_reset_ = false;
debug_pose_ = Pose::nanPose();
hil_message_check_ = 0;
sitl_message_check_ = 0;
}

//*** Start: VehicleControllerBase implementation ***//
Expand Down Expand Up @@ -1002,12 +1001,13 @@ struct MavLinkDroneController::impl {
MavLinkTelemetry data;
connection_->getTelemetry(data);
if (data.messagesReceived == 0) {
hil_message_check_++;
if (hil_message_check_ == 100) {
if (!hil_message_timer_.started()) {
hil_message_timer_.start();
} else if (hil_message_timer_.seconds() > messageReceivedTimeout) {
addStatusMessage("not receiving any messages from HIL, please restart your HIL node and try again");
}
} else {
hil_message_check_ = 0;
hil_message_timer_.stop();
}

// listen to the other mavlink connection also
Expand All @@ -1020,15 +1020,18 @@ struct MavLinkDroneController::impl {
data.messagesHandled += sitl.messagesHandled;
data.messagesReceived += sitl.messagesReceived;
data.messagesSent += sitl.messagesSent;

if (sitl.messagesReceived == 0)
{
sitl_message_check_++;
if (sitl_message_check_ == 100) {
addStatusMessage("not receiving any messages from SITL, please restart your SITL app and try again");
if (!sitl_message_timer_.started()) {
sitl_message_timer_.start();
}
else if (sitl_message_timer_.seconds() > messageReceivedTimeout) {
addStatusMessage("not receiving any messages from SITL, please restart your SITL node and try again");
}
}
else {
sitl_message_check_ = 0;
sitl_message_timer_.stop();
}
}

Expand Down
8 changes: 7 additions & 1 deletion docs/code_structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,10 @@ DroneShell demonstrates how to connect to the simulator using UDP. The simulato

## Contributing

See [Contibution Guidelines](docs/contributing.md)
See [Contibution Guidelines](docs/contributing.md)

## Unreal Framework

The following picture illustrates how AirSim is loaded and invoked by the Unreal Game Engine:

![AirSimConstruction](images/airsim_startup.png)
Binary file added docs/images/airsim_startup.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 9998b74

Please sign in to comment.