Files
gcs-nf/App/ToolsUI/RemoteControl/remotecontrol.cpp
T
2021-07-09 18:26:47 +08:00

432 lines
12 KiB
C++

#include "remotecontrol.h"
RemoteControl::RemoteControl(QWidget *parent) :
ToolsWidget(parent),
joystick_left(new JoyStick(this)),
joystick_right(new JoyStick(this)),
PowerSwitch(new Switch(this)),
AUX5Switch(new Switch(this)),
AUX6Switch(new Switch(this)),
AUX7Switch(new Switch(this)),
AUX8Switch(new Switch(this))
{
this->setTop(true);
resize(600,400);
setMinimumSize(600,400);
timer = new QTimer();
timer->setInterval(100);
connect(timer,&QTimer::timeout,
this,&RemoteControl::timerout);
joystick_left->setGeometry(0,
this->height()/2 - joystick_left->height()/2,
joystick_left->width(),
joystick_left->height());
joystick_right->setGeometry(this->width() - joystick_right->width(),
this->height()/2 - joystick_right->height()/2,
joystick_right->width(),
joystick_right->height());
//设置正反向
joystick_left->setDirection(0,NORMAL);
joystick_left->setDirection(1,NORMAL);
joystick_right->setDirection(0,NORMAL);
joystick_right->setDirection(1,REVERSE);
if(mode == RightHand)
{
joystick_left->setRecovery(0,true);//方向
joystick_left->setRecovery(1,true);//升降
joystick_right->setRecovery(0,true);//副翼
joystick_right->setRecovery(1,false);//油门
}
else if(mode == LeftHand)
{
joystick_left->setRecovery(0,true);//方向
joystick_left->setRecovery(1,false);//油门
joystick_right->setRecovery(0,true);//副翼
joystick_right->setRecovery(1,true);//升降
}
/*
//微调
TrimSwitch_right0->setSegment(3);
TrimSwitch_right0->setPosition(1);
TrimSwitch_right0->setSwitchdir(1);
TrimSwitch_right0->setRecovery(true);
TrimSwitch_right0->setGeometry(joystick_right->x(),
joystick_right->y() + joystick_right->height(),
joystick_right->width(),
TrimSwitch_right0->height());
//qDebug() << TrimSwitch_aileron->height();
TrimSwitch_right1->setSegment(3);
TrimSwitch_right1->setPosition(1);
TrimSwitch_right1->setSwitchdir(0);
TrimSwitch_right1->setRecovery(true);
TrimSwitch_right1->setGeometry(joystick_right->x() - TrimSwitch_right1->width(),
joystick_right->y(),
TrimSwitch_right1->width(),
joystick_right->height());
TrimSwitch_left1->setSegment(3);
TrimSwitch_left1->setPosition(1);
TrimSwitch_left1->setSwitchdir(0);
TrimSwitch_left1->setRecovery(true);
TrimSwitch_left1->setGeometry(joystick_left->width(),
joystick_left->y(),
TrimSwitch_left1->width(),
joystick_left->height());
TrimSwitch_left0->setSegment(3);
TrimSwitch_left0->setPosition(1);
TrimSwitch_left0->setSwitchdir(1);
TrimSwitch_left0->setRecovery(true);
TrimSwitch_left0->setGeometry(joystick_left->x(),
joystick_left->y() + joystick_left->height(),
joystick_left->width(),
TrimSwitch_left0->height());
*/
//辅助按钮
AUX5Switch->setSegment(3);
AUX5Switch->setPosition(1);
AUX5Switch->setSwitchdir(0);
AUX5Switch->setGeometry(joystick_left->x(),
joystick_left->y() - AUX5Switch->height(),
AUX5Switch->width(),
AUX5Switch->height());
AUX6Switch->setSegment(3);
AUX6Switch->setPosition(1);
AUX6Switch->setSwitchdir(0);
AUX6Switch->setGeometry(joystick_left->x() + joystick_left->width() - AUX6Switch->width(),
joystick_left->y() - AUX6Switch->height(),
AUX6Switch->width(),
AUX6Switch->height());
AUX7Switch->setSegment(3);
AUX7Switch->setPosition(1);
AUX7Switch->setSwitchdir(0);
AUX7Switch->setGeometry(joystick_right->x(),
joystick_right->y() - AUX7Switch->height(),
AUX7Switch->width(),
AUX7Switch->height());
AUX8Switch->setSegment(3);
AUX8Switch->setPosition(1);
AUX8Switch->setSwitchdir(0);
AUX8Switch->setGeometry(joystick_right->x() + joystick_right->width() - AUX8Switch->width(),
joystick_right->y() - AUX8Switch->height(),
AUX8Switch->width(),
AUX8Switch->height());
//电源开关
PowerSwitch->resize(50,100);
PowerSwitch->setGeometry(this->width()/2 - PowerSwitch->width()/2,
joystick_left->y(),
PowerSwitch->width(),
PowerSwitch->height());
PowerSwitch->setSegment(3);
PowerSwitch->setPosition(3);
PowerSwitch->setNotice(0,tr("Soft"));
PowerSwitch->setNotice(1,tr("Remote"));
PowerSwitch->setNotice(2,tr("OFF"));
//底部标签
remotelabel = new QLabel(tr("remote"),this);
remotelabel->resize(this->width(),30);
remotelabel->raise();
remotelabel->setGeometry(0,
joystick_left->y() + joystick_left->height(),
this->width(),
30);
connect(joystick_left,SIGNAL(positionChange(uint16_t,uint16_t)),
this,SLOT(Left_stickUpdate(uint16_t,uint16_t)));
connect(joystick_right,SIGNAL(positionChange(uint16_t,uint16_t)),
this,SLOT(Right_stickUpdate(uint16_t,uint16_t)));
connect(AUX5Switch,SIGNAL(positionChange(uint16_t)),
this,SLOT(AUX5SwitchUpdate(uint16_t)));
connect(AUX6Switch,SIGNAL(positionChange(uint16_t)),
this,SLOT(AUX6SwitchUpdate(uint16_t)));
connect(AUX7Switch,SIGNAL(positionChange(uint16_t)),
this,SLOT(AUX7SwitchUpdate(uint16_t)));
connect(AUX8Switch,SIGNAL(positionChange(uint16_t)),
this,SLOT(AUX8SwitchUpdate(uint16_t)));
connect(PowerSwitch,SIGNAL(positionChange(uint16_t)),
this,SLOT(PowerSwitchUpdate(uint16_t)));
//init
if(mode == RightHand)
{
joystick_left->setValue(0,channel[4]);
joystick_left->setValue(1,channel[2]);
joystick_right->setValue(0,channel[1]);
joystick_right->setValue(1,channel[3]);
}
else if(mode == LeftHand)
{
joystick_left->setValue(0,channel[1]);
joystick_left->setValue(1,channel[3]);
joystick_right->setValue(0,channel[4]);
joystick_right->setValue(1,channel[2]);
}
AUX5Switch->setValue(channel[5]);
AUX6Switch->setValue(channel[6]);
AUX7Switch->setValue(channel[7]);
AUX8Switch->setValue(channel[8]);
}
RemoteControl::~RemoteControl()
{
delete joystick_left;
delete joystick_right;
if(timer)
{
timer->stop();
delete timer;
timer = nullptr;
}
}
void RemoteControl::Left_stickUpdate(uint16_t channel0, uint16_t channel1)
{
if(mode == RightHand)//右手油门
{
channel[4] = channel0;//方向
channel[2] = channel1;//升降
}
else if(mode == LeftHand)//左手油门
{
channel[1] = channel0;//副翼
channel[3] = channel1;//油门
}
stateChanged();
}
void RemoteControl::Right_stickUpdate(uint16_t channel0, uint16_t channel1)
{
if(mode == RightHand)//右手油门
{
channel[1] = channel0;//副翼
channel[3] = channel1;//油门
}
else if(mode == LeftHand)//左手油门
{
channel[4] = channel0;//方向
channel[2] = channel1;//升降
}
stateChanged();
}
void RemoteControl::AUX5SwitchUpdate(uint16_t value)
{
channel[5] = value;
stateChanged();
}
void RemoteControl::AUX6SwitchUpdate(uint16_t value)
{
channel[6] = value;
stateChanged();
}
void RemoteControl::AUX7SwitchUpdate(uint16_t value)
{
channel[7] = value;
stateChanged();
}
void RemoteControl::AUX8SwitchUpdate(uint16_t value)
{
channel[8] = value;
stateChanged();
}
void RemoteControl::PowerSwitchUpdate(uint16_t value)
{
if(value <= 1300)
{
isActive = 0;
isActive = 2;
timer->start();
}
else if(value <= 1600)
{
isActive = 1;
timer->stop();
}
else
{
isActive = 2;
isActive = 0;
timer->stop();
}
//qDebug() << isActive << value;
update();
emit RemoteActive(isActive);
}
void RemoteControl::stateChanged(void)
{
QList<uint16_t> rc;
rc.append(RSSI++);
rc.append(channel[1]);
rc.append(channel[2]);
rc.append(channel[3]);
rc.append(channel[4]);
rc.append(channel[5]);
rc.append(channel[6]);
rc.append(channel[7]);
rc.append(channel[8]);
rc.append(channel[9]);
rc.append(channel[10]);
rc.append(channel[11]);
rc.append(channel[12]);
rc.append(channel[13]);
rc.append(channel[14]);
rc.append(channel[15]);
rc.append(channel[16]);
rc.append(channel[17]);
rc.append(channel[18]);
if(isActive == 2)
{
emit RemoteControlUpdate(rc);
}
remotelabel->raise();
remotelabel->setText(QString("%1:%2:%3:%4:%5:%6:%7:%8:%9:%10")\
.arg(rc[0],5).arg(rc[1],4).arg(rc[2],4)\
.arg(rc[3],4).arg(rc[4],4).arg(rc[5],4)\
.arg(rc[6],4).arg(rc[7],4).arg(rc[8],4).arg(rc[9],4));
}
void RemoteControl::setFixed(bool value)
{
isFixed = value;
if(isFixed == true)
{
//全部都不回中
joystick_left->setRecovery(0,false);
joystick_left->setRecovery(1,false);
joystick_right->setRecovery(0,false);
joystick_right->setRecovery(1,false);
}
else
{
if(mode == RightHand)
{
joystick_left->setRecovery(0,true);//方向
joystick_left->setRecovery(1,true);//升降
joystick_right->setRecovery(0,true);//副翼
joystick_right->setRecovery(1,false);//油门
}
else if(mode == LeftHand)
{
joystick_left->setRecovery(0,true);//方向
joystick_left->setRecovery(1,false);//油门
joystick_right->setRecovery(0,true);//副翼
joystick_right->setRecovery(1,true);//升降
}
}
}
void RemoteControl::setMode(uint8_t value)
{
mode = value;
if(mode == RightHand)
{
joystick_left->setRecovery(0,true);//方向
joystick_left->setRecovery(1,true);//升降
joystick_right->setRecovery(0,true);//副翼
joystick_right->setRecovery(1,false);//油门
}
else if(mode == LeftHand)
{
joystick_left->setRecovery(0,true);//方向
joystick_left->setRecovery(1,false);//油门
joystick_right->setRecovery(0,true);//副翼
joystick_right->setRecovery(1,true);//升降
}
}
void RemoteControl::setChannelOveride(QList<uint16_t> rc)
{
if(isActive == 1)
{
for(int i = 1;i <= 18;i++)
{
channel[i] = rc.at(i);
}
if(mode == RightHand)//右手油门
{
joystick_right->setValue(0,rc.at(1));
joystick_right->setValue(1,rc.at(3));
joystick_left->setValue(0,rc.at(4));
joystick_left->setValue(1,rc.at(2));
}
else if(mode == LeftHand)
{
joystick_left->setValue(0,rc.at(1));
joystick_left->setValue(1,rc.at(3));
joystick_right->setValue(0,rc.at(4));
joystick_right->setValue(1,rc.at(2));
}
AUX5Switch->setValue(rc.at(5));
AUX6Switch->setValue(rc.at(6));
AUX7Switch->setValue(rc.at(7));
AUX8Switch->setValue(rc.at(8));
remotelabel->raise();
remotelabel->setText(QString("%1:%2:%3:%4:%5:%6:%7:%8:%9:%10")\
.arg(rc[0],5).arg(rc[1],4).arg(rc[2],4)\
.arg(rc[3],4).arg(rc[4],4).arg(rc[5],4)\
.arg(rc[6],4).arg(rc[7],4).arg(rc[8],4).arg(rc[9],4));
update();
}
}
void RemoteControl::timerout(void)
{
stateChanged();
}