diff options
author | Yuta Doi <yuta-d@witz-inc.co.jp> | 2018-05-17 16:48:33 +0900 |
---|---|---|
committer | Yuta Doi <yuta-d@witz-inc.co.jp> | 2018-05-17 16:52:10 +0900 |
commit | 9f61fdc39edd93c7da5b77b4bc92963e4df31b82 (patch) | |
tree | 4c9648fdef3435581a327ca0a7a11ad8d2e03148 | |
parent | a9f97575b05a5e49ace1f34e608bbb5aabdb8311 (diff) |
App (ResourceManager) class has the current car info
and LowCanClient does not check whether CAN signal value is changed
Change-Id: I41dfd0772f8fb4db70c4b5c0caa35791600abc54
Signed-off-by: Yuta Doi <yuta-d@witz-inc.co.jp>
-rw-r--r-- | src/app.cpp | 42 | ||||
-rw-r--r-- | src/app.hpp | 11 | ||||
-rw-r--r-- | src/low_can_client.cpp | 205 | ||||
-rw-r--r-- | src/low_can_client.hpp | 73 | ||||
-rw-r--r-- | src/main.cpp | 74 |
5 files changed, 165 insertions, 240 deletions
diff --git a/src/app.cpp b/src/app.cpp index 296ce35..ac789ad 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -119,6 +119,13 @@ App::App(wl::display *d) } catch (std::exception &e) { HMI_ERROR("wm", "Loading of configuration failed: %s", e.what()); } + + // Initialize current car info + this->crr_car_info_.parking_brake_stt = true; + this->crr_car_info_.accel_pedal_stt = false; + this->crr_car_info_.accel_pedal_pos = 0; + this->crr_car_info_.car_stt = "stop"; + this->crr_car_info_.headlamp_stt = false; } int App::init() { @@ -369,15 +376,16 @@ void App::allocateWindowResource(char const *event, char const *drawing_name, json_bool is_changed; is_changed = jh::getBoolFromJson(json_parking_brake, "is_changed"); if (is_changed) { - // Update parking brake state std::string parking_brake_state = jh::getStringFromJson(json_parking_brake, "state"); HMI_DEBUG("wm", "parking_brake_state: %s", parking_brake_state.c_str()); - // Emit parking brake event + // Update state and emit event if ("parking_brake_off" == parking_brake_state) { + this->crr_car_info_.parking_brake_stt = false; this->emitParkingBrakeOff(); } else if ("parking_brake_on" == parking_brake_state) { + this->crr_car_info_.parking_brake_stt = true; this->emitParkingBrakeOn(); } else { @@ -393,6 +401,24 @@ void App::allocateWindowResource(char const *event, char const *drawing_name, return; } + is_changed = jh::getBoolFromJson(json_accel_pedal, "is_changed"); + if (is_changed) { + std::string accel_pedal_state = jh::getStringFromJson(json_accel_pedal, "state"); + HMI_DEBUG("wm", "accel_pedal_state: %s", accel_pedal_state.c_str()); + + // Update state + if ("accel_pedal_off" == accel_pedal_state) { + this->crr_car_info_.accel_pedal_stt = false; + } + else if ("accel_pedal_on" == accel_pedal_state) { + this->crr_car_info_.accel_pedal_stt = true; + } + else { + reply("Unknown accel pedal state"); + return; + } + } + // Check car state json_object* json_car; if (!json_object_object_get_ex(json_out, "car", &json_car)) { @@ -402,15 +428,16 @@ void App::allocateWindowResource(char const *event, char const *drawing_name, is_changed = jh::getBoolFromJson(json_car, "is_changed"); if (is_changed) { - // Update car state std::string car_state = jh::getStringFromJson(json_car, "state"); HMI_DEBUG("wm", "car_state: %s", car_state.c_str()); // Emit car event if ("car_stop" == car_state) { + this->crr_car_info_.car_stt = "stop"; this->emitCarStop(); } else if ("car_run" == car_state) { + this->crr_car_info_.car_stt = "run"; this->emitCarRun(); } else { @@ -428,15 +455,16 @@ void App::allocateWindowResource(char const *event, char const *drawing_name, is_changed = jh::getBoolFromJson(json_lamp, "is_changed"); if (is_changed) { - // Update car state std::string lamp_state = jh::getStringFromJson(json_lamp, "state"); HMI_DEBUG("wm", "lamp_state: %s", lamp_state.c_str()); - // Emit lamp event + // Update state and emit event if ("lamp_off" == lamp_state) { + this->crr_car_info_.headlamp_stt = false; this->emitHeadlampOff(); } else if ("lamp_on" == lamp_state) { + this->crr_car_info_.headlamp_stt = true; this->emitHeadlampOn(); } else { @@ -1066,6 +1094,10 @@ void App::setSurfaceSize(const char* role, const char* area) { this->enqueue_flushdraw(surface_id); } +void App::setAccelPedalPos(double val) { + this->crr_car_info_.accel_pedal_pos = val; +} + extern const char* kDefaultAppDb; int App::loadAppDb() { HMI_DEBUG("wm", "Call"); diff --git a/src/app.hpp b/src/app.hpp index b7d6815..13bc421 100644 --- a/src/app.hpp +++ b/src/app.hpp @@ -128,6 +128,14 @@ struct id_allocator { } }; +typedef struct CarInfo { + bool parking_brake_stt; + bool accel_pedal_stt; + double accel_pedal_pos; + const char *car_stt; + bool headlamp_stt; +} CarInfo; + struct App { typedef std::unordered_map<uint32_t, struct compositor::rect> rect_map; @@ -234,6 +242,8 @@ struct App { void surface_created(uint32_t surface_id); void surface_removed(uint32_t surface_id); + void setAccelPedalPos(double val); + private: PolicyManager pm_; LayoutManager lm_; @@ -241,6 +251,7 @@ private: std::unordered_map<std::string, std::string> app2role_; std::unordered_map<std::string, std::string> role2app_; std::unordered_map<int, int> appid2role_; + CarInfo crr_car_info_; int allocateSurface(); void setSurfaceSize(const char* role, const char* area); diff --git a/src/low_can_client.cpp b/src/low_can_client.cpp index e3e472f..192e124 100644 --- a/src/low_can_client.cpp +++ b/src/low_can_client.cpp @@ -32,15 +32,8 @@ LowCanClient::LowCanClient() : headlamp_status_(FALSE), parking_brake_status_(TRUE), accel_pedal_pos_(0), - prv_lamp_state_("lamp_off"), - crr_lamp_state_("lamp_off"), - prv_parking_brake_state_("parking_brake_on"), - crr_parking_brake_state_("parking_brake_on"), - prv_accel_pedal_state_("accel_pedal_off"), - crr_accel_pedal_state_("accel_pedal_off"), - is_changed_lamp_state_(false), - is_changed_parking_brake_state_(false), - is_changed_accel_pedal_state_(false) + accel_pedal_stt_(FALSE), + is_changed_accel_pedal_stt_(false) { HMI_DEBUG("wm:lcc", "Call"); } @@ -67,7 +60,7 @@ void LowCanClient::initialize() { // Set Event json_object *json_obj = json_object_new_object(); json_object_object_add(json_obj, "event", - json_object_new_string(this->kEventName_[i])); + json_object_new_string(this->kSignalName_[i])); // Set filter if (0 != strcmp("", this->kFilterValue_[i])) { @@ -88,197 +81,91 @@ void LowCanClient::initialize() { return; } -void LowCanClient::analyzeCanSignal(struct json_object *object) { +const char* LowCanClient::analyzeCanSignal(struct json_object *object) { HMI_DEBUG("wm:lcc", "object:%s", json_object_get_string(object)); const char* name = jh::getStringFromJson(object, "name"); HMI_DEBUG("wm:lcc", "CAN signal name:%s", name); - if (strstr(name, this->kEventName_[0])) { - HMI_DEBUG("wm:lcc", "Receive %s", this->kEventName_[0]); + if (strstr(name, this->kSignalName_[0])) { // Update vehicle speed - int speed = jh::getIntFromJson(object, "value"); - if (this->vehicle_speed_ != speed) { - this->vehicle_speed_ = speed; - HMI_DEBUG("wm:lcc", "Update vehicle speed:%d", this->vehicle_speed_); - } + this->vehicle_speed_ = jh::getIntFromJson(object, "value"); + HMI_DEBUG("wm:lcc", "Update vehicle speed:%d", this->vehicle_speed_); } - else if (strstr(name, this->kEventName_[1])) { - HMI_DEBUG("wm:lcc", "Receive %s", this->kEventName_[1]); + else if (strstr(name, this->kSignalName_[1])) { // Update transmission gear position - int gear_pos = jh::getIntFromJson(object, "value"); - if (this->trans_gear_pos_ != gear_pos) { - this->trans_gear_pos_ = gear_pos; - HMI_DEBUG("wm:lcc", "Update transmission gear position:%d", this->trans_gear_pos_); - } + this->trans_gear_pos_ = jh::getIntFromJson(object, "value"); + HMI_DEBUG("wm:lcc", "Update transmission gear position:%d", this->trans_gear_pos_); } - else if (strstr(name, this->kEventName_[2])) { - HMI_DEBUG("wm:lcc", "Receive %s", this->kEventName_[2]); + else if (strstr(name, this->kSignalName_[2])) { // Update headlamp status - json_bool headlamp = jh::getBoolFromJson(object, "value"); - if (this->headlamp_status_ != headlamp) { - this->headlamp_status_ = headlamp; - HMI_DEBUG("wm:lcc", "Update headlamp status:%d", this->headlamp_status_); - } + this->headlamp_status_ = jh::getBoolFromJson(object, "value"); + HMI_DEBUG("wm:lcc", "Update headlamp status:%d", this->headlamp_status_); } - else if (strstr(name, this->kEventName_[3])) { - HMI_DEBUG("wm:lcc", "Receive %s", this->kEventName_[3]); + else if (strstr(name, this->kSignalName_[3])) { // Update parking gear status - json_bool parking_brake = jh::getBoolFromJson(object, "value"); - if (this->parking_brake_status_ != parking_brake) { - this->parking_brake_status_ = parking_brake; - HMI_DEBUG("wm:lcc", "Update parking brake status:%d", this->parking_brake_status_); - } - } - else if (strstr(name, this->kEventName_[4])) { - HMI_DEBUG("wm:lcc", "Receive %s", this->kEventName_[4]); - // Update accelerator pedal status - double accel_pedal_pos = jh::getDoubleFromJson(object, "value"); - if (this->accel_pedal_pos_ != accel_pedal_pos) { - this->accel_pedal_pos_ = accel_pedal_pos; - HMI_DEBUG("wm:lcc", "Update accelerator pedal status:%lf", this->accel_pedal_pos_); - } - } - - // Update transmission gear position state - if (TransGearPosValN == this->trans_gear_pos_) { - this->crr_accel_pedal_state_ = "trans_gear_neutral"; - } - else { - this->crr_accel_pedal_state_ = "trans_gear_not_neutral"; - } - HMI_DEBUG("wm:lcc", "Current transmission gear state:%s", this->crr_trans_gear_state_.c_str()); - - // Update lamp state - if (true == this->headlamp_status_) { - this->crr_lamp_state_ = "lamp_on"; - } - else { - this->crr_lamp_state_ = "lamp_off"; - } - HMI_DEBUG("wm:lcc", "Current lamp state:%s", this->crr_lamp_state_.c_str()); - - // Update parking brake state - if (this->parking_brake_status_) { - this->crr_parking_brake_state_ = "parking_brake_on"; - } - else { - this->crr_parking_brake_state_ = "parking_brake_off"; - } - HMI_DEBUG("wm:lcc", "Current parking brake state:%s", this->crr_parking_brake_state_.c_str()); - - // Update accelerator pedal state - if (0 == this->accel_pedal_pos_) { - this->crr_accel_pedal_state_ = "accel_pedal_off"; - } - else { - this->crr_accel_pedal_state_ = "accel_pedal_on"; - } - HMI_DEBUG("wm:lcc", "Current accelerator pedal state:%s", this->crr_accel_pedal_state_.c_str()); - - // If transmission gear state is changed, - // backup current state for previous state and set flag - if (this->prv_trans_gear_state_ != this->crr_trans_gear_state_) { - HMI_DEBUG("wm:lcc", "Transmission Gear is changed: %s -> %s", - this->prv_trans_gear_state_.c_str(), this->crr_trans_gear_state_.c_str()); - this->prv_trans_gear_state_ = this->crr_trans_gear_state_; - this->is_changed_trans_gear_state_ = true; + this->parking_brake_status_ = jh::getBoolFromJson(object, "value"); + HMI_DEBUG("wm:lcc", "Update parking brake status:%d", this->parking_brake_status_); } + else if (strstr(name, this->kSignalName_[4])) { + // Clear flag for whether accel pedal state is changed + this->is_changed_accel_pedal_stt_ = false; - // If lamp state is changed, - // backup current state for previous state and set flag - if (this->prv_lamp_state_ != this->crr_lamp_state_) { - HMI_DEBUG("wm:lcc", "Lamp state is changed: %s -> %s", - this->prv_lamp_state_.c_str(), this->crr_lamp_state_.c_str()); - this->prv_lamp_state_ = this->crr_lamp_state_; - this->is_changed_lamp_state_ = true; - } + // Update accelerator pedal status + this->accel_pedal_pos_ = jh::getDoubleFromJson(object, "value"); + HMI_DEBUG("wm:lcc", "Update accelerator pedal position:%lf", this->accel_pedal_pos_); - // If parking brake state is changed, - // backup current state for previous state and set flag - if (this->prv_parking_brake_state_ != this->crr_parking_brake_state_) { - HMI_DEBUG("wm:lcc", "Parking Brake state is changed: %s -> %s", - this->prv_parking_brake_state_.c_str(), this->crr_parking_brake_state_.c_str()); - this->prv_parking_brake_state_ = this->crr_parking_brake_state_; - this->is_changed_parking_brake_state_ = true; - } + bool accel_pedal_stt; + if (0 != this->accel_pedal_pos_) { + accel_pedal_stt = true; + } + else { + accel_pedal_stt = false; + } - // If accelerator pedal state is changed, - // backup current state for previous state and set flag - if (this->prv_accel_pedal_state_ != this->crr_accel_pedal_state_) { - HMI_DEBUG("wm:lcc", "Accelerator Pedal is changed: %s -> %s", - this->prv_accel_pedal_state_.c_str(), this->crr_accel_pedal_state_.c_str()); - this->prv_accel_pedal_state_ = this->crr_accel_pedal_state_; - this->is_changed_accel_pedal_state_ = true; + if (accel_pedal_stt != this->accel_pedal_stt_) { + this->is_changed_accel_pedal_stt_ = true; + this->accel_pedal_stt_ = accel_pedal_stt; + } } -} -bool LowCanClient::isChangedTransGearState() { - HMI_DEBUG("wm:lcc", "Call"); - - // Return changed flag - return this->is_changed_trans_gear_state_; + return name; } -bool LowCanClient::isChangedLampState() { - HMI_DEBUG("wm:lcc", "Call"); - - // Return changed flag - return this->is_changed_lamp_state_; -} - -bool LowCanClient::isChangedParkingBrakeState() { +bool LowCanClient::isChangedAccelPedalState() { HMI_DEBUG("wm:lcc", "Call"); - // Return changed flag - return this->is_changed_parking_brake_state_; + return this->is_changed_accel_pedal_stt_; } -bool LowCanClient::isChangedAccelPedalState() { +int LowCanClient::getCurrentTransGearState() { HMI_DEBUG("wm:lcc", "Call"); - // Return changed flag - return this->is_changed_accel_pedal_state_; + return this->trans_gear_pos_; } -const char* LowCanClient::getCurrentTransGearState() { +bool LowCanClient::getCurrentHeadlampState() { HMI_DEBUG("wm:lcc", "Call"); - // Clear changed flag - this->is_changed_trans_gear_state_ = false; - - // Return current transmission gear state - return this->crr_trans_gear_state_.c_str(); + return (bool)this->headlamp_status_; } -const char* LowCanClient::getCurrentLampState() { +bool LowCanClient::getCurrentParkingBrakeState() { HMI_DEBUG("wm:lcc", "Call"); - // Clear changed flag - this->is_changed_lamp_state_ = false; - - // Return current lamp state - return this->crr_lamp_state_.c_str(); + return (bool)this->parking_brake_status_; } -const char* LowCanClient::getCurrentParkingBrakeState() { +double LowCanClient::getCurrentAccelPedalPosition() { HMI_DEBUG("wm:lcc", "Call"); - // Clear changed flag - this->is_changed_parking_brake_state_ = false; - - // Return current parking brake state - return this->crr_parking_brake_state_.c_str(); + return this->accel_pedal_pos_; } -const char* LowCanClient::getCurrentAccelPedalState() { +bool LowCanClient::getCurrentAccelPedalState() { HMI_DEBUG("wm:lcc", "Call"); - // Clear changed flag - this->is_changed_accel_pedal_state_ = false; - - // Return current accelerator pedal state - return this->crr_accel_pedal_state_.c_str(); + return this->accel_pedal_stt_; } diff --git a/src/low_can_client.hpp b/src/low_can_client.hpp index 9cd10e2..4bc3747 100644 --- a/src/low_can_client.hpp +++ b/src/low_can_client.hpp @@ -31,16 +31,37 @@ public: explicit LowCanClient(); ~LowCanClient() = default; + enum SignalNo { + SignalNoVehicliSpeed = 0, + SignalNoTransGearPos, + SignalNoHeadlame, + SignalNoParkingBrake, + SignalNoAccelPedalPos, + + SignalNum, + + SignalNoMin = SignalNoVehicliSpeed, + SignalNoMax = SignalNum - 1, + }; + + const std::vector<const char*> kSignalName_{ + "vehicle.speed", + "transmission_gear_position", + "headlamp_status", + "parking_brake_status", + "accelerator.pedal.position", + }; + void initialize(); - void analyzeCanSignal(struct json_object *object); - bool isChangedTransGearState(); - bool isChangedLampState(); - bool isChangedParkingBrakeState(); + const char* analyzeCanSignal(struct json_object *object); + + int getCurrentTransGearState(); + bool getCurrentHeadlampState(); + bool getCurrentParkingBrakeState(); + double getCurrentAccelPedalPosition(); + bool getCurrentAccelPedalState(); + bool isChangedAccelPedalState(); - const char* getCurrentTransGearState(); - const char* getCurrentLampState(); - const char* getCurrentParkingBrakeState(); - const char* getCurrentAccelPedalState(); private: // Disable copy and move @@ -62,27 +83,6 @@ private: TransGearPosValN, }; - enum SignalNo { - SignalNoVehicliSpeed = 0, - SignalNoTransGearPos, - SignalNoHeadlame, - SignalNoParkingBrake, - SignalNoAccelPedalPos, - - SignalNum, - - SignalNoMin = SignalNoVehicliSpeed, - SignalNoMax = SignalNum - 1, - }; - - const std::vector<const char*> kEventName_{ - "vehicle.speed", - "transmission_gear_position", - "headlamp_status", - "parking_brake_status", - "accelerator.pedal.position", - }; - const std::vector<const char*> kFilterValue_{ "", "", @@ -96,20 +96,9 @@ private: json_bool headlamp_status_; json_bool parking_brake_status_; double accel_pedal_pos_; + bool accel_pedal_stt_; - std::string prv_trans_gear_state_; - std::string crr_trans_gear_state_; - std::string prv_lamp_state_; - std::string crr_lamp_state_; - std::string prv_parking_brake_state_; - std::string crr_parking_brake_state_; - std::string prv_accel_pedal_state_; - std::string crr_accel_pedal_state_; - - bool is_changed_trans_gear_state_; - bool is_changed_lamp_state_; - bool is_changed_parking_brake_state_; - bool is_changed_accel_pedal_state_; + bool is_changed_accel_pedal_stt_; }; } // namespace wm diff --git a/src/main.cpp b/src/main.cpp index 3a60b94..1db33a4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -664,50 +664,56 @@ void on_event(const char *event, struct json_object *object){ // If receive low can signal if (strstr(event, "low-can")) { + wm::LowCanClient *lcc = &(g_afb_instance->lcc_); + wm::App *app = &(g_afb_instance->app); + // Analyze low can signal - g_afb_instance->lcc_.analyzeCanSignal(object); + const char* signal_name = lcc->analyzeCanSignal(object); - if (g_afb_instance->lcc_.isChangedParkingBrakeState()) { - // If parking brake state is changed + // If car info is updated, set event name + const char *can_event = nullptr; + if (strstr(signal_name, lcc->kSignalName_[lcc->SignalNoParkingBrake])) { HMI_DEBUG("wm", "Parking Brake state is changed"); - // Get parking brake state - const char* parking_brake_state = g_afb_instance->lcc_.getCurrentParkingBrakeState(); - - // Allocate window resource - g_afb_instance->app.allocateWindowResource(parking_brake_state, nullptr, - nullptr, nullptr, - [](const char* errmsg){ - if (errmsg != nullptr) { - HMI_ERROR("wm", errmsg); - } - }); + // Set event + if (lcc->getCurrentParkingBrakeState()) { + can_event = "parking_brake_on"; + } + else { + can_event = "parking_brake_off"; + } } - if (g_afb_instance->lcc_.isChangedAccelPedalState()) { - // If accelerator pedal state is changed - HMI_DEBUG("wm", "Accelerator Pedal state is changed"); + else if (strstr(signal_name, lcc->kSignalName_[lcc->SignalNoAccelPedalPos])) { + // Update accel pedal position + app->setAccelPedalPos(lcc->getCurrentAccelPedalPosition()); - // Get parking brake state - const char* accel_pedal_state = g_afb_instance->lcc_.getCurrentAccelPedalState(); + if (lcc->isChangedAccelPedalState()) { + HMI_DEBUG("wm", "Accelerator Pedal state is changed"); - // Allocate window resource - g_afb_instance->app.allocateWindowResource(accel_pedal_state, nullptr, - nullptr, nullptr, - [](const char* errmsg){ - if (errmsg != nullptr) { - HMI_ERROR("wm", errmsg); + // Set event + if (lcc->getCurrentAccelPedalState()) { + can_event = "accel_pedal_on"; } - }); + else { + can_event = "accel_pedal_off"; + } + } + } + else if (strstr(signal_name, lcc->kSignalName_[lcc->SignalNoHeadlame])) { + HMI_DEBUG("wm", "Headlamp state is changed"); + + // Set event + if (lcc->getCurrentHeadlampState()) { + can_event = "headlamp_on"; + } + else { + can_event = "headlamp_off"; + } } - else if (g_afb_instance->lcc_.isChangedLampState()) { - // If lamp state is changed - HMI_DEBUG("wm", "Lamp state is changed"); - - // Get lamp state - const char* lamp_state = g_afb_instance->lcc_.getCurrentLampState(); - // Allocate window resource - g_afb_instance->app.allocateWindowResource(lamp_state, nullptr, + // Allocate window resource + if (nullptr != can_event) { + g_afb_instance->app.allocateWindowResource(can_event, nullptr, nullptr, nullptr, [](const char* errmsg){ if (errmsg != nullptr) { |