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;
- // char str[64];
- // ZeroMemory(str, sizeof(str));
- // std::string ret;
- for (int32_t i = 0; i < receivedCnt; i++)
- {
- int32_t id = ntohl(p->canid) & 0x0FFF;
-
- switch (id)
- {
- case 0x181:
- {
- OnSignal(p->data);
- //if (b == true)
- _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;
- }
- //std::string str = std::to_string(id) +"\n";
- }
- 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)
- {
- /*std::string str;
- char buffer[128];
- for (int i = 0; i < 8; i++)
- {
- int16_t t = (data[i]) & 0x00FF;
- sprintf_s(buffer, "%2x ", t);
- str += buffer;
- }
- str += "\n";
- OutputDebugStringA(str.c_str());
- */
- /*
- ret = (data[0] & 0x01) != 0;
- if (ret == true)
- {
- if (_bootstrap == false)
- {
- if (_window->GetControlState() == ControlState::Process)
- {
-
- _bootstrap = true;
- RemoNet::CCBootStrapReq Req;
- MessageHead Head;
- CIOBuffer pBuffer;
- Head.Command = RemoNet::CC_BootReq;
- Head.Length = Req.ByteSizeLong();
- Head.Serialize(pBuffer.Buffer);
- auto ptr = pBuffer.Buffer + MessageHead::Size();
- Req.SerializeToArray(ptr, Head.Length);
- pBuffer.Length = MessageHead::Size() + Head.Length;
- _window->SendData(&pBuffer);
- }
- }
- }
- ret = (data[0] & 0x02) != 0;
- if (ret == true)
- {
- if (!_startup && _bootstrapd)
- {
- if (_window->GetControlState() == ControlState::Process)
- {
- _startup = true;
- RemoNet::CCStartupReq Req;
- MessageHead Head;
- CIOBuffer pBuffer;
- Head.Command = RemoNet::CC_StartupReq;
- Head.Length = Req.ByteSizeLong();
- Head.Serialize(pBuffer.Buffer);
- auto ptr = pBuffer.Buffer + MessageHead::Size();
- Req.SerializeToArray(ptr, Head.Length);
- pBuffer.Length = MessageHead::Size() + Head.Length;
- _window->SendData(&pBuffer);
- }
- }
- }
- */
- /*if (data[0] == 0)
- {
- if (_front_on == true || _back_on == true)
- {
- _reseted = true;
- }
- }
- */
- bool ret = (data[0] & 0x10) != 0; //Ç°³µµÈ
- //if (ret == true)
- {
- _front_on = (data[0] & 0x10) != 0;
- //if (_window->GetControlState() == ControlState::Process)
- /* {
- if (_reseted == true)
- {
- _front_on = !_front_on;
- _reseted = false;
- }
-
- }*/
- }
- ret = (data[0] & 0x8) != 0; //ºó³µµÆ
- //if (ret == true)
- {
- _back_on = (data[0] & 0x8) != 0;
- /* if (_window->GetControlState() == ControlState::Process)
- {
- if (_reseted == true)
- {
- _back_on = !_back_on;
- _reseted = false;
- }
-
- }
- */
- }
-
- // if (ret == true)
- // {
- if (_window->GetControlState()==ControlState::Process)
- {
- _wiper_on = (data[1] & 0x1) != 0;
- /*
- RemoNet::Wiper req;
- req.set_ret(_front_on);
- CIOBuffer pBuffer;
- MessageHead Head;
- Head.Command = RemoNet::CC_Wiper;
- Head.Length = req.ByteSizeLong();
- Head.Serialize(pBuffer.Buffer);
- auto ptr = pBuffer.Buffer + MessageHead::Size();
- req.SerializeToArray(ptr, Head.Length);
- pBuffer.Length = MessageHead::Size() + Head.Length;
- _window->SendData(&pBuffer);
- */
- }
- //}
- _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;
- /*
- if (signal == -1)
- {
- _steer = xvalue + 1000;
- }
- else
- _steer = xvalue;
- */
-
- //OutputDebugString(std::to_string(_steer).c_str());
- /*
- char buffer[64];
- std::string ret;
- for (int i = 0; i < 8; i += 2)
- {
- int16_t value = (data[5] >> i) & 0x3;
- sprintf_s(buffer, "%x ", value);
- ret += buffer;
- }
- */
- _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;
- }
- // for (int i = 0; i < 8; i += 2)
- // {
- // int16_t value = (data[6] >> i) & 0x3;
- // sprintf_s(buffer, "%x ", value);
- // ret += buffer;
- // }
- // for (int i = 5; i < 8; i++)
- // {
- // int16_t t = (data[i]) & 0x00FF;
- // sprintf_s(buffer, "%x ", t);
- // ret += buffer;
- // }
-
- return true;
- }
- bool CControlSensor::OnRightJoyStick(int8_t* data)
- {
- /*
- std::string str;
- char buffer[128];
- for (int i = 0; i < 8; i++)
- {
- int16_t t = (data[i]) & 0x00FF;
- sprintf_s(buffer, "%2x ", t);
- str += buffer;
- }
- str += "\n";
- OutputDebugStringA(str.c_str());
- */
- /*
- _side = OpSide::Empty;
- if (((data[5] >> 2) & 0x3) == 0x01)
- {
- _side = OpSide::Arm;
-
- }
- else if (((data[5] >> 4) & 0x3) == 0x01)
- {
-
- _side = OpSide::Bucket;
- }
- */
- int32_t signal = 0;
- /*if ((data[2] & 0x3) != 0)
- {
- if ((data[2] & 0x03) != 0x01)
- {
- return false;
- }
- 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 (_side == OpSide::Arm)
- // {
-
- // _bucket = 0;
- // OutputDebugStringA(std::to_string(_arm).c_str());
- // OutputDebugStringA("\n");
- // }
- // else if (_side == OpSide::Bucket)
- /* {
- _bucket = xvalue * signal;
- _arm = 0;
- // OutputDebugStringA(std::to_string(_bucket).c_str());
- // OutputDebugStringA("\n");
- }
- */
- 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 (_emergenyd != false) return ControlStatus::Emergency;
- if (_steer != 0) return ControlStatus::Steer;
- if (abs(_throttle) > 5) return ControlStatus::Throttle;
- if (abs(_brake) > 5) return ControlStatus::Brake;
- // if (_side != OpSide::Empty) return ControlStatus::ArmTriger;
- 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)
- {
- }
|