Skip to content

Commit

Permalink
[Unity] Fix Car API working
Browse files Browse the repository at this point in the history
Set API enabled so that it uses API controls rather than keyboard
  • Loading branch information
rajat2004 committed Sep 27, 2020
1 parent 1aa8a93 commit be2b917
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ void CarPawnApi::updateMovement(const msr::airlib::CarApiBase::CarControls& cont
SetCarApiControls(controls, car_name_.c_str());
}

void CarPawnApi::enableApi(bool enable)
{
SetEnableApi(enable, car_name_.c_str());
}

msr::airlib::CarApiBase::CarState CarPawnApi::getCarState() const
{
AirSimCarState carState = GetCarState(car_name_.c_str());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class CarPawnApi

void updateMovement(const msr::airlib::CarApiBase::CarControls& controls);
msr::airlib::CarApiBase::CarState getCarState() const;
void enableApi(bool enable);

void reset();
void update();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,19 +149,24 @@ void CarPawnSimApi::updateCarControls()
current_controls_ = keyboard_controls_;
}

bool api_enabled = vehicle_api_->isApiControlEnabled();

//if API-client control is not active then we route keyboard/joystick control to car
if (!vehicle_api_->isApiControlEnabled())
if (!api_enabled)
{
//all car controls from anywhere must be routed through API component
// This is so that getCarControls API works correctly
vehicle_api_->setCarControls(current_controls_);
pawn_api_->updateMovement(current_controls_);
}
else
{
PrintLogMessage("Control Mode: ", "API", getVehicleName().c_str(), ErrorLogSeverity::Information);
// API is enabled, so we use the controls set by API
current_controls_ = vehicle_api_->getCarControls();
pawn_api_->updateMovement(current_controls_);
}

// Update whether to use API controls or keyboard controls
pawn_api_->enableApi(api_enabled);
pawn_api_->updateMovement(current_controls_);
}

//*** Start: UpdatableState implementation ***//
Expand Down

0 comments on commit be2b917

Please sign in to comment.