diff --git a/Servo/include/ServoController.h b/Servo/include/ServoController.h index 36573be..9ea6c9e 100644 --- a/Servo/include/ServoController.h +++ b/Servo/include/ServoController.h @@ -6,49 +6,48 @@ class ServoController { - private: - ModbusWrapper _modbusWrapper; - const int SRV_START_ANGLE_INT_PART_REG = 2000; - const int SRV_STOP_ANGLE_INT_PART_REG = 2001; - const int SRV_AZIMUTH_SPEED_REG = 2002; - const int SRV_APPLY_SETTING_REG = 2004; - const int SRV_START_STOP_REG = 2005; - const int SRV_ZERO_OFFSET_ANGLE = 2007; - const int SRV_START_ANGLE_FRAC_PART_REG = 2010; - const int SRV_STOP_ANGLE_FRAC_PART_REG = 2011; - const int SRV_CALIBRATE_REG = 2013; - const int SRV_ENABLE_DRIVE_REG = 2017; - const int SRV_AZIMUTH_REG = 2030; - - const int SRV_ANGLE_FACTOR = 100; - const int SRV_SPEED_FACTOR = 1000; - const quint16 SRV_APPLY_SETTING_VAL = 1000; - const quint16 SRV_CALIBRATION_VAL = 13576; - - void apply(); - void enableDrive(); - void setStartStop(bool start); - void setStartAngle(double angle); - void setStopAngle(double angle); - void setSpeed(double speed); - void changeMode(double startAngle, double stopAngle, double speed); - ModbusConfig initiateConfig(); + ModbusWrapper _modbusWrapper; + const int SRV_START_ANGLE_INT_PART_REG = 2000; + const int SRV_STOP_ANGLE_INT_PART_REG = 2001; + const int SRV_AZIMUTH_SPEED_REG = 2002; + const int SRV_APPLY_SETTING_REG = 2003;//2004; + const int SRV_START_STOP_REG = 2004; //2005; + const int SRV_ZERO_OFFSET_ANGLE = 2005; //2007; + const int SRV_START_ANGLE_FRAC_PART_REG = 2006; //2010; + const int SRV_STOP_ANGLE_FRAC_PART_REG = 2007; //2011; + const int SRV_CALIBRATE_REG = 2008; //2013; + const int SRV_ENABLE_DRIVE_REG = 2009; //2017; + const int SRV_AZIMUTH_REG = 2010; //2030; + + const double SRV_ANGLE_FACTOR = 100.0; + const double SRV_SPEED_FACTOR = 1000.0; + const quint16 SRV_APPLY_SETTING_VAL = 176; + const quint16 SRV_CALIBRATION_VAL = 13576; + + void apply(); + void enableDrive(); + void setStartStop(bool start); + void setStartAngle(double angle); + void setStopAngle(double angle); + void setSpeed(double speed); + void changeMode(double startAngle, double stopAngle, double speed); + ModbusConfig initiateConfig(QString serialPort); public: - void init(); - void startMoving(); - void stopMoving(); - void resetAzimuth(); - void fix(double angle); - void sector(double startAngle, double stopAngle, double speed); - - double getSpeed(); - double getStopAngle(); - double getStartAngle(); - double getAngleOffset(); - void setAngleOffset(double offset); - double getAzimuth(); + void init(QString serialPort); + void startMoving(); + void stopMoving(); + void resetAzimuth(); + void fix(double angle); + void sector(double startAngle, double stopAngle, double speed); + + double getSpeed(); + double getStopAngle(); + double getStartAngle(); + double getAngleOffset(); + void setAngleOffset(double offset); + double getAzimuth(); }; -#endif // SERVOCONTROLLER_H +#endif //SERVOCONTROLLER_H diff --git a/Servo/src/ServoController.cpp b/Servo/src/ServoController.cpp index cb7627f..6dce790 100644 --- a/Servo/src/ServoController.cpp +++ b/Servo/src/ServoController.cpp @@ -1,143 +1,157 @@ #include "ServoController.h" -ModbusConfig ServoController::initiateConfig() +ModbusConfig ServoController::initiateConfig(QString serialPort) { - ModbusConfig _config; - _config.baud =QSerialPort::Baud19200; - _config.parity = QSerialPort::EvenParity; - _config.dataBits = QSerialPort::Data8; - _config.stopBits = QSerialPort::OneStop; - _config.responseTime = 2000; - return _config; + ModbusConfig _config; + _config.baud = QSerialPort::Baud19200; + _config.parity = QSerialPort::EvenParity; + _config.dataBits = QSerialPort::Data8; + _config.stopBits = QSerialPort::OneStop; + _config.responseTime = 2000; + _config.serialPort = serialPort; + _config.clientAddress = 1; + + return _config; } /*************************************************************************************************/ void ServoController::apply() { - _modbusWrapper.setSingleRegister(SRV_APPLY_SETTING_REG, SRV_APPLY_SETTING_VAL); + _modbusWrapper.setSingleRegister(SRV_APPLY_SETTING_REG, SRV_APPLY_SETTING_VAL); } /*************************************************************************************************/ void ServoController::enableDrive() { - _modbusWrapper.setSingleRegister(SRV_ENABLE_DRIVE_REG, 3); - apply(); + _modbusWrapper.setSingleRegister(SRV_ENABLE_DRIVE_REG, 3); + apply(); } /*************************************************************************************************/ void ServoController::setStartStop(bool start) { - _modbusWrapper.setSingleRegister(SRV_START_STOP_REG, start ? 3 : 0); - apply(); + _modbusWrapper.setSingleRegister(SRV_START_STOP_REG, start ? 3 : 0); + apply(); } /*************************************************************************************************/ void ServoController::setStartAngle(double angle) { - _modbusWrapper.setSingleRegister(SRV_START_ANGLE_INT_PART_REG, static_cast(angle)); - _modbusWrapper.setSingleRegister(SRV_START_ANGLE_FRAC_PART_REG, static_cast((angle - (static_cast(angle))) * 100)); + _modbusWrapper.setSingleRegister(SRV_START_ANGLE_INT_PART_REG, static_cast(angle)); + _modbusWrapper.setSingleRegister(SRV_START_ANGLE_FRAC_PART_REG, + static_cast((angle - (static_cast(angle))) * + 100)); } /*************************************************************************************************/ void ServoController::setStopAngle(double angle) { - _modbusWrapper.setSingleRegister(SRV_STOP_ANGLE_INT_PART_REG, static_cast(angle)); - _modbusWrapper.setSingleRegister(SRV_STOP_ANGLE_FRAC_PART_REG, static_cast((angle - (static_cast(angle))) * 100)); + _modbusWrapper.setSingleRegister(SRV_STOP_ANGLE_INT_PART_REG, static_cast(angle)); + _modbusWrapper.setSingleRegister(SRV_STOP_ANGLE_FRAC_PART_REG, + static_cast((angle - (static_cast(angle))) * + 100)); } /*************************************************************************************************/ void ServoController::setSpeed(double speed) { - _modbusWrapper.setSingleRegister(SRV_AZIMUTH_SPEED_REG, static_cast(speed * SRV_SPEED_FACTOR)); + _modbusWrapper.setSingleRegister(SRV_AZIMUTH_SPEED_REG, + static_cast(speed * SRV_SPEED_FACTOR)); } /*************************************************************************************************/ void ServoController::changeMode(double startAngle, double stopAngle, double speed) { - setStartAngle(startAngle); - setStopAngle(stopAngle); - setSpeed(speed); - setStartStop(true); - enableDrive(); - apply(); + setStartAngle(startAngle); + setStopAngle(stopAngle); + setSpeed(speed); + setStartStop(true); + enableDrive(); + apply(); } /*************************************************************************************************/ -void ServoController::init() +void ServoController::init(QString serialPort) { - _modbusWrapper.init(); - _modbusWrapper.connectToDevice(initiateConfig()); + _modbusWrapper.init(); + _modbusWrapper.connectToDevice(initiateConfig(serialPort)); } /*************************************************************************************************/ void ServoController::startMoving() { - setStartStop(true); + setStartStop(true); } /*************************************************************************************************/ void ServoController::stopMoving() { - setStartStop(false); + setStartStop(false); } /*************************************************************************************************/ void ServoController::resetAzimuth() { - _modbusWrapper.setSingleRegister(SRV_CALIBRATE_REG, SRV_CALIBRATION_VAL); + _modbusWrapper.setSingleRegister(SRV_CALIBRATE_REG, SRV_CALIBRATION_VAL); } /*************************************************************************************************/ void ServoController::fix(double angle) { - changeMode(angle, angle, 5 / 6.0); + changeMode(angle, angle, 5 / 6.0); } /*************************************************************************************************/ void ServoController::sector(double startAngle, double stopAngle, double speed) { - changeMode(startAngle, stopAngle, speed); + changeMode(startAngle, stopAngle, speed); } /*************************************************************************************************/ double ServoController::getSpeed() { - auto speedVector = _modbusWrapper.getHoldingRegister(SRV_AZIMUTH_SPEED_REG, 1); - return (speedVector[0] / SRV_SPEED_FACTOR); + auto speedVector = _modbusWrapper.getHoldingRegister(SRV_AZIMUTH_SPEED_REG, 1); + + return (speedVector[0] / SRV_SPEED_FACTOR); } /*************************************************************************************************/ double ServoController::getStopAngle() { - auto speedVector1 = _modbusWrapper.getHoldingRegister(SRV_STOP_ANGLE_INT_PART_REG, 1); - auto speedVector2 = _modbusWrapper.getHoldingRegister(SRV_STOP_ANGLE_FRAC_PART_REG, 1); - return ((speedVector1[0] + speedVector2[0]) / SRV_ANGLE_FACTOR); + auto speedVector1 = _modbusWrapper.getHoldingRegister(SRV_STOP_ANGLE_INT_PART_REG, 1); + auto speedVector2 = _modbusWrapper.getHoldingRegister(SRV_STOP_ANGLE_FRAC_PART_REG, 1); + + return ((speedVector1[0] + speedVector2[0]) / SRV_ANGLE_FACTOR); } /*************************************************************************************************/ double ServoController::getStartAngle() { - auto speedVector1 = _modbusWrapper.getHoldingRegister(SRV_START_ANGLE_INT_PART_REG, 1); - auto speedVector2 = _modbusWrapper.getHoldingRegister(SRV_START_ANGLE_FRAC_PART_REG, 1); - return ((speedVector1[0] + speedVector2[0]) / SRV_ANGLE_FACTOR); + auto speedVector1 = _modbusWrapper.getHoldingRegister(SRV_START_ANGLE_INT_PART_REG, 1); + auto speedVector2 = _modbusWrapper.getHoldingRegister(SRV_START_ANGLE_FRAC_PART_REG, 1); + + return ((speedVector1[0] + speedVector2[0]) / SRV_ANGLE_FACTOR); } /*************************************************************************************************/ double ServoController::getAngleOffset() { - auto angleOffsetVector = _modbusWrapper.getHoldingRegister(SRV_ZERO_OFFSET_ANGLE, 1); - return (angleOffsetVector[0] / SRV_ANGLE_FACTOR); + auto angleOffsetVector = _modbusWrapper.getHoldingRegister(SRV_ZERO_OFFSET_ANGLE, 1); + + return (angleOffsetVector[0] / SRV_ANGLE_FACTOR); } /*************************************************************************************************/ void ServoController::setAngleOffset(double offset) { - _modbusWrapper.setSingleRegister(SRV_ZERO_OFFSET_ANGLE, static_cast(offset * SRV_ANGLE_FACTOR)); + _modbusWrapper.setSingleRegister(SRV_ZERO_OFFSET_ANGLE, + static_cast(offset * SRV_ANGLE_FACTOR)); } /*************************************************************************************************/ double ServoController::getAzimuth() { - auto azimuthVector = _modbusWrapper.getHoldingRegister(SRV_AZIMUTH_REG, 1); - return (azimuthVector[0] / SRV_ANGLE_FACTOR); + auto azimuthVector = _modbusWrapper.getHoldingRegister(SRV_AZIMUTH_REG, 1); + + return (azimuthVector[0] / SRV_ANGLE_FACTOR); } diff --git a/Test/MainWindow.cpp b/Test/MainWindow.cpp index f124647..8f1d563 100644 --- a/Test/MainWindow.cpp +++ b/Test/MainWindow.cpp @@ -17,6 +17,7 @@ MainWindow::MainWindow(QWidget* parent) MainWindow::~MainWindow() { delete ui; + timer->stop(); } /*************************************************************************************************/ @@ -196,12 +197,11 @@ void MainWindow::on_writeMultiRegister_clicked() void MainWindow::on_init_clicked() { try { - _servoControler.init(); + _servoControler.init(ui->serialPortServo->text()); timer = new QTimer(); - timer->setInterval(2000); + timer->setInterval(1000); timer->start(); connect(timer, &QTimer::timeout, this, &MainWindow::handleGetRequestFromServo); - timer->stop(); } catch(ServoException exp) { @@ -279,7 +279,7 @@ void MainWindow::on_sectorSpeed_clicked() void MainWindow::on_fix_clicked() { try { - _servoControler.fix(ui->speed->text().toDouble()); + _servoControler.fix(ui->startAngle->text().toDouble()); } catch(ServoException exp) { diff --git a/Test/MainWindow.ui b/Test/MainWindow.ui index 87b564d..706f62d 100644 --- a/Test/MainWindow.ui +++ b/Test/MainWindow.ui @@ -33,9 +33,9 @@ - 40 + 30 10 - 89 + 141 25 @@ -47,7 +47,7 @@ 20 - 420 + 460 761 91 @@ -230,7 +230,7 @@ background-color: rgb(186, 178, 158); - 250 + 260 10 86 25 @@ -421,7 +421,7 @@ background-color: rgb(186, 178, 158); 40 50 301 - 341 + 331 @@ -431,7 +431,7 @@ background-color: rgb(186, 178, 158); 16 - 120 + 110 281 181 @@ -441,7 +441,7 @@ background-color: rgb(186, 178, 158); 110 - 310 + 300 80 25 @@ -506,7 +506,7 @@ background-color: rgb(186, 178, 158); 20 - 100 + 90 126 17 @@ -519,7 +519,7 @@ background-color: rgb(186, 178, 158); - 200 + 210 10 41 25 @@ -539,7 +539,7 @@ background-color: rgb(186, 178, 158); 30 20 - 471 + 671 71 @@ -549,7 +549,7 @@ background-color: rgb(186, 178, 158); - 340 + 542 30 111 25 @@ -562,7 +562,7 @@ background-color: rgb(186, 178, 158); - 28 + 230 30 61 25 @@ -575,7 +575,7 @@ background-color: rgb(186, 178, 158); - 110 + 312 30 91 25 @@ -588,7 +588,7 @@ background-color: rgb(186, 178, 158); - 210 + 412 30 91 25 @@ -598,6 +598,29 @@ background-color: rgb(186, 178, 158); StopMoving + + + + 10 + 30 + 81 + 25 + + + + SerialPort: + + + + + + 90 + 30 + 121 + 25 + + +