Skip to content

Commit

Permalink
[Controller] add needReport
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark-tz committed May 15, 2024
1 parent 49b7a5d commit 178f020
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 70 deletions.
91 changes: 56 additions & 35 deletions Controller/src/qml/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,9 @@ import QtGamepad 1.0
import Client.Component 1.0 as Client
ApplicationWindow{
visible:true;
width:580;
width:650;
height:700;
color: "#333"
/*
//固定窗口大小
minimumHeight: height;
minimumWidth: width;
maximumHeight: height;
maximumWidth: width;
*/
id:window;
Client.Serial { id : serial; }
Client.Translator{ id : translator; }
Expand Down Expand Up @@ -156,7 +149,8 @@ ApplicationWindow{
property int velYStep : 20;//VyStep
property int velRStep : 20;//VrStep
property bool mode : false;//KickMode
property int dribbleLevel : 2;//DribLevel
property bool needReport : false;
property int dribbleLevel : 3;//DribLevel
property int rushSpeed : 20;//RushSpeed

property int m_VEL : 255//MaxVel
Expand All @@ -170,15 +164,25 @@ ApplicationWindow{
property int itemWidth : (width-columnSpacing*(columns-1))/columns;

ZText{ text:qsTr("Robot") + translator.emptyString }
//最多12辆车
SpinBox{ minimumValue:0; maximumValue:15; value:parent.robotID; width:parent.itemWidth
onEditingFinished:{parent.robotID = value}}
ZText{ text:"Stop" }
//有用吗?
Button{ text:qsTr("[Space]") + translator.emptyString;width:parent.itemWidth
Button{ text:qsTr("[Space]") + translator.emptyString;width:parent.itemWidth;
onClicked: { crazyShow.updateStop(); }
}
ZText{ text:"Report [R]" }
Button{
text: crazyShow.needReport ? qsTr("True") : qsTr("False") + translator.emptyString;width:parent.itemWidth;
onClicked: {
crazyShow.needReport = !crazyShow.needReport;
}
style: ButtonStyle{
background: Rectangle{
radius: 2
color: crazyShow.needReport ? "#9BF80C" : "lightgrey";
}
}
}
ZText{ text:" " }
ZText{ text:" " }
ZText{ text:qsTr("Vx [W/S]") + translator.emptyString }
//Vx:(-m_VEL, m_VEL)
SpinBox{ minimumValue:-crazyShow.m_VEL; maximumValue:crazyShow.m_VEL; value:parent.velX;width:parent.itemWidth
Expand Down Expand Up @@ -214,14 +218,20 @@ ApplicationWindow{
SpinBox{ minimumValue:1; maximumValue:crazyShow.velocityRMax; value:parent.m_VELR;width:parent.itemWidth
onEditingFinished:{parent.m_VELR = value;}}
ZText{ text:qsTr("Shoot [E]") + translator.emptyString}
Button{ text:(parent.shoot? qsTr("true") : qsTr("false")) + translator.emptyString;width:parent.itemWidth
Button{ text:(parent.shoot? qsTr("True") : qsTr("False")) + translator.emptyString;width:parent.itemWidth
onClicked: {
parent.shoot = !parent.shoot;
}
style: ButtonStyle{
background: Rectangle{
radius: 2
color: crazyShow.shoot ? "#9BF80C" : "lightgrey";
}
}
}

ZText{ text:qsTr("KickMode [Up]") + translator.emptyString}
Button{ text:(parent.mode?qsTr("chip"):qsTr("flat")) + translator.emptyString;width:parent.itemWidth
Button{ text:(parent.mode?qsTr("Chip"):qsTr("Flat")) + translator.emptyString;width:parent.itemWidth
onClicked: {
parent.mode = !parent.mode
}
Expand All @@ -231,31 +241,37 @@ ApplicationWindow{
SpinBox{ minimumValue:0; maximumValue:parent.kickPowerMax; value:parent.power;width:parent.itemWidth
onEditingFinished:{parent.power = value;}}
ZText{ text:qsTr("Dribb [Q]") + translator.emptyString }
Button{ text:(parent.dribble ? qsTr("true") : qsTr("false")) +translator.emptyString;width:parent.itemWidth
Button{ text:(parent.dribble ? qsTr("True") : qsTr("False")) +translator.emptyString;width:parent.itemWidth
onClicked: {
parent.dribble = !parent.dribble;
}
style: ButtonStyle{
background: Rectangle{
radius: 2
color: crazyShow.dribble ? "#9BF80C" : "lightgrey";
}
}
}
ZText{ text:qsTr("DribLevel") + translator.emptyString}
//DribLevel:(0, dribbleMaxLevel)
SpinBox{ minimumValue:0; maximumValue:crazyShow.dribbleMaxLevel; value:parent.dribbleLevel;width:parent.itemWidth
onEditingFinished:{parent.dribbleLevel = value;}}
ZText{ text:" " }
ZText{ text:" " }
ZText{ text:qsTr("Rush [G]") + translator.emptyString }
Button{ text:(parent.rush ? qsTr("true") : qsTr("false")) +translator.emptyString;width:parent.itemWidth;
onClicked: {
parent.rush = !parent.rush;
crazyShow.updateRush();
}
}
ZText{ text:qsTr("RushSpeed") + translator.emptyString}
//RushSpeed:(0, m_VEL)
SpinBox{ minimumValue:0; maximumValue:crazyShow.m_VEL; value:parent.rushSpeed;width:parent.itemWidth
onEditingFinished:{parent.rushSpeed = value;}}
ZText{ text:"Shooting" }
Rectangle{
width:parent.itemWidth; height:20; color:parent.shoot ? "red" : "lightgrey";
}
width:parent.itemWidth; height:24;radius:3; color:parent.shoot ? "#d9534f" : "lightgrey";
}
// ZText{ text:qsTr("Rush [G]") + translator.emptyString }
// Button{ text:(parent.rush ? qsTr("True") : qsTr("False")) +translator.emptyString;width:parent.itemWidth;
// onClicked: {
// parent.rush = !parent.rush;
// crazyShow.updateRush();
// }
// }
// ZText{ text:qsTr("RushSpeed") + translator.emptyString}
// //RushSpeed:(0, m_VEL)
// SpinBox{ minimumValue:0; maximumValue:crazyShow.m_VEL; value:parent.rushSpeed;width:parent.itemWidth
// onEditingFinished:{parent.rushSpeed = value;}}


//键盘响应实现
Keys.onPressed:getFocus(event);
Expand Down Expand Up @@ -293,6 +309,7 @@ ApplicationWindow{
}
function handleKeyboardEvent(e){
switch(e){
case 'r':{crazyShow.needReport = !crazyShow.needReport;break;}
case 'U':{crazyShow.mode = !crazyShow.mode;break;}
case 'a':{crazyShow.velY = crazyShow.limitVel(crazyShow.velY-crazyShow.velYStep,-crazyShow.m_VEL,crazyShow.m_VEL);
break;}
Expand Down Expand Up @@ -322,7 +339,7 @@ ApplicationWindow{
}
//serial.updateCommandParams在C++中实现
function updateCommand(){
serial.updateCommandParams(crazyShow.robotID,crazyShow.velX,crazyShow.velY,crazyShow.velR,crazyShow.dribble,crazyShow.dribbleLevel,crazyShow.mode,crazyShow.shoot,crazyShow.power);
serial.updateCommandParams(crazyShow.robotID,crazyShow.velX,crazyShow.velY,crazyShow.velR,crazyShow.dribble,crazyShow.dribbleLevel,crazyShow.mode,crazyShow.shoot,crazyShow.power,crazyShow.needReport);
}
function updateFromGamepad(){
crazyShow.velX = -parseInt(gamepad.axisLeftY*10)/10.0*crazyShow.m_VEL;
Expand Down Expand Up @@ -358,9 +375,13 @@ ApplicationWindow{
if(vel<minValue) return minValue;
return vel;
}
// Shortcut{
// sequence:"G";
// onActivated:crazyShow.handleKeyboardEvent('g');
// }
Shortcut{
sequence:"G";
onActivated:crazyShow.handleKeyboardEvent('g');
sequence:"R";
onActivated:crazyShow.handleKeyboardEvent('r');
}
Shortcut{
sequence:"A";
Expand Down
62 changes: 32 additions & 30 deletions Controller/src/radiopacket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <QSerialPort>
#include <QElapsedTimer>
#include <cstdio>
#include <thread>
namespace {
struct NJ_Command{
float power;
Expand All @@ -13,6 +14,7 @@ namespace {
int id;
bool valid;
bool kick_mode;
bool need_report;
};
void encodeLegacy(const NJ_Command& command,QByteArray& tx,int num);
}
Expand Down Expand Up @@ -74,19 +76,27 @@ bool RadioPacket::sendCommand(){
static QElapsedTimer timer;
if(times == 0) timer.start();
if(serialPtr != NULL){
encode();
// qDebug() << "0x" << transmitPacket.toHex();
//transmitPacket是包含命令的包
transmitPacket.data();
encode(true);
// //transmitPacket是包含命令的包
serialPtr->write((transmitPacket.data()),TRANSMIT_PACKET_SIZE);
// // qDebug() << "sent 0x" << transmitPacket.toHex();
serialPtr->flush();
std::this_thread::sleep_for(std::chrono::milliseconds(4));
// serialPtr->write((transmitPacket.data()),TRANSMIT_PACKET_SIZE);
// // qDebug() << "sent 0x" << transmitPacket.toHex();
// serialPtr->flush();
encode(false);
std::this_thread::sleep_for(std::chrono::milliseconds(4));
serialPtr->write((transmitPacket.data()),TRANSMIT_PACKET_SIZE);
// qDebug() << "sent 0x" << transmitPacket.toHex();
serialPtr->flush();
// sendSocket.writeDatagram(transmitPacket.data(), 25 ,QHostAddress("10.12.225.78"),1030);
return true;
}
return false;
}

bool RadioPacket::encode(){
bool RadioPacket::encode(bool fake){
NJ_Command NJ_CMD;

NJ_CMD.valid = true;
Expand All @@ -97,38 +107,30 @@ bool RadioPacket::encode(){
NJ_CMD.dribble = this->ctrl ? this->ctrlPowerLevel : 0;
NJ_CMD.power = this->shoot ? this->shootPowerLevel : 0;
NJ_CMD.kick_mode = this->shootMode;
NJ_CMD.need_report = this->needReport;

auto& tx = transmitPacket;
int count = 0;
tx.fill(0x00);
tx[0] = 0xff;
tx[21] = ((this->frequency&0x0f)<<4) | 0x07;

encodeLegacy(NJ_CMD,tx,0);
if(fake){
// NJ_CMD.id = 0;
// encodeLegacy(NJ_CMD,tx,0);
// NJ_CMD.id = 1;
// encodeLegacy(NJ_CMD,tx,1);
// NJ_CMD.id = 2;
// encodeLegacy(NJ_CMD,tx,2);
}else{
// NJ_CMD.id = 3;
encodeLegacy(NJ_CMD,tx,0);
// NJ_CMD.id = 4;
// encodeLegacy(NJ_CMD,tx,1);
// NJ_CMD.id = 5;
// encodeLegacy(NJ_CMD,tx,2);
}

// transmitPacket[0] = packageType | gameStatus;
// //RobotID
// transmitPacket[1] = (robotID) & 0x0f;
// transmitPacket[0] = transmitPacket[0] | 0x08;
// //Robot1 Config
// //shoot or chip
// transmitPacket[1] = transmitPacket[1] | (shootMode << 6 );
// //power level
// transmitPacket[1] = transmitPacket[1] | (ctrl ? (ctrlPowerLevel << 4):0);
// //速度的低位
// transmitPacket[2] = ((velX >= 0)?0:0x80) | (abs(velX) & 0x7f);
// transmitPacket[3] = ((velY >= 0)?0:0x80) | (abs(velY) & 0x7f);
// transmitPacket[4] = ((velR >= 0)?0:0x80) | (abs(velR) & 0x7f);
// //Don't understand !
// if(transmitPacket[2] == char(0xff)) transmitPacket[4] = 0xfe;
// if(transmitPacket[3] == char(0xff)) transmitPacket[5] = 0xfe;
// if(transmitPacket[4] == char(0xff)) transmitPacket[6] = 0xfe;
// //clear Byte[17-24]
// transmitPacket[17] = transmitPacket[18] = transmitPacket[19] = transmitPacket[20] = transmitPacket[21] = transmitPacket[22] = transmitPacket[23] = transmitPacket[24] = 0;
// //速度的高位
// transmitPacket[17] = ((abs(velX) & 0x180) >> 1) | ((abs(velY) & 0x180) >> 3) | ((abs(velR) & 0x780) >> 7);
// //shoot power
// transmitPacket[21] = (shoot ? shootPowerLevel:0) & 0x7f;
return true;
}
namespace{
Expand All @@ -155,7 +157,7 @@ void encodeLegacy(const NJ_Command& command,QByteArray& tx,int num){

TXBuff[2] = TXBuff[2] | 0x01 << real_num;
// dribble & kick_mode
TXBuff[6*i + 3] = 0x01 | ((((kick_mode?0x01:0x00)<<2)|(0x03 & dribble))<<4);
TXBuff[6*i + 3] = (command.need_report?0x08:0x00) | 0x01 | ((((kick_mode?0x01:0x00)<<2)|(0x03 & dribble))<<4);

// velx
if(vx < 0) TXBuff[6*i + 4] = TXBuff[6*i + 4] | (0x20);
Expand Down
6 changes: 4 additions & 2 deletions Controller/src/radiopacket.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ class RadioPacket
explicit RadioPacket(QSerialPort* serialPtr);
bool sendStartPacket();
bool sendCommand();
void updateCommandParams(int robotID,int velX,int velY,int velR,bool ctrl,int ctrlLevel,bool mode,bool shoot,int power){
void updateCommandParams(int robotID,int velX,int velY,int velR,bool ctrl,int ctrlLevel,bool mode,bool shoot,int power,bool needReport){
this->robotID = robotID;
this->velX = velX; this->velY = velY; this->velR = velR;
this->ctrl = ctrl;
this->shootMode = mode;this->shoot = shoot; this->shootPowerLevel = power;
this->ctrlPowerLevel = ctrlLevel;
this->needReport = needReport;
}
void updateFrequency(int);
private:
Expand All @@ -26,7 +27,7 @@ class RadioPacket
QByteArray transmitPacket;
QSerialPort* serialPtr;
int frequency;
bool encode();
bool encode(bool fake=false);
private:
QUdpSocket sendSocket;
bool shoot;
Expand All @@ -42,6 +43,7 @@ class RadioPacket
qint16 velR;
quint16 ctrlPowerLevel;
quint16 shootPowerLevel;
bool needReport;
};

#endif // RADIOPACKET_H
4 changes: 2 additions & 2 deletions Controller/src/serialobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class SerialObject : public QObject
Q_INVOKABLE void closeSerialPort();
Q_INVOKABLE void sendStartPacket();
Q_INVOKABLE void sendCommand(){radioPacket.sendCommand();}
Q_INVOKABLE void updateCommandParams(int robotID,int velX,int velY,int velR,bool dribble,int dribbleLevel,bool mode,bool shoot,int power){
radioPacket.updateCommandParams(robotID,velX,velY,velR,dribble,dribbleLevel,mode,shoot,power);
Q_INVOKABLE void updateCommandParams(int robotID,int velX,int velY,int velR,bool dribble,int dribbleLevel,bool mode,bool shoot,int power,bool needReport){
radioPacket.updateCommandParams(robotID,velX,velY,velR,dribble,dribbleLevel,mode,shoot,power,needReport);
}
void readData();
private:
Expand Down
2 changes: 1 addition & 1 deletion doc/posts/1_rocos_basic/1_0.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(1_0_compile)=
# 编译&运行

## 在Ubuntu 22.04下编译Rocos
## 在Ubuntu 22.04 & 24.04下编译Rocos
* 在安装好系统后,配置源,可以参考[清华源](https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/),选择对应版本后更改**软件源配置文件**`/etc/apt/sources.list`

* 更新源并安装Rocos所需要的依赖
Expand Down

0 comments on commit 178f020

Please sign in to comment.