Skip to content

Commit

Permalink
Add option for manual gear shifting to VehicleControl
Browse files Browse the repository at this point in the history
  • Loading branch information
nsubiron committed Dec 13, 2018
1 parent b37b76d commit 56534e9
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 15 deletions.
24 changes: 19 additions & 5 deletions LibCarla/source/carla/rpc/VehicleControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,24 @@ namespace rpc {
float in_steer,
float in_brake,
bool in_hand_brake,
bool in_reverse)
bool in_reverse,
bool in_manual_gear_shift,
int32_t in_gear)
: throttle(in_throttle),
steer(in_steer),
brake(in_brake),
hand_brake(in_hand_brake),
reverse(in_reverse) {}
reverse(in_reverse),
manual_gear_shift(in_manual_gear_shift),
gear(in_gear) {}

float throttle = 0.0f;
float steer = 0.0f;
float brake = 0.0f;
bool hand_brake = false;
bool reverse = false;
bool manual_gear_shift = false;
int32_t gear = 0;

#ifdef LIBCARLA_INCLUDED_FROM_UE4

Expand All @@ -45,7 +51,9 @@ namespace rpc {
steer(Control.Steer),
brake(Control.Brake),
hand_brake(Control.bHandBrake),
reverse(Control.bReverse) {}
reverse(Control.bReverse),
manual_gear_shift(Control.bManualGearShift),
gear(Control.Gear) {}

operator FVehicleControl() const {
FVehicleControl Control;
Expand All @@ -54,6 +62,8 @@ namespace rpc {
Control.Brake = brake;
Control.bHandBrake = hand_brake;
Control.bReverse = reverse;
Control.bManualGearShift = manual_gear_shift;
Control.Gear = gear;
return Control;
}

Expand All @@ -65,7 +75,9 @@ namespace rpc {
steer != rhs.steer ||
brake != rhs.brake ||
hand_brake != rhs.hand_brake ||
reverse != rhs.reverse;
reverse != rhs.reverse ||
manual_gear_shift != rhs.manual_gear_shift ||
gear != rhs.gear;
}

bool operator==(const VehicleControl &rhs) const {
Expand All @@ -77,7 +89,9 @@ namespace rpc {
steer,
brake,
hand_brake,
reverse);
reverse,
manual_gear_shift,
gear);
};

} // namespace rpc
Expand Down
8 changes: 6 additions & 2 deletions LibCarla/source/carla/sensor/data/ActorDynamicState.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ namespace detail {
steer(control.steer),
brake(control.brake),
hand_brake(control.hand_brake),
reverse(control.reverse) {}
reverse(control.reverse),
manual_gear_shift(control.manual_gear_shift),
gear(control.gear) {}

operator rpc::VehicleControl() const {
return {throttle, steer, brake, hand_brake, reverse};
return {throttle, steer, brake, hand_brake, reverse, manual_gear_shift, gear};
}

private:
Expand All @@ -44,6 +46,8 @@ namespace detail {
float brake;
bool hand_brake;
bool reverse;
bool manual_gear_shift;
int32_t gear;
};
#pragma pack(pop)

Expand Down
12 changes: 9 additions & 3 deletions PythonAPI/source/libcarla/Control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ namespace rpc {
<< ", steer=" << control.steer
<< ", brake=" << control.brake
<< ", hand_brake=" << boolalpha(control.hand_brake)
<< ", reverse=" << boolalpha(control.reverse) << ')';
<< ", reverse=" << boolalpha(control.reverse)
<< ", manual_gear_shift=" << boolalpha(control.manual_gear_shift)
<< ", gear=" << control.gear << ')';
return out;
}

Expand All @@ -29,17 +31,21 @@ void export_control() {
namespace cr = carla::rpc;

class_<cr::VehicleControl>("VehicleControl")
.def(init<float, float, float, bool, bool>(
.def(init<float, float, float, bool, bool, bool, int>(
(arg("throttle")=0.0f,
arg("steer")=0.0f,
arg("brake")=0.0f,
arg("hand_brake")=false,
arg("reverse")=false)))
arg("reverse")=false,
arg("manual_gear_shift")=false,
arg("gear")=0)))
.def_readwrite("throttle", &cr::VehicleControl::throttle)
.def_readwrite("steer", &cr::VehicleControl::steer)
.def_readwrite("brake", &cr::VehicleControl::brake)
.def_readwrite("hand_brake", &cr::VehicleControl::hand_brake)
.def_readwrite("reverse", &cr::VehicleControl::reverse)
.def_readwrite("manual_gear_shift", &cr::VehicleControl::manual_gear_shift)
.def_readwrite("gear", &cr::VehicleControl::gear)
.def("__eq__", &cr::VehicleControl::operator==)
.def("__ne__", &cr::VehicleControl::operator!=)
.def(self_ns::str(self_ns::self))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,27 @@ float ACarlaWheeledVehicle::GetMaximumSteerAngle() const

void ACarlaWheeledVehicle::ApplyVehicleControl(const FVehicleControl &VehicleControl)
{
SetThrottleInput(VehicleControl.Throttle);
SetSteeringInput(VehicleControl.Steer);
SetBrakeInput(VehicleControl.Brake);
SetHandbrakeInput(VehicleControl.bHandBrake);
SetReverse(VehicleControl.bReverse);
auto *MovementComponent = GetVehicleMovementComponent();
MovementComponent->SetThrottleInput(VehicleControl.Throttle);
MovementComponent->SetSteeringInput(VehicleControl.Steer);
MovementComponent->SetBrakeInput(VehicleControl.Brake);
MovementComponent->SetHandbrakeInput(VehicleControl.bHandBrake);
if (Control.bReverse != VehicleControl.bReverse)
{
MovementComponent->SetUseAutoGears(!VehicleControl.bReverse);
MovementComponent->SetTargetGear(VehicleControl.bReverse ? -1 : 1, true);
}
else
{
MovementComponent->SetUseAutoGears(!VehicleControl.bManualGearShift);
if (VehicleControl.bManualGearShift)
{
MovementComponent->SetTargetGear(VehicleControl.Gear, true);
}
}
Control = VehicleControl;
Control.Gear = MovementComponent->GetCurrentGear();
Control.bReverse = Control.Gear < 0;
}

void ACarlaWheeledVehicle::SetThrottleInput(const float Value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class CARLA_API ACarlaWheeledVehicle : public AWheeledVehicle
/// @}
// ===========================================================================
/// @name Set functions
/// @todo Keep only ApplyVehicleControl.
// ===========================================================================
/// @{
public:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,10 @@ struct CARLA_API FVehicleControl

UPROPERTY(Category = "Vehicle Control", EditAnywhere, BlueprintReadWrite)
bool bReverse = false;

UPROPERTY(Category = "Vehicle Control", EditAnywhere, BlueprintReadWrite)
bool bManualGearShift = false;

UPROPERTY(Category = "Vehicle Control", EditAnywhere, BlueprintReadWrite, meta = (EditCondition = bManualGearShift))
int32 Gear = 0;
};

0 comments on commit 56534e9

Please sign in to comment.