7 changed files with 446 additions and 33 deletions
@ -0,0 +1,173 @@ |
|||||
|
#include "ServoController.h" |
||||
|
|
||||
|
|
||||
|
ServoController::ServoController() |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
/*************************************************************************************************/ |
||||
|
ModbusConfig ServoController::initiateConfig() |
||||
|
{ |
||||
|
ModbusConfig _config; |
||||
|
_config.baud =QSerialPort::Baud19200; |
||||
|
_config.parity = QSerialPort::EvenParity; |
||||
|
_config.dataBits = QSerialPort::Data8; |
||||
|
_config.stopBits = QSerialPort::OneStop; |
||||
|
_config.responseTime = 2000; |
||||
|
return _config; |
||||
|
|
||||
|
} |
||||
|
|
||||
|
/*************************************************************************************************/ |
||||
|
void ServoController::apply() |
||||
|
{ |
||||
|
_modbusWrapper.setSingleRegister(SRV_APPLY_SETTING_REG, SRV_APPLY_SETTING_VAL); |
||||
|
} |
||||
|
|
||||
|
/*************************************************************************************************/ |
||||
|
void ServoController::enableDrive() |
||||
|
{ |
||||
|
_modbusWrapper.setSingleRegister(SRV_ENABLE_DRIVE_REG, 3); |
||||
|
apply(); |
||||
|
} |
||||
|
|
||||
|
void ServoController::setStartStop(bool start) |
||||
|
{ |
||||
|
if(start) |
||||
|
_modbusWrapper.setSingleRegister(SRV_START_STOP_REG, 3); |
||||
|
else |
||||
|
_modbusWrapper.setSingleRegister(SRV_START_STOP_REG, 0); |
||||
|
apply(); |
||||
|
} |
||||
|
|
||||
|
/*************************************************************************************************/ |
||||
|
void ServoController::setStartAngle(double angle) |
||||
|
{ |
||||
|
_modbusWrapper.setSingleRegister(SRV_START_ANGLE_INT_PART_REG, static_cast<quint16>(angle)); |
||||
|
_modbusWrapper.setSingleRegister(SRV_START_ANGLE_FRAC_PART_REG, static_cast<quint16>((angle - (static_cast<int>(angle))) * 100)); |
||||
|
} |
||||
|
|
||||
|
/*************************************************************************************************/ |
||||
|
void ServoController::setStopAngle(double angle) |
||||
|
{ |
||||
|
_modbusWrapper.setSingleRegister(SRV_STOP_ANGLE_INT_PART_REG, static_cast<quint16>(angle)); |
||||
|
_modbusWrapper.setSingleRegister(SRV_STOP_ANGLE_FRAC_PART_REG, static_cast<quint16>((angle - (static_cast<int>(angle))) * 100)); |
||||
|
} |
||||
|
|
||||
|
/*************************************************************************************************/ |
||||
|
void ServoController::setSpeed(double speed) |
||||
|
{ |
||||
|
_modbusWrapper.setSingleRegister(SRV_AZIMUTH_SPEED_REG, static_cast<quint16>(speed*SRV_SPEED_FACTOR)); |
||||
|
} |
||||
|
|
||||
|
/*************************************************************************************************/ |
||||
|
void ServoController::changeMode(double startAngle, double stopAngle, double speed) |
||||
|
{ |
||||
|
setStartAngle(startAngle); |
||||
|
setStopAngle(stopAngle); |
||||
|
setSpeed(speed); |
||||
|
setStartStop(true); |
||||
|
enableDrive(); |
||||
|
apply(); |
||||
|
} |
||||
|
|
||||
|
/*************************************************************************************************/ |
||||
|
void ServoController::init() |
||||
|
{ |
||||
|
_modbusWrapper.init(); |
||||
|
_modbusWrapper.connectToDevice(initiateConfig()); |
||||
|
|
||||
|
} |
||||
|
|
||||
|
/*************************************************************************************************/ |
||||
|
void ServoController::startMoving() |
||||
|
{ |
||||
|
setStartStop(true); |
||||
|
} |
||||
|
|
||||
|
/*************************************************************************************************/ |
||||
|
void ServoController::stopMoving() |
||||
|
{ |
||||
|
setStartStop(false); |
||||
|
} |
||||
|
|
||||
|
/*************************************************************************************************/ |
||||
|
void ServoController::resetAzimuth() |
||||
|
{ |
||||
|
_modbusWrapper.setSingleRegister(SRV_CALIBRATE_REG, SRV_CALIBRATION_VAL); |
||||
|
} |
||||
|
|
||||
|
/*************************************************************************************************/ |
||||
|
void ServoController::fix(double angle) |
||||
|
{ |
||||
|
changeMode(angle, angle, 5/6.0); |
||||
|
} |
||||
|
|
||||
|
/*************************************************************************************************/ |
||||
|
void ServoController::sector(double startAngle, double stopAngle, double speed) |
||||
|
{ |
||||
|
changeMode(startAngle, stopAngle, speed); |
||||
|
} |
||||
|
|
||||
|
/*************************************************************************************************/ |
||||
|
double ServoController::getSpeed() |
||||
|
{ |
||||
|
double speed; |
||||
|
QVector<quint16> speedVector; |
||||
|
speedVector = _modbusWrapper.getHoldingRegister(SRV_AZIMUTH_SPEED_REG,1); |
||||
|
speed = speedVector[0] / SRV_SPEED_FACTOR; |
||||
|
return speed; |
||||
|
} |
||||
|
|
||||
|
/*************************************************************************************************/ |
||||
|
double ServoController::getStopAngle() |
||||
|
{ |
||||
|
double speed; |
||||
|
QVector<quint16> speedVector1, speedVector2; |
||||
|
speedVector1 = _modbusWrapper.getHoldingRegister(SRV_STOP_ANGLE_INT_PART_REG,1); |
||||
|
speedVector2 = _modbusWrapper.getHoldingRegister(SRV_STOP_ANGLE_FRAC_PART_REG,1); |
||||
|
speed = (speedVector1[0] + speedVector2[0]) / SRV_ANGLE_FACTOR; |
||||
|
return speed; |
||||
|
} |
||||
|
|
||||
|
/*************************************************************************************************/ |
||||
|
double ServoController::getStartAngle() |
||||
|
{ |
||||
|
double speed; |
||||
|
QVector<quint16> speedVector1, speedVector2; |
||||
|
speedVector1 = _modbusWrapper.getHoldingRegister(SRV_START_ANGLE_INT_PART_REG,1); |
||||
|
speedVector2 = _modbusWrapper.getHoldingRegister(SRV_START_ANGLE_FRAC_PART_REG,1); |
||||
|
speed = (speedVector1[0] + speedVector2[0]) / SRV_ANGLE_FACTOR; |
||||
|
return speed; |
||||
|
} |
||||
|
|
||||
|
/*************************************************************************************************/ |
||||
|
double ServoController::getAngleOffset() |
||||
|
{ |
||||
|
double angleOffset; |
||||
|
QVector<quint16> angleOffsetVector; |
||||
|
angleOffsetVector = _modbusWrapper.getHoldingRegister(SRV_ZERO_OFFSET_ANGLE,1); |
||||
|
angleOffset = angleOffsetVector[0] / SRV_ANGLE_FACTOR; |
||||
|
return angleOffset; |
||||
|
|
||||
|
} |
||||
|
|
||||
|
/*************************************************************************************************/ |
||||
|
void ServoController::setAngleOffset(double offset) |
||||
|
{ |
||||
|
_modbusWrapper.setSingleRegister(SRV_ZERO_OFFSET_ANGLE, static_cast<quint16>(offset*SRV_ANGLE_FACTOR)); |
||||
|
} |
||||
|
|
||||
|
/*************************************************************************************************/ |
||||
|
double ServoController::getAzimuth() |
||||
|
{ |
||||
|
double azimuth; |
||||
|
QVector<quint16> azimuthVector; |
||||
|
azimuthVector = _modbusWrapper.getHoldingRegister(SRV_AZIMUTH_REG,1); |
||||
|
azimuth = azimuthVector[0] / SRV_ANGLE_FACTOR; |
||||
|
return azimuth; |
||||
|
|
||||
|
} |
||||
|
|
||||
|
|
||||
|
|
@ -0,0 +1,44 @@ |
|||||
|
#ifndef MAINWINDOW_H |
||||
|
#define MAINWINDOW_H |
||||
|
|
||||
|
#include <QMainWindow> |
||||
|
|
||||
|
#include "ModbusWrapper.h" |
||||
|
#include "ModbusConfig.h" |
||||
|
#include "ServoController.h" |
||||
|
|
||||
|
QT_BEGIN_NAMESPACE |
||||
|
namespace Ui { class MainWindow; |
||||
|
} |
||||
|
QT_END_NAMESPACE |
||||
|
|
||||
|
class MainWindow : public QMainWindow |
||||
|
{ |
||||
|
Q_OBJECT |
||||
|
|
||||
|
private: |
||||
|
Ui::MainWindow* ui; |
||||
|
ModbusWrapper modbusWrapper; |
||||
|
ModbusConfig configDevice; |
||||
|
ServoController _servoControler; |
||||
|
|
||||
|
public: |
||||
|
MainWindow(QWidget* parent = nullptr); |
||||
|
~MainWindow(); |
||||
|
void printCoilsDataFromClient(QBitArray uiCoils); |
||||
|
void printRegisterDataFromClient(QVector<quint16> uiHoldingRegisters); |
||||
|
|
||||
|
//uncrustify off |
||||
|
private slots: |
||||
|
void on_connect_clicked(); |
||||
|
//uncrustify on |
||||
|
void on_readButton_clicked(); |
||||
|
void on_writeSingleCoil_clicked(); |
||||
|
void on_writeMultiCoil_clicked(); |
||||
|
void on_writeSingleRegister_clicked(); |
||||
|
void on_writeMultiRegister_clicked(); |
||||
|
void on_fix_clicked(); |
||||
|
void on_pushButton_clicked(); |
||||
|
}; |
||||
|
|
||||
|
#endif //MAINWINDOW_H |
Loading…
Reference in new issue