diff --git a/include/botui/Create3Widget.h b/include/botui/Create3Widget.h index 3705148a..5dd693c4 100644 --- a/include/botui/Create3Widget.h +++ b/include/botui/Create3Widget.h @@ -4,6 +4,7 @@ #include "StandardWidget.h" #include +#include #include namespace Ui @@ -15,30 +16,27 @@ class Create3SensorModel; class Create3Widget : public StandardWidget { -Q_OBJECT + Q_OBJECT public: Create3Widget(Device *device, QWidget *parent = 0); ~Create3Widget(); private slots: - void create3Connect(); - // void resetServer(); - void sensorList(); - void exampleList(); QString getIP(); void toggleChanged(); - + void indexChanged(int index); + void run(); private: Ui::Create3Widget *ui; - Create3SensorModel *_model; + Create3SensorModel *_model; double m_setpointVal; double m_feedbackVal; int m_position_1; + QStringListModel *model; double m_vel_1; }; - #endif diff --git a/src/Create3Widget.cpp b/src/Create3Widget.cpp index 0d73d211..4e885d44 100644 --- a/src/Create3Widget.cpp +++ b/src/Create3Widget.cpp @@ -1,7 +1,7 @@ #include "Create3Widget.h" #include "ui_Create3Widget.h" #include "Create3SensorListWidget.h" -#include "Create3ExampleWidget.h" + #include "Create3SensorModel.h" #include #include @@ -18,6 +18,9 @@ #include #include "RootController.h" +int exampleIndex; +QStringList programList; + class Create3SensorItemDelegate : public QItemDelegate { public: @@ -40,9 +43,6 @@ Create3Widget::Create3Widget(Device *device, QWidget *parent) performStandardSetup(tr("Create 3"), false); setStyleSheet("QScrollBar:vertical {border: 2px solid grey;background:grey ;width: 100px; margin: 22px 0 22px 0; } QScrollBar::handle:vertical { border: 2px solid grey;background: white; min-height: 20px; } QScrollBar::add-line:vertical { border: 2px solid grey;background: #32CC99; height: 20px; subcontrol-position: bottom; subcontrol-origin: margin; } QScrollBar::sub-line:vertical {border: 2px solid grey; background: #32CC99; height: 20px; subcontrol-position: top; subcontrol-origin: margin; } QScrollBar::up-arrow:vertical, QScrollBar::down-arrow:vertical { border: 2px solid grey;width: 3px; height: 3px; background: white; }QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical {border: 2px solid grey; background: none;}"); - connect(ui->Create3SensorListButton, SIGNAL(clicked()), SLOT(sensorList())); - connect(ui->Create3ExampleProgramButton, SIGNAL(clicked()), SLOT(exampleList())); - ui->create3IP->setText(getIP()); QStringList arguments; @@ -59,16 +59,30 @@ Create3Widget::Create3Widget(Device *device, QWidget *parent) if (ipOutput == "192.168.125.1") { - ui->toggleSwitch->setChecked(false); //Wifi toggle side + ui->toggleSwitch->setChecked(false); // Wifi toggle side } else if (ipOutput == "192.168.186.3") { - ui->toggleSwitch->setChecked(true); //Ethernet toggle side + ui->toggleSwitch->setChecked(true); // Ethernet toggle side } connect(ui->toggleSwitch, SIGNAL(stateChanged(int)), this, SLOT(toggleChanged())); - - + connect(ui->exampleComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(indexChanged(int))); + + connect(ui->exampleRunButton, SIGNAL(clicked()), SLOT(run())); + + // Create model + model = new QStringListModel(this); + ui->listWidget->clear(); + programList.clear(); + programList << "printf('Connecting to Create3 robot...');" + << "create3_connect_manual('192.168.125.1', 50051);" + << "printf('Connected!');"; + + ui->listWidget->addItems(programList); + model->setStringList(programList); + qDebug() << "Program List: " << programList; + ui->listWidget->setSpacing(6); } Create3Widget::~Create3Widget() @@ -82,8 +96,8 @@ void Create3Widget::toggleChanged() StandardWidget::disableMenuBar(); ui->toggleSwitch->setEnabled(false); - ui->Create3SensorListButton->setEnabled(false); - ui->Create3ExampleProgramButton->setEnabled(false); + ui->exampleComboBox->setEnabled(false); + ui->exampleRunButton->setEnabled(false); QProcess checkCreate3IPState; QString startCommand = "cat"; @@ -106,10 +120,11 @@ void Create3Widget::toggleChanged() { StandardWidget::enableMenuBar(); - ui->toggleSwitch->setChecked(false); //Wifi toggle side + ui->toggleSwitch->setChecked(false); // Wifi toggle side ui->toggleSwitch->setEnabled(true); - ui->Create3SensorListButton->setEnabled(true); - ui->Create3ExampleProgramButton->setEnabled(true); + ui->exampleComboBox->setEnabled(true); + ui->exampleRunButton->setEnabled(true); + return; } else @@ -127,10 +142,11 @@ void Create3Widget::toggleChanged() { StandardWidget::enableMenuBar(); - ui->toggleSwitch->setChecked(true); //Ethernet toggle side + ui->toggleSwitch->setChecked(true); // Ethernet toggle side ui->toggleSwitch->setEnabled(true); - ui->Create3SensorListButton->setEnabled(true); - ui->Create3ExampleProgramButton->setEnabled(true); + ui->exampleComboBox->setEnabled(true); + ui->exampleRunButton->setEnabled(true); + return; } else @@ -178,36 +194,413 @@ QString Create3Widget::getIP() return output; } -void Create3Widget::sensorList() -{ - RootController::ref().presentWidget(new Create3SensorListWidget(device())); -} +// void Create3Widget::sensorList() +// { +// RootController::ref().presentWidget(new Create3SensorListWidget(device())); +// } + +// void Create3Widget::exampleList() +// { +// RootController::ref().presentWidget(new Create3ExampleWidget(device())); +// } -void Create3Widget::exampleList() +void Create3Widget::indexChanged(int index) { - RootController::ref().presentWidget(new Create3ExampleWidget(device())); + + programList.clear(); + ui->listWidget->clear(); + ui->listWidget->addItems(programList); + model->setStringList(programList); + switch (index) + { + case 0: // 01_Connect + { + + programList << "printf('Connecting to Create3 robot...');" + << "create3_connect_manual('192.168.125.1', 50051);" + << "printf('Connected!');"; + qDebug() << "Program List: " << programList; + ui->listWidget->addItems(programList); + model->setStringList(programList); + break; + } + + case 1: // 02_Dock_Undock + { + programList.clear(); + programList << "printf('Connecting to Create3 robot...');" + << "create3_connect_manual('192.168.125.1', 50051);" + << "printf('Connected!');" + << "printf('Undocking...');" + << "create3_undock();" + << "create3_wait();" + << "printf('Done!');"; + qDebug() << "Program List: " << programList; + + ui->listWidget->addItems(programList); + model->setStringList(programList); + break; + } + case 2: // 03_Move_Velocity + { + + programList + << "printf('Connecting to Create3 robot...');" + << "create3_connect_manual('192.168.125.1', 50051);" + << "printf('Connected!');" + << "printf('Moving forward at 0.5 m/s for 1 second...');" + << "//Move forward at 0.5 m/s for 1 second" + << "create3_velocity_set_components(0.5, 0);" + << "//Wait for 1 second" + << "usleep(1000000);" + << "//Stop" + << "create3_velocity_set_components(0.0, 0.0);" + << "printf('Done!');"; + qDebug() << "Program List: " << programList; + ui->listWidget->addItems(programList); + model->setStringList(programList); + ui->listWidget->item(4)->setForeground(QColor(0, 128, 0)); + ui->listWidget->item(6)->setForeground(QColor(0, 128, 0)); + ui->listWidget->item(8)->setForeground(QColor(0, 128, 0)); + + break; + } + case 3: // 04_Rotate + programList + << "printf('Connecting to Create3 robot...');" + << "create3_connect_manual('192.168.125.1', 50051);" + << "printf('Connected!');" + << "printf('Rotate at 0.5 m/s for 1 second...');" + << "//Rotate at 90 deg/s (PI/2 rad/s) to 90 degrees (PI/2 radians)" + << "create3_rotate_degrees(90, 90);" + << "create3_rotate_radians(1.57, 1.57);" + << "//Wait for 1 second" + << "create3_wait();" + << "printf('Done!');"; + ui->listWidget->addItems(programList); + model->setStringList(programList); + ui->listWidget->item(4)->setForeground(QColor(0, 128, 0)); + ui->listWidget->item(7)->setForeground(QColor(0, 128, 0)); + + break; + case 4: // 05_Arc + programList + << "printf('Connecting to Create3 robot...');" + << "create3_connect_manual('192.168.125.1', 50051);" + << "printf('Connected!');" + << "printf('Rotate at 0.5 m/s for 1 second...');" + << "//Arc of 90 deg (PI/2 rad) with radius of 0.5 m" + << "create3_drive_arc_degrees(0.5, 90, 0.306);" + << "create3_drive_arc_radians(0.5, -1.57, 0.460);" + << "//Wait for 1 second" + << "create3_wait();" + << "printf('Done!');"; + ui->listWidget->addItems(programList); + model->setStringList(programList); + ui->listWidget->item(4)->setForeground(QColor(0, 128, 0)); + ui->listWidget->item(7)->setForeground(QColor(0, 128, 0)); + break; + case 5: // 06_led_test + programList + << "printf('Connecting to Create3 robot...');" + << "create3_connect_manual('192.168.125.1', 50051);" + << "printf('Connected!');" + << "printf('Blinking lights...');" + << "//Blink the lights green for 5 seconds." + << "Create3LedColor green;" + << "green.r = 0;" + << "green.g = 255;" + << "green.b = 0;" + << "Create3Lightring lightring;" + << "lightring.led0 = green;" + << "lightring.led1 = green;" + << "lightring.led2 = green;" + << "lightring.led3 = green;" + << "lightring.led4 = green;" + << "lightring.led5 = green;" + << "// Blink the lights green for 5 seconds." + << "create3_led_animation(Create3BlinkLights, lightring, 5.0);" + << "//Wait for 1 second" + << "create3_wait();" + << "printf('Done!');"; + ui->listWidget->addItems(programList); + model->setStringList(programList); + ui->listWidget->item(4)->setForeground(QColor(0, 128, 0)); + ui->listWidget->item(16)->setForeground(QColor(0, 128, 0)); + ui->listWidget->item(18)->setForeground(QColor(0, 128, 0)); + break; + case 6: // 07_sensors + programList + << "printf('Connecting to Create3 robot...');" + << "create3_connect_manual('192.168.125.1', 50051);" + << "printf('Connected!');" + << "printf('Sensor Values:');" + << "printf(' Left Side Cliff: %d', create3_sensor_cliff(0));" + << "printf(' Left Front Cliff: %d', create3_sensor_cliff(1));" + << "printf(' Right Front Cliff: %d', create3_sensor_cliff(2));" + << "printf(' Right Side Cliff: %d', create3_sensor_cliff(3));" + << "printf(' Left Side IR: %d', create3_sensor_ir(0));" + << "printf(' Left IR: %d', create3_sensor_ir(1));" + << "printf(' Left Front IR: %d', create3_sensor_ir(2));" + << "printf(' Left Center IR: %d', create3_sensor_ir(3));" + << "printf(' Right Center IR: %d', create3_sensor_ir(4));" + << "printf(' Right Front IR: %d', create3_sensor_ir(5));" + << "printf(' Right IR: %d', create3_sensor_ir(6));" + << "printf(' Left Bumper: %d', create3_sensor_bump(0));" + << "printf(' Left Front Bumper: %d', create3_sensor_bump(1));" + << "printf(' Center Front Bumper: %d', create3_sensor_bump(2));" + << "printf(' Right Front Bumper: %d', create3_sensor_bump(3));" + << "printf(' Right Bumper: %d', create3_sensor_bump(4));" + << "while (create3_sensor_bump(3) == 0)" + << "{" + << " if (create3_sensor_bump(3) == 1)" + << " {" + << " create3_velocity_set_components(0.0, 0.0);" + << " break;" + << " }" + << " create3_velocity_set_components(0.15, 0.0);" + << "}" + << "printf('Done!');"; + ui->listWidget->addItems(programList); + model->setStringList(programList); + + break; + default: + break; + } } -void Create3Widget::create3Connect() +void Create3Widget::run() { - int connected = 0; - try + exampleIndex = ui->exampleComboBox->currentIndex(); + switch (exampleIndex) { - connected = create3_connect(); - qDebug() << "Create connected? " << connected; + + case 0: + { // 01_Connect + try + { + printf("Connecting to Create3 robot...\n"); + create3_connect_manual("192.168.125.1", 50051); + printf("Connected!\n"); + } + catch (const std::exception &e) + { + std::cerr << e.what() << '\n'; + } + break; } - catch (const std::exception &e) + + case 1: // 02_Dock_Undock { - std::cerr << e.what() << '\n'; + + try + { + printf("Connecting to Create3 robot...\n"); + + create3_connect_manual("192.168.125.1", 50051); + + printf("Connected!\n"); + + printf("Undocking...\n"); + // Move forward at 0.5 m/s for 1 second + create3_undock(); + + create3_wait(); + + printf("Docking...\n"); + + create3_dock(); + + create3_wait(); + + printf("Done!\n"); + } + catch (const std::exception &e) + { + std::cerr << e.what() << '\n'; + } + + break; } - if (connected == 1) + + case 2: // 03_Move_Velocity { - qDebug() << "Create connected"; - + try + { + printf("Connecting to Create3 robot...\n"); + + create3_connect_manual("192.168.125.1", 50051); + + printf("Connected!\n"); + + printf("Moving forward at 0.5 m/s for 1 second...\n"); + + // Move forward at 0.5 m/s for 1 second + create3_velocity_set_components(0.5, 0); + + // Wait for 1 second + usleep(1000000); + + // Stop + create3_velocity_set_components(0.0, 0.0); + + printf("Done!\n"); + } + catch (const std::exception &e) + { + std::cerr << e.what() << '\n'; + } + break; + } + + case 3: // 04_Rotate + { + try + { + printf("Connecting to Create3 robot...\n"); + + create3_connect_manual("192.168.125.1", 50051); + + printf("Connected!\n"); + + printf("Rotate at 0.5 m/s for 1 second...\n"); + + // Rotate at 90 deg/s (PI/2 rad/s) to 90 degrees (PI/2 radians) + create3_rotate_degrees(90, 90); + create3_rotate_radians(1.57, 1.57); + + // Wait for 1 second + create3_wait(); + + printf("Done!\n"); + } + catch (const std::exception &e) + { + std::cerr << e.what() << '\n'; + } + + break; } - else + + case 4: // 05_Arc { - qDebug() << "Create not connected"; - + try + { + printf("Connecting to Create3 robot...\n"); + + create3_connect_manual("192.168.125.1", 50051); + + printf("Connected!\n"); + + printf("Rotate at 0.5 m/s for 1 second...\n"); + + // Arc of 90 deg (PI/2 rad) with radius of 0.5 m + create3_drive_arc_degrees(0.5, 90, 0.306); + create3_drive_arc_radians(0.5, -1.57, 0.460); + + // Wait for 1 second + create3_wait(); + + printf("Done!\n"); + } + catch (const std::exception &e) + { + std::cerr << e.what() << '\n'; + } + + break; + } + + case 5: // 06_led_test + { + try + { + printf("Connecting to Create3 robot...\n"); + + create3_connect_manual("192.168.125.1", 50051); + + printf("Connected!\n"); + + printf("Blinking lights...\n"); + // Blink the lights green for 5 seconds. + + Create3LedColor green; + green.r = 0; + green.g = 255; + green.b = 0; + + Create3Lightring lightring; + lightring.led0 = green; + lightring.led1 = green; + lightring.led2 = green; + lightring.led3 = green; + lightring.led4 = green; + lightring.led5 = green; + + create3_led_animation(Create3BlinkLights, lightring, 5.0); // Blink the lights green for 5 seconds. + + // Wait for 1 second + create3_wait(); + + printf("Done!\n"); + } + catch (const std::exception &e) + { + std::cerr << e.what() << '\n'; + } + + break; + } + case 6: // 07_sensors + { + try + { + printf("Connecting to Create3 robot...\n"); + + create3_connect_manual("192.168.125.1", 50051); + + printf("Connected!\n"); + + printf("Sensor Values:\n"); + printf(" Left Side Cliff: %d\n", create3_sensor_cliff(0)); + printf(" Left Front Cliff: %d\n", create3_sensor_cliff(1)); + printf(" Right Front Cliff: %d\n", create3_sensor_cliff(2)); + printf(" Right Side Cliff: %d\n", create3_sensor_cliff(3)); + printf("\n"); + printf(" Left Side IR: %d\n", create3_sensor_ir(0)); + printf(" Left IR: %d\n", create3_sensor_ir(1)); + printf(" Left Front IR: %d\n", create3_sensor_ir(2)); + printf(" Left Center IR: %d\n", create3_sensor_ir(3)); + printf(" Right Center IR: %d\n", create3_sensor_ir(4)); + printf(" Right Front IR: %d\n", create3_sensor_ir(5)); + printf(" Right IR: %d\n", create3_sensor_ir(6)); + + printf(" Left Bumper: %d\n", create3_sensor_bump(0)); + printf(" Left Front Bumper: %d\n", create3_sensor_bump(1)); + printf(" Center Front Bumper: %d\n", create3_sensor_bump(2)); + printf(" Right Front Bumper: %d\n", create3_sensor_bump(3)); + printf(" Right Bumper: %d\n", create3_sensor_bump(4)); + + while (create3_sensor_bump(3) == 0) + { + if (create3_sensor_bump(3) == 1) + { + create3_velocity_set_components(0.0, 0.0); + break; + } + create3_velocity_set_components(0.15, 0.0); + } + + printf("Done!\n"); + } + catch (const std::exception &e) + { + std::cerr << e.what() << '\n'; + } + } + + default: + break; } } diff --git a/ui/Create3Widget.ui b/ui/Create3Widget.ui index 9aed9b32..06c3bb09 100644 --- a/ui/Create3Widget.ui +++ b/ui/Create3Widget.ui @@ -25,7 +25,7 @@ - 0 + 11 0 @@ -41,6 +41,49 @@ 35 + + + + + + + 160 + 16777215 + + + + + 22 + + + + Create3 IP: + + + + + + + + 25 + 0 + + + + + 22 + + + + + + + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter + + + + + @@ -130,87 +173,98 @@ QCheckBox::indicator:unchecked{ - - - - - - 160 - 16777215 - - - - - 22 - - - - Create3 IP: - - - - - - - - 25 - 0 - - - - - 22 - - - - - - - Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter - - - - - - - - - - - - 0 - 80 - - - - - 21 - - - - Create 3 Sensor List - - - - - - - - 0 - 80 - - - - - 21 - - - - Create 3 Example Programs - - - - + + + + 22 + + + + + + + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter + + + + + + + + + 16777215 + 16777215 + + + + + 01_Connect + + + + + 02_Dock_Undock + + + + + 03_Move_Velocity + + + + + 04_Rotate + + + + + 05_Arc + + + + + 06_LED + + + + + 07_Sensors + + + + + + + + Run + + + + + + + + + + + + + 0 + + + + + + + + + 0 + + + + + +