|
@@ -2,7 +2,8 @@
|
|
|
#include <thread>
|
|
|
#include "message_queue.h"
|
|
|
#include <iomanip>
|
|
|
-
|
|
|
+#include <chrono> // 用于时间相关的功能
|
|
|
+#include <thread> // 用于线程操作
|
|
|
|
|
|
#define GET_BIT(x, bit) ((x & (1 << bit)) >> bit)
|
|
|
|
|
@@ -279,30 +280,130 @@ int CCanBusSensor::gearselection(cannet_frame* frame){
|
|
|
|
|
|
cannet_frame& carframe = frame[0];
|
|
|
|
|
|
+ CCanBusSensor::speedlevel(&carframe);
|
|
|
switch (carframe.data[5])
|
|
|
{
|
|
|
- case 0x10:
|
|
|
+ case 0x10: //前进档
|
|
|
return 1;
|
|
|
|
|
|
- case 0x00:
|
|
|
+ case 0x00: //空档
|
|
|
return 2;
|
|
|
|
|
|
- case 0x40:
|
|
|
+ case 0x40: //后退档
|
|
|
return 3;
|
|
|
+
|
|
|
+ default:
|
|
|
+ return -1;
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
+void CCanBusSensor::speedlevel(cannet_frame* frame){
|
|
|
+
|
|
|
+ cannet_frame& gearframe = frame[0];
|
|
|
+
|
|
|
+ switch (gearframe.data[5])
|
|
|
+ {
|
|
|
+ case 0x14: //前进档升档
|
|
|
+ if(speed_flag < 14)
|
|
|
+ speed_flag++;
|
|
|
+ std::this_thread::sleep_for(std::chrono::seconds(2));
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 0x11: //前进档降档
|
|
|
+ if(speed_flag > 11)
|
|
|
+ speed_flag--;
|
|
|
+ std::this_thread::sleep_for(std::chrono::seconds(2));
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 0x44: //后退档升档
|
|
|
+ if(speed_flag < 14)
|
|
|
+ speed_flag++;
|
|
|
+ std::this_thread::sleep_for(std::chrono::seconds(2));
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 0x41: //后退档升档
|
|
|
+ if(speed_flag > 11)
|
|
|
+ speed_flag--;
|
|
|
+ std::this_thread::sleep_for(std::chrono::seconds(2));
|
|
|
+ break;
|
|
|
+
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
void CCanBusSensor::runSpeed(unsigned char input, unsigned char &byte0, unsigned char &byte1) {
|
|
|
+
|
|
|
+ // unsigned int mapped_value;
|
|
|
+ // cout<<"speed_flag before change: "<<speed_flag<<endl;
|
|
|
+ // switch (speed_flag)
|
|
|
+ // {
|
|
|
+ // case 11:
|
|
|
+ // mapped_value = static_cast<unsigned int>(input-2) * 375 / 0x7F;
|
|
|
+ // cout<<"run speed: 11"<<mapped_value<<endl;
|
|
|
+ // break;
|
|
|
+
|
|
|
+ // case 12:
|
|
|
+ // mapped_value = static_cast<unsigned int>(input-2) * 750 / 0x7F;
|
|
|
+ // cout<<"run speed: 12"<<mapped_value<<endl;
|
|
|
+ // break;
|
|
|
+
|
|
|
+ // case 13:
|
|
|
+ // mapped_value = static_cast<unsigned int>(input-2) * 1125 / 0x7F;
|
|
|
+ // cout<<"run speed: 13"<<mapped_value<<endl;
|
|
|
+ // break;
|
|
|
+
|
|
|
+ // case 14:
|
|
|
+ // mapped_value = static_cast<unsigned int>(input-2) * 1500 / 0x7F;
|
|
|
+ // cout<<"run speed: 14"<<mapped_value<<endl;
|
|
|
+ // break;
|
|
|
+
|
|
|
+ // default:
|
|
|
+ // break;
|
|
|
+ // }
|
|
|
+ // cout<<"speed_flag after change: "<<speed_flag<<endl;
|
|
|
+
|
|
|
// 将输入值从 0x00 - 0xFF 映射到 0x0000 - 0x05DC (0 - 1500)
|
|
|
- unsigned int mapped_value = static_cast<unsigned int>(input-2) * 1500 / 0x7F;
|
|
|
- cout<<"run speed: "<<mapped_value<<endl;
|
|
|
+ // 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) {
|
|
|
+
|
|
|
+ // unsigned int mapped_value;
|
|
|
+ // switch (speed_flag)
|
|
|
+ // {
|
|
|
+ // case 11:
|
|
|
+ // mapped_value = static_cast<unsigned int>(input-2) * -375 / 0x7F;
|
|
|
+ // cout<<"back speed: 11"<<mapped_value<<endl;
|
|
|
+ // break;
|
|
|
+
|
|
|
+ // case 12:
|
|
|
+ // mapped_value = static_cast<unsigned int>(input-2) * -750 / 0x7F;
|
|
|
+ // cout<<"back speed: 12"<<mapped_value<<endl;
|
|
|
+ // break;
|
|
|
+
|
|
|
+ // case 13:
|
|
|
+ // mapped_value = static_cast<unsigned int>(input-2) * -1125 / 0x7F;
|
|
|
+ // cout<<"back speed: 13"<<mapped_value<<endl;
|
|
|
+ // break;
|
|
|
+
|
|
|
+ // case 14:
|
|
|
+ // mapped_value = static_cast<unsigned int>(input-2) * -1500 / 0x7F;
|
|
|
+ // cout<<"back speed: 14"<<mapped_value<<endl;
|
|
|
+ // break;
|
|
|
+
|
|
|
+ // default:
|
|
|
+ // break;
|
|
|
+ // }
|
|
|
+
|
|
|
// 将输入值从 0x00 - 0xFF 映射到 -1500 到 0 的范围
|
|
|
int mapped_value = static_cast<int>(input-2) * -1500 / 0x7F;
|
|
|
cout<<"back speed: "<<mapped_value<<endl;
|
|
@@ -325,15 +426,16 @@ void CCanBusSensor::move_run(cannet_frame* frame){
|
|
|
if(carframe.data[4]>0x02 && carframe.data[4]<=0x7F)
|
|
|
{
|
|
|
CCanBusSensor::runSpeed(carframe.data[4],id_111[0],id_111[1]);
|
|
|
+
|
|
|
run_flag = true;
|
|
|
- std::cout<<"油门前进"<<std::endl;
|
|
|
+ // std::cout<<"油门前进"<<std::endl;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
id_111[0] = 0x00;
|
|
|
id_111[1] = 0x00;
|
|
|
run_flag = true;
|
|
|
- std::cout<<"前进油门松开"<<std::endl;
|
|
|
+ // std::cout<<"前进油门松开"<<std::endl;
|
|
|
}
|
|
|
}
|
|
|
else
|
|
@@ -341,7 +443,7 @@ void CCanBusSensor::move_run(cannet_frame* frame){
|
|
|
std::copy(id_init,id_init+8,id_111);
|
|
|
run_flag = false;
|
|
|
|
|
|
- std::cout<<"前进档:刹车"<<std::endl;
|
|
|
+ // std::cout<<"前进档:刹车"<<std::endl;
|
|
|
}
|
|
|
|
|
|
}
|
|
@@ -374,7 +476,7 @@ void CCanBusSensor::move_back(cannet_frame* frame){
|
|
|
CCanBusSensor::backSpeed(carframe.data[4],id_111[0],id_111[1]);
|
|
|
run_flag = true;
|
|
|
|
|
|
- std::cout<<"油门后退"<<std::endl;
|
|
|
+ // std::cout<<"油门后退"<<std::endl;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
@@ -382,7 +484,7 @@ void CCanBusSensor::move_back(cannet_frame* frame){
|
|
|
id_111[1] = 0x00;
|
|
|
run_flag = true;
|
|
|
|
|
|
- std::cout<<"后退油门松开"<<std::endl;
|
|
|
+ // std::cout<<"后退油门松开"<<std::endl;
|
|
|
}
|
|
|
}
|
|
|
else
|
|
@@ -390,7 +492,7 @@ void CCanBusSensor::move_back(cannet_frame* frame){
|
|
|
std::copy(id_init,id_init+8,id_111);
|
|
|
run_flag = false;
|
|
|
|
|
|
- std::cout<<"后退档:刹车"<<std::endl;
|
|
|
+ // std::cout<<"后退档:刹车"<<std::endl;
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -398,7 +500,7 @@ void CCanBusSensor::leftSpeed(unsigned char input, unsigned char &byte0, unsigne
|
|
|
|
|
|
// 将输入值从 0x00 - 0xFA 映射到 0x0000 - 0x020B (0 - 523)
|
|
|
unsigned int mapped_value = static_cast<unsigned int>(input) * 523 / 0xFA;
|
|
|
- cout<<"run speed: "<<mapped_value<<endl;
|
|
|
+ // cout<<"run speed: "<<mapped_value<<endl;
|
|
|
// 提取高八位和低八位
|
|
|
byte0 = static_cast<unsigned char>((mapped_value >> 8) & 0xFF); // 高八位
|
|
|
byte1 = static_cast<unsigned char>(mapped_value & 0xFF); // 低八位
|
|
@@ -408,7 +510,7 @@ void CCanBusSensor::rightSpeed(unsigned char input, unsigned char &byte0, unsign
|
|
|
|
|
|
// 将输入值从 0x00 - 0xFA 映射到 -523 到 0 的范围
|
|
|
int mapped_value = static_cast<int>(input) * -523 / 0xFA;
|
|
|
- cout<<"back speed: "<<mapped_value<<endl;
|
|
|
+ // cout<<"back speed: "<<mapped_value<<endl;
|
|
|
|
|
|
// 将负数调整为 0xFA24 ( -1500) 到 0x0000 的正数范围
|
|
|
unsigned short adjusted_value = static_cast<unsigned short>(mapped_value + 0x10000);
|
|
@@ -491,21 +593,22 @@ void CCanBusSensor::OnMessage(cannet_frame* frames, int32_t count) //下发控
|
|
|
{
|
|
|
case 1:
|
|
|
CCanBusSensor::move_run(&frame);
|
|
|
- cout<<"case 1:前进档"<<endl;
|
|
|
+ // cout<<"case 1:前进档"<<endl;
|
|
|
break;
|
|
|
|
|
|
case 2:
|
|
|
CCanBusSensor::move_neutral(&frame);
|
|
|
- cout<<"case 2:空档"<<endl;
|
|
|
+ // cout<<"case 2:空档"<<endl;
|
|
|
break;
|
|
|
|
|
|
case 3:
|
|
|
CCanBusSensor::move_back(&frame);
|
|
|
- cout<<"case 3:后退档"<<endl;
|
|
|
+ // cout<<"case 3:后退档"<<endl;
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
- cout<<"gear error"<<endl;
|
|
|
+ // cout<<"gear error"<<endl;
|
|
|
+ cout<<"升档降档"<<endl;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
@@ -532,11 +635,11 @@ void CCanBusSensor::OnMessage(cannet_frame* frames, int32_t count) //下发控
|
|
|
Sendframe[3].can_id = 0x111;
|
|
|
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;
|
|
|
+ // 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()
|