Add option for manual gear shifting to VehicleControl

This commit is contained in:
nsubiron 2018-12-11 15:59:41 +01:00
parent b37b76d1cc
commit 56534e9569
6 changed files with 62 additions and 15 deletions

View File

@ -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
@ -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;
@ -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;
}
@ -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 {
@ -77,7 +89,9 @@ namespace rpc {
steer,
brake,
hand_brake,
reverse);
reverse,
manual_gear_shift,
gear);
};
} // namespace rpc

View File

@ -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:
@ -44,6 +46,8 @@ namespace detail {
float brake;
bool hand_brake;
bool reverse;
bool manual_gear_shift;
int32_t gear;
};
#pragma pack(pop)

View File

@ -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;
}
@ -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))

View File

@ -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)

View File

@ -96,6 +96,7 @@ public:
/// @}
// ===========================================================================
/// @name Set functions
/// @todo Keep only ApplyVehicleControl.
// ===========================================================================
/// @{
public:

View File

@ -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;
};