123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461 |
- #define WIN32_LEAN_AND_MEAN
- #include <WinSock2.h>
- #include "./include/EgoInterface.h"
- #include "EgoWindow.h"
- #include "protocol.pb.h"
- #include "control_sensor.h"
- #include "../common/iobuffer.h"
- #include "protocol.pb.h"
- CControlSensor::CControlSensor(CMessageQueue* q)
- {
- _window = static_cast<CEgoWindow*>(q);
- assert(_window != nullptr);
- _collection = Sensor_Collection::Sensor_None;
- _brake = _bucket = _throttle = _steer = 0;
- _arm = 0;
- _bucket = 0;
- _emergency= _back_on = _front_on = _wiper_on = false;
- }
- void CControlSensor::Notify(int8_t* buffer, int32_t size)
- {
- int32_t receivedCnt = size / CAN_MSG_LEN;
- cannet_frame* p = (cannet_frame*)buffer;
-
-
-
- for (int32_t i = 0; i < receivedCnt; i++)
- {
- int32_t id = ntohl(p->canid) & 0x0FFF;
-
- switch (id)
- {
- case 0x181:
- {
- OnSignal(p->data);
-
- _collection = (Sensor_Collection)(_collection | Sensor_Collection::Sensor_Task);
- }
- break;
- case 0x633:
- {
- bool b = OnLeftJoyStick(p->data);
- if (b == true)
- _collection = (Sensor_Collection)(_collection | Sensor_Collection::Sensor_Left);
- }
- break;
- case 0x634:
- {
- bool b = OnRightJoyStick(p->data);
- if (b == true)
- _collection = (Sensor_Collection)(_collection | Sensor_Collection::Sensor_Right);
- }
- break;
- }
-
- }
- if (_window->GetControlState()== ControlState::Process&&_collection == Sensor_Collection::Sensor_All)
- {
-
-
- RemoNet::CCRobotAnalog _robot;
- _robot.set_brake(_brake);
- _robot.set_arm(_arm);
- _robot.set_bucket(_bucket);
- _robot.set_steer(_steer);
- _robot.set_throttle(_throttle);
- _robot.set_frontlight(_front_on);
- _robot.set_backlight(_back_on);
- _robot.set_wipe(_wiper_on);
- _robot.set_emergency(_emergency);
- _robot.set_gears(_gear);
- _robot.set_resume(_resume);
- MessageHead Head;
- CIOBuffer pBuffer;
- Head.Command = RemoNet::CC_RobotAnalog;
- Head.Length = _robot.ByteSizeLong();
- Head.Serialize(pBuffer.Buffer);
- auto ptr = pBuffer.Buffer + MessageHead::Size();
- _robot.SerializeToArray(ptr, Head.Length);
- pBuffer.Length = MessageHead::Size() + Head.Length;
- _window->SendData(&pBuffer);
-
- _collection = Sensor_Collection::Sensor_None;
- }
- }
- bool CControlSensor::OnSignal(int8_t* data)
- {
-
-
-
- bool ret = (data[0] & 0x10) != 0;
-
- {
- _front_on = (data[0] & 0x10) != 0;
-
-
- }
- ret = (data[0] & 0x8) != 0;
-
- {
- _back_on = (data[0] & 0x8) != 0;
-
- }
-
- if (_window->GetControlState()==ControlState::Process)
- {
- _wiper_on = (data[1] & 0x1) != 0;
-
- }
-
- _resume = (data[2]) != 0;
- _emergency = data[3] != 0;
- _throttle = data[4];
- _brake = data[5];
- return true;
-
- }
- bool CControlSensor::OnLeftJoyStick(int8_t* data)
- {
- int32_t signal = 0;
- if ((data[0] & 0x3) != 0)
- {
- if ((data[0] & 0x03) != 0x01)
- {
- return false;
- }
- signal = 0;
- }
- if ((data[0] & (0x3 << 2)) != 0)
- {
- int32_t value = data[0] >> 2;
- if ((value & 0x03) != 0x01)
- {
- return false;
- }
- signal = -1;
- }
- if ((data[0] & (0x3 << 4)) != 0)
- {
- int32_t value = data[0] >> 4;
- if ((value & 0x03) != 0x01)
- {
- return false;
- }
- signal = 1;
- }
- uint16_t xvalue = ((uint16_t)data[1]) & 0x00FF;
- xvalue <<= 2;
- uint16_t lvalue = ((uint16_t)data[0]) & 0x00FF;
- xvalue |= (lvalue >> 6);
- _steer = xvalue * signal;
-
-
-
-
- _gear = RemoNet::Gears::N;
- int32_t d = (data[5] >> 4) & 0x3;
- int32_t r = (data[5] >> 6) & 0x3;
- if (d == 0x1)
- {
- _gear = RemoNet::Gears::D;
- }
- else if (r == 0x1)
- {
- _gear = RemoNet::Gears::R;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- return true;
- }
- bool CControlSensor::OnRightJoyStick(int8_t* data)
- {
-
-
- int32_t signal = 0;
-
- if ((data[2] & (0x3 << 2)) != 0)
- {
- int32_t value = data[2] >> 2;
- if ((value & 0x03) != 0x01)
- {
- return false;
- }
- signal = -1;
- }
- else if ((data[2] & (0x3 << 4)) != 0)
- {
- int32_t value = data[2] >> 4;
- if ((value & 0x03) != 0x01)
- {
- return false;
- }
-
- signal = 1;
- }
- uint16_t xvalue = ((uint16_t)data[3]) & 0x00FF;
- xvalue <<= 2;
- uint16_t lvalue = ((uint16_t)(data[2]>>6)) & 0x0003;
- xvalue |= lvalue;
- _arm = xvalue * signal;
-
-
-
-
-
- if ((data[0] & (0x3 << 2)) != 0)
- {
- int32_t value = data[0] >> 2;
- if ((value & 0x03) != 0x01)
- {
- return false;
- }
- signal = -1;
- }
- else if ((data[0] & (0x3 << 4)) != 0)
- {
- int32_t value = data[0] >> 4;
- if ((value & 0x03) != 0x01)
- {
- return false;
- }
- signal = 1;
- }
-
- xvalue = ((uint16_t)data[1]) & 0x00FF;
- xvalue <<= 2;
- lvalue = ((uint16_t)(data[0] >> 6)) & 0x0003;
- xvalue |= lvalue;
- _bucket = xvalue * signal;
- std::string str = "arm:";
- str +=std::to_string(_arm)+", bucket :"+ std::to_string(_bucket);
- str += "\n";
- OutputDebugStringA(str.c_str());
- return true;
- }
- ControlStatus CControlSensor::CheckStatus()
- {
- if (_gear != RemoNet::Gears::N) return ControlStatus::GearNotN;
- if (_bootstrap != false) return ControlStatus::BootStrap;
- if (_startup != false) return ControlStatus::Startup;
-
- if (_steer != 0) return ControlStatus::Steer;
- if (abs(_throttle) > 5) return ControlStatus::Throttle;
- if (abs(_brake) > 5) return ControlStatus::Brake;
- return ControlStatus::Ok;
- }
- void CControlSensor::OnBootstrapd(bool ret)
- {
- _bootstrapd = ret;
- }
- void CControlSensor::OnStartupd(bool ret)
- {
- _startupd = ret;
- }
- void CControlSensor::Start()
- {
- }
- void CControlSensor::Stop()
- {
- }
- void CControlSensor::SetSensorSocket(SensorSocket<CControlSensor>* can)
- {
- }
|