Skip to content

Commit

Permalink
Add the callback function for the change of speed loop PID parameter …
Browse files Browse the repository at this point in the history
…adjustment slider value, and modify the packet sending function to send different Zhi according to the change of different sliders
  • Loading branch information
ZhuYanzhen1 committed Sep 20, 2021
1 parent f2a7d5e commit bab30b4
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 29 deletions.
2 changes: 1 addition & 1 deletion tools/CMakeLists.txt.user
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.11.1, 2021-09-19T14:13:41. -->
<!-- Written by QtCreator 4.11.1, 2021-09-20T15:23:01. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
Expand Down
93 changes: 77 additions & 16 deletions tools/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ MainWindow::MainWindow(QWidget *parent)
ui->mode_set_cb->addItem("Torque Control");
ui->mode_set_cb->addItem("Speed Control");
ui->mode_set_cb->addItem("Angle Control");
ui->mode_set_cb->setEditText("Torque Control");
for (int counter = 0; counter < 4; counter++)
angle_slider_change_flag[counter] = false;
for (int counter = 0; counter < 4; counter++)
speed_slider_change_flag[counter] = false;
}

MainWindow::~MainWindow(){
Expand Down Expand Up @@ -43,14 +46,6 @@ void MainWindow::mdtp_callback_handler(unsigned char pid, const unsigned char *d
}
}

void MainWindow::on_user_expect_slider_valueChanged(int value){
float maximum = ui->slider_maximum_value->toPlainText().toFloat();
float minimum = ui->slider_minimum_value->toPlainText().toFloat();
expect_slider_change_flag = true;
expect_slider_value = (maximum - minimum) * (value / 10000.0f) + minimum;
ui->slider_current_value->setText(QString::number(expect_slider_value, 10, 2));
}

void MainWindow::on_open_btn_clicked(){
if(serial->isOpen() == false){
if(set_serial_badurate() == true){
Expand Down Expand Up @@ -117,19 +112,85 @@ void MainWindow::on_refresh_btn_clicked(){
refresh_serial_port();
}

/* slider value changed functions */

void MainWindow::on_speed_kp_slider_valueChanged(int value)
{
float maximum = ui->speed_kp_maximum->toPlainText().toFloat();
float minimum = ui->speed_kp_minimum->toPlainText().toFloat();
speed_slider_change_flag[0] = true;
speed_slider_value[0] = (maximum - minimum) * (value / 10000.0f) + minimum;
ui->speed_kp_value->setText(QString::number(speed_slider_value[0], 10, 2));
}

void MainWindow::on_speed_ki_slider_valueChanged(int value){
float maximum = ui->speed_ki_maximum->toPlainText().toFloat();
float minimum = ui->speed_ki_minimum->toPlainText().toFloat();
speed_slider_change_flag[1] = true;
speed_slider_value[1] = (maximum - minimum) * (value / 10000.0f) + minimum;
ui->speed_ki_value->setText(QString::number(speed_slider_value[1], 10, 2));
}

void MainWindow::on_speed_kd_slider_valueChanged(int value){
float maximum = ui->speed_kd_maximum->toPlainText().toFloat();
float minimum = ui->speed_kd_minimum->toPlainText().toFloat();
speed_slider_change_flag[2] = true;
speed_slider_value[2] = (maximum - minimum) * (value / 10000.0f) + minimum;
ui->speed_kd_value->setText(QString::number(speed_slider_value[2], 10, 2));
}

void MainWindow::on_speed_summax_slider_valueChanged(int value){
float maximum = ui->speed_summax_maximum->toPlainText().toFloat();
float minimum = ui->speed_summax_minimum->toPlainText().toFloat();
speed_slider_change_flag[3] = true;
speed_slider_value[3] = (maximum - minimum) * (value / 10000.0f) + minimum;
ui->speed_summax_value->setText(QString::number(speed_slider_value[3], 10, 2));
}

void MainWindow::on_user_expect_slider_valueChanged(int value){
float maximum = ui->slider_maximum_value->toPlainText().toFloat();
float minimum = ui->slider_minimum_value->toPlainText().toFloat();
expect_slider_change_flag = true;
expect_slider_value = (maximum - minimum) * (value / 10000.0f) + minimum;
ui->slider_current_value->setText(QString::number(expect_slider_value, 10, 2));
}

/* some functions related to timer interrupt */

void MainWindow::slider_timer_timeout(){
unsigned char buffer[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
unsigned int uintp32 = 0x00000000UL;
if(expect_slider_change_flag == true){
unsigned char buffer[8] = {0x3C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
unsigned int uintp32 = (*((unsigned int *) (&expect_slider_value)));
buffer[4] = (unsigned char) ((uintp32 >> 24UL) & 0x000000ffUL);
buffer[5] = (unsigned char) ((uintp32 >> 16UL) & 0x000000ffUL);
buffer[6] = (unsigned char) ((uintp32 >> 8UL) & 0x000000ffUL);
buffer[7] = (unsigned char) (uintp32 & 0x000000ffUL);
mdtp_data_transmit(0x00, buffer);
buffer[0] = 0x3C;
uintp32 = (*((unsigned int *) (&expect_slider_value)));
expect_slider_change_flag = false;
goto mdtp_sendpackage;
}

for (int counter = 0; counter < 4; counter++) {
if(speed_slider_change_flag[counter] != false){
buffer[0] = ((4 + counter) << 4) | ((~(4 + counter)) & 0x0f);
uintp32 = (*((unsigned int *) (&speed_slider_value[counter])));
speed_slider_change_flag[counter] = false;
goto mdtp_sendpackage;
}
}

for (int counter = 0; counter < 4; counter++) {
if(angle_slider_change_flag[counter] != false){
buffer[0] = ((8 + counter) << 4) | ((~(8 + counter)) & 0x0f);
uintp32 = (*((unsigned int *) (&angle_slider_value[counter])));
angle_slider_change_flag[counter] = false;
goto mdtp_sendpackage;
}
}

mdtp_sendpackage:
buffer[4] = (unsigned char) ((uintp32 >> 24UL) & 0x000000ffUL);
buffer[5] = (unsigned char) ((uintp32 >> 16UL) & 0x000000ffUL);
buffer[6] = (unsigned char) ((uintp32 >> 8UL) & 0x000000ffUL);
buffer[7] = (unsigned char) (uintp32 & 0x000000ffUL);
mdtp_data_transmit(0x00, buffer);
}

/* some functions related to plot initialize and refresh */
Expand Down
12 changes: 12 additions & 0 deletions tools/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ private slots:
void on_calibrate_btn_clicked();
void on_user_expect_slider_valueChanged(int value);

void on_speed_kp_slider_valueChanged(int value);

void on_speed_ki_slider_valueChanged(int value);

void on_speed_kd_slider_valueChanged(int value);

void on_speed_summax_slider_valueChanged(int value);

public slots:
void serial_received();
void slider_timer_timeout();
Expand All @@ -35,7 +43,11 @@ public slots:
Ui::MainWindow *ui;
QSerialPort *serial;
bool expect_slider_change_flag = false;
bool speed_slider_change_flag[4];
bool angle_slider_change_flag[4];
float expect_slider_value = 0;
float speed_slider_value[4];
float angle_slider_value[4];
QTimer *slider_timer;
uint64_t curve_counter = 0;
QVector<double> curve_velocity, curve_angle, curve_x;
Expand Down
30 changes: 18 additions & 12 deletions tools/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
<rect>
<x>10</x>
<y>10</y>
<width>721</width>
<width>701</width>
<height>461</height>
</rect>
</property>
Expand All @@ -171,9 +171,9 @@
</property>
<property name="geometry">
<rect>
<x>740</x>
<x>720</x>
<y>10</y>
<width>121</width>
<width>141</width>
<height>31</height>
</rect>
</property>
Expand All @@ -193,9 +193,9 @@
</property>
<property name="geometry">
<rect>
<x>739</x>
<x>719</x>
<y>130</y>
<width>121</width>
<width>141</width>
<height>22</height>
</rect>
</property>
Expand Down Expand Up @@ -227,9 +227,9 @@
</property>
<property name="geometry">
<rect>
<x>740</x>
<x>720</x>
<y>90</y>
<width>121</width>
<width>141</width>
<height>31</height>
</rect>
</property>
Expand All @@ -249,7 +249,7 @@
</property>
<property name="geometry">
<rect>
<x>740</x>
<x>720</x>
<y>160</y>
<width>41</width>
<height>31</height>
Expand Down Expand Up @@ -303,9 +303,9 @@
</property>
<property name="geometry">
<rect>
<x>740</x>
<x>720</x>
<y>50</y>
<width>121</width>
<width>141</width>
<height>31</height>
</rect>
</property>
Expand All @@ -315,11 +315,17 @@
<pointsize>10</pointsize>
</font>
</property>
<property name="editable">
<bool>false</bool>
</property>
<property name="currentText">
<string/>
</property>
</widget>
<widget class="QLabel" name="slider_current_value">
<property name="geometry">
<rect>
<x>790</x>
<x>780</x>
<y>170</y>
<width>41</width>
<height>16</height>
Expand Down Expand Up @@ -819,7 +825,7 @@
<property name="geometry">
<rect>
<x>320</x>
<y>40</y>
<y>30</y>
<width>281</width>
<height>421</height>
</rect>
Expand Down

0 comments on commit bab30b4

Please sign in to comment.