|
@@ -1,7 +1,7 @@
|
|
|
#include "can_bus.h"
|
|
|
#include <thread>
|
|
|
#include "message_queue.h"
|
|
|
-
|
|
|
+#include <iomanip>
|
|
|
|
|
|
|
|
|
#define GET_BIT(x, bit) ((x & (1 << bit)) >> bit)
|
|
@@ -235,7 +235,7 @@ bool CCanBusSensor::car_stop(cannet_frame* frame){
|
|
|
switch (carframe.data[2])
|
|
|
{
|
|
|
case 0x00:
|
|
|
- *id_111 = *car_stop;
|
|
|
+ std::copy(id_init,id_init+8,id_111);
|
|
|
stop_flag = true;
|
|
|
|
|
|
break;
|
|
@@ -278,96 +278,138 @@ void CCanBusSensor::toggleLamp(cannet_frame* frame){
|
|
|
int CCanBusSensor::gearselection(cannet_frame* frame){
|
|
|
|
|
|
cannet_frame& carframe = frame[0];
|
|
|
+
|
|
|
switch (carframe.data[5])
|
|
|
{
|
|
|
case 0x10:
|
|
|
return 1;
|
|
|
- break;
|
|
|
|
|
|
case 0x00:
|
|
|
return 2;
|
|
|
- break;
|
|
|
|
|
|
case 0x40:
|
|
|
return 3;
|
|
|
- break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-void CCanBusSensor::gear_act(int gear){
|
|
|
|
|
|
- switch (gear)
|
|
|
- {
|
|
|
- case 1:
|
|
|
- id_633[0] =
|
|
|
- }
|
|
|
+void CCanBusSensor::runSpeed(unsigned char input, unsigned char &byte0, unsigned char &byte1) {
|
|
|
+
|
|
|
+ unsigned int mapped_value = static_cast<unsigned int>(input-2) * 1500 / 0x7F;
|
|
|
+ cout<<"run speed: "<<mapped_value<<endl;
|
|
|
+
|
|
|
+ byte0 = static_cast<unsigned char>((mapped_value >> 8) & 0xFF);
|
|
|
+ byte1 = static_cast<unsigned char>(mapped_value & 0xFF);
|
|
|
+}
|
|
|
+
|
|
|
+void CCanBusSensor::backSpeed(unsigned char input, unsigned char &byte0, unsigned char &byte1) {
|
|
|
+
|
|
|
+ int mapped_value = static_cast<int>(input-2) * -1500 / 0x7F;
|
|
|
+ cout<<"back speed: "<<mapped_value<<endl;
|
|
|
|
|
|
+
|
|
|
+ unsigned short adjusted_value = static_cast<unsigned short>(mapped_value + 0x10000);
|
|
|
+
|
|
|
+
|
|
|
+ byte0 = static_cast<unsigned char>((adjusted_value >> 8) & 0xFF);
|
|
|
+ byte1 = static_cast<unsigned char>(adjusted_value & 0xFF);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
void CCanBusSensor::move_run(cannet_frame* frame){
|
|
|
|
|
|
- cannet_frame& carframe = frame[0];
|
|
|
+ cannet_frame& carframe = frame[0];
|
|
|
|
|
|
- switch (gearselection)
|
|
|
+ if(!(carframe.data[5]>0x02 && carframe.data[5]<=0x7F))
|
|
|
+ {
|
|
|
if(carframe.data[4]>0x02 && carframe.data[4]<=0x7F)
|
|
|
{
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- id_111[0] = 0x00;
|
|
|
- id_111[1] = 0x96;
|
|
|
-
|
|
|
- run_flag = true;
|
|
|
+ CCanBusSensor::runSpeed(carframe.data[4],id_111[0],id_111[1]);
|
|
|
std::cout<<"油门前进"<<std::endl;
|
|
|
}
|
|
|
- else if(carframe.data[5]>0x02 && carframe.data[5]<=0x7F)
|
|
|
+ else
|
|
|
{
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
id_111[0] = 0x00;
|
|
|
id_111[1] = 0x00;
|
|
|
-
|
|
|
- run_flag = true;
|
|
|
- std::cout<<"刹车后退"<<std::endl;
|
|
|
+ std::cout<<"前进油门松开"<<std::endl;
|
|
|
}
|
|
|
-
|
|
|
- else { id_111[0] = 0x00; id_111[1] = 0x00; }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ id_111[0] = 0x00;
|
|
|
+ id_111[1] = 0x00;
|
|
|
+ id_111[2] = 0x00;
|
|
|
+ id_111[3] = 0x00;
|
|
|
+ std::cout<<"前进档:刹车"<<std::endl;
|
|
|
+ }
|
|
|
|
|
|
}
|
|
|
|
|
|
-void CCanBusSensor::move_rot(cannet_frame* frame){
|
|
|
|
|
|
- cannet_frame& carframe = frame[0];
|
|
|
+void CCanBusSensor::move_back(cannet_frame* frame){
|
|
|
|
|
|
- switch(carframe.data[0])
|
|
|
+ cannet_frame& carframe = frame[0];
|
|
|
+
|
|
|
+ if(!(carframe.data[5]>0x02 && carframe.data[5]<=0x7F))
|
|
|
+ {
|
|
|
+ if(carframe.data[4]>0x02 && carframe.data[4]<=0x7F)
|
|
|
{
|
|
|
- case 0x01:
|
|
|
-
|
|
|
- id_111[2] = 0x00;
|
|
|
- id_111[3] = 0x00;
|
|
|
-
|
|
|
-
|
|
|
- break;
|
|
|
+ CCanBusSensor::backSpeed(carframe.data[4],id_111[0],id_111[1]);
|
|
|
+ std::cout<<"油门后退"<<std::endl;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ id_111[0] = 0x00;
|
|
|
+ id_111[1] = 0x00;
|
|
|
+
|
|
|
+ std::cout<<"后退油门松开"<<std::endl;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ id_111[0] = 0x00;
|
|
|
+ id_111[1] = 0x00;
|
|
|
+ id_111[2] = 0x00;
|
|
|
+ id_111[3] = 0x00;
|
|
|
+ std::cout<<"后退档:刹车"<<std::endl;
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
- case 0x04:
|
|
|
|
|
|
- id_111[2] = 0x00;
|
|
|
- id_111[3] = 0xc8;
|
|
|
+void CCanBusSensor::move_rot(cannet_frame* frame){
|
|
|
|
|
|
- rot_flag = true;
|
|
|
-
|
|
|
- break;
|
|
|
+ cannet_frame& carframe = frame[0];
|
|
|
|
|
|
- case 0x10:
|
|
|
+ if(!stop_flag)
|
|
|
+ {
|
|
|
+ switch(carframe.data[0])
|
|
|
+ {
|
|
|
+ case 0x01:
|
|
|
+
|
|
|
+ id_111[2] = 0x00;
|
|
|
+ id_111[3] = 0x00;
|
|
|
+
|
|
|
+ break;
|
|
|
|
|
|
- id_111[2] = 0xFF;
|
|
|
- id_111[3] = 0x38;
|
|
|
+ case 0x04:
|
|
|
|
|
|
- rot_flag = true;
|
|
|
-
|
|
|
- break;
|
|
|
+ id_111[2] = 0x00;
|
|
|
+ id_111[3] = 0xc8;
|
|
|
+
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 0x10:
|
|
|
|
|
|
+ id_111[2] = 0xFF;
|
|
|
+ id_111[3] = 0x38;
|
|
|
+
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ id_111[2] = 0x00;
|
|
|
+ id_111[3] = 0x00;
|
|
|
}
|
|
|
|
|
|
}
|
|
@@ -381,17 +423,17 @@ void CCanBusSensor::move_rot(cannet_frame* frame){
|
|
|
void CCanBusSensor::OnMessage(cannet_frame* frames, int32_t count)
|
|
|
{
|
|
|
|
|
|
- for (int32_t i = count; i > 0; i--)
|
|
|
+ for (int32_t i = 3; i >= 0; i--)
|
|
|
{
|
|
|
cannet_frame& frame = frames[i];
|
|
|
|
|
|
int32_t canid = frame.canid;
|
|
|
-
|
|
|
+ Sendframe[3].can_dlc = frame.dlc;
|
|
|
|
|
|
CCanBusSensor::order(&frame);
|
|
|
switch (canid)
|
|
|
{
|
|
|
- case 0x181:
|
|
|
+ case 0x181:
|
|
|
|
|
|
|
|
|
|
|
@@ -406,33 +448,60 @@ void CCanBusSensor::OnMessage(cannet_frame* frames, int32_t count)
|
|
|
if(!CCanBusSensor::car_stop(&frame))
|
|
|
{
|
|
|
|
|
|
- CCanBusSensor::move_run(&frame);
|
|
|
+ switch (gear)
|
|
|
+ {
|
|
|
+ case 1:
|
|
|
+ CCanBusSensor::move_run(&frame);
|
|
|
+ cout<<"case 1:前进档"<<endl;
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 2:
|
|
|
+ id_111[0] = 0x00;
|
|
|
+ id_111[1] = 0x00;
|
|
|
+ cout<<"case 2:空档"<<endl;
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 3:
|
|
|
+ CCanBusSensor::move_back(&frame);
|
|
|
+ cout<<"case 3:后退档"<<endl;
|
|
|
+ break;
|
|
|
+
|
|
|
+ default:
|
|
|
+ cout<<"gear error"<<endl;
|
|
|
+ break;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 0x0CFDD633:
|
|
|
|
|
|
- if(!CCanBusSensor::car_stop(&frame))
|
|
|
+ if(!(CCanBusSensor::car_stop(&frame)))
|
|
|
{
|
|
|
|
|
|
- int gear = CCanBusSensor::gearselection(&frame);
|
|
|
- CCanBusSensor::gear_act(gear);
|
|
|
+ gear = CCanBusSensor::gearselection(&frame);
|
|
|
+ cout<<"档位:"<<gear<<endl;
|
|
|
CCanBusSensor::move_rot(&frame);
|
|
|
}
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
-
|
|
|
default:
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
Sendframe[3].can_id = 0x111;
|
|
|
- Sendframe[3].can_dlc = frame.dlc;
|
|
|
- memcpy(Sendframe[3].data,id_111,frame.dlc);
|
|
|
- cout<<"id_111: "<<static_cast<int>(id_111[0])<<static_cast<int>(id_111[1])<<static_cast<int>(id_111[2])<<static_cast<int>(id_111[3])<<endl;
|
|
|
+
|
|
|
+
|
|
|
+ std::copy(id_111,id_111+8,Sendframe[3].data);
|
|
|
+
|
|
|
+ std::cout << "Array contents: ";
|
|
|
+ for (unsigned char value : id_111) {
|
|
|
+ std::cout << "0x" << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(value) << " ";
|
|
|
+ }
|
|
|
+ std::cout << std::endl;
|
|
|
+
|
|
|
}
|
|
|
|
|
|
void CCanBusSensor::SendStatusToMSG()
|