Page MenuHomec4science

cellulounity.cpp
No OneTemporary

File Metadata

Created
Thu, Apr 25, 15:51

cellulounity.cpp

#include "cellulounity.h"
#include "cellulothread.h"
#include "cellulorobotwrapper.h"
#include "cellulopoolclient.h"
#include <QDebug>
using Cellulo::CelluloBluetoothScanner;
using Cellulo::CelluloBluetooth;
CelluloThread* thread = nullptr;
CelluloBluetoothScanner* scanner = nullptr;
CelluloPoolClientWrapper* client = nullptr;
// Number of robots used from the pool
int used_robots;
/*
* Library initialization
*/
void initialize()
{
if (thread == nullptr) {
qDebug() << "Creating thread...";
thread = new CelluloThread;
qDebug() << "Starting thread...";
thread->start();
}
// initializing the library again...
if (scanner != nullptr && client != nullptr) {
qDebug() << "Client deinit...";
client->deinit();
qDebug() << "Deleting scanner and client...";
scanner->deleteLater();
client->deleteLater();
qDebug() << "Setting ptrs to nullptr";
scanner = nullptr;
client = nullptr;
}
if (scanner == nullptr) {
qDebug() << "Creating scanner...";
scanner = new CelluloBluetoothScanner;
qDebug() << "Moving scanner to thread...";
scanner->moveToThread(thread);
}
if (client == nullptr) {
qDebug() << "Creating pool...";
client = new CelluloPoolClientWrapper;
qDebug() << "Moving pool to thread...";
client->moveToThread(thread);
qDebug() << "Initializing pool...";
client->init();
used_robots = 0;
}
}
/*
* Scanner bindings
*/
void startScanning() {
if (scanner == nullptr) {
qDebug() << "Cannot start scanning because initialize() was not called";
return;
}
qDebug() << "Starting scanner...";
scanner->start();
}
void stopScanning() {
if (scanner == nullptr) {
qDebug() << "Cannot stop scanning because initialize() was not called";
return;
}
qDebug() << "Stopping scanner...";
scanner->stop();
}
void clearScanResults() {
if(scanner == nullptr) {
qDebug() << "Cannot clear scan results because initialize() was not called";
return;
}
qDebug() << "Clearing scanner results...";
scanner->clear();
}
int getScanResultsLength() {
if (scanner == nullptr) {
qDebug() << "Cannot get scan results length because initialize() was not called";
return -1;
}
return scanner->getFoundRobots().length();
}
const char * getScanResultAtIndex(int index) {
if (scanner == nullptr) {
qDebug() << "Cannot get scan result at index" << index << "because initialize() was not called";
return nullptr;
}
QByteArray utf8 = scanner->getFoundRobots().at(index).toUtf8();
return utf8.constData();
}
/*
* Robot creation
*/
int64_t newRobotFromPool() {
if (client == nullptr) {
qDebug() << "Cannot create a new robot because initialize() was not called";
return 0;
}
if (used_robots >= client->robots_N) {
qDebug() << "Cannot create a new robot because the pool is exhausted. Connect to more and restart the app.";
return 0;
}
Cellulo::CelluloBluetooth* robot1 = client->getRobots().at(used_robots++);
qDebug() << "Wrapping robot" << robot1;
CelluloRobotWrapper* robot = new CelluloRobotWrapper(robot1);
qDebug() << "Moving robot to thread...";
robot->moveToThread(thread);
qDebug() << "Initializing robot...";
robot->init();
qDebug() << "Got wrapped robot" << robot;
return (int64_t) robot;
}
int64_t newRobotFromMAC(const char * address) {
if (scanner == nullptr) {
qDebug() << "Cannot create a new robot because initialize() was not called";
return 0;
}
qDebug() << "Creating bluetooth object...";
CelluloBluetooth* bluetooth = new CelluloBluetooth();
qDebug() << "Moving bluetooth object to thread...";
bluetooth->moveToThread(thread);
qDebug() << "Moving bluetooth timers to thread...";
bluetooth->moveTimersToThread(thread);
qDebug() << "Setting robot MAC address" << address;
bluetooth->setMacAddr(QString::fromUtf8(address));
qDebug() << "Setting automatic connect";
bluetooth->setAutoConnect(1);
qDebug() << "Creating robot object...";
CelluloRobotWrapper* robot = new CelluloRobotWrapper(bluetooth);
qDebug() << "Moving robot to thread...";
robot->moveToThread(thread);
qDebug() << "Initializing robot...";
robot->init();
qDebug() << "Got wrapped robot" << robot;
return (int64_t) robot;
}
/*
* Robot bindings
*/
void setGoalVelocity(int64_t robot, float vx, float vy, float w) {
((CelluloRobotWrapper*) robot)->setGoalVelocity(vx, vy, w);
}
void setGoalPose(int64_t robot, float x, float y, float theta, float v, float w) {
((CelluloRobotWrapper*) robot)->setGoalPose(x, y, theta, v, w);
}
void setGoalPosition(int64_t robot, float x, float y, float v) {
((CelluloRobotWrapper*) robot)->setGoalPosition(x, y, v);
}
void clearTracking(int64_t robot) {
((CelluloRobotWrapper*) robot)->clearTracking();
}
void clearHapticFeedback(int64_t robot) {
((CelluloRobotWrapper*) robot)->clearHapticFeedback();
}
void setVisualEffect(int64_t robot, int64_t effect, int64_t r, int64_t g, int64_t b, int64_t value) {
((CelluloRobotWrapper*) robot)->setVisualEffect(effect, QColor(r, g, b), value);
}
void setCasualBackdriveAssistEnabled(int64_t robot, int64_t enabled) {
((CelluloRobotWrapper*) robot)->setCasualBackdriveAssistEnabled(enabled);
}
void setHapticBackdriveAssist(int64_t robot, float xAssist, float yAssist, float thetaAssist) {
((CelluloRobotWrapper*) robot)->setHapticBackdriveAssist(xAssist, yAssist, thetaAssist);
}
void reset(int64_t robot) {
((CelluloRobotWrapper*) robot)->reset();
}
void simpleVibrate(int64_t robot, float iX, float iY, float iTheta, int64_t period, int64_t duration) {
((CelluloRobotWrapper*) robot)->simpleVibrate(iX, iY, iTheta, period, duration);
}
float getX(int64_t robot) {
return ((CelluloRobotWrapper*) robot)->robot->getX();
}
float getY(int64_t robot) {
return ((CelluloRobotWrapper*) robot)->robot->getY();
}
float getTheta(int64_t robot) {
return ((CelluloRobotWrapper*) robot)->robot->getTheta();
}
int64_t getKidnapped(int64_t robot) {
Cellulo::CelluloBluetooth* robot1 = (Cellulo::CelluloBluetooth*) ((CelluloRobotWrapper*) robot)->robot;
return robot1->getKidnapped();
}
void destroyRobot(int64_t robot) {
qDebug() << "Removing callback...";
((CelluloRobotWrapper*) robot)->kidnappedCallback = nullptr;
qDebug() << "Calling deinit...";
((CelluloRobotWrapper*) robot)->deinit();
}
const char * getMacAddr(int64_t robot) {
return ((CelluloRobotWrapper*) robot)->robot->getMacAddr().toUtf8().constData();
}
int getConnectionStatus(int64_t robot) {
return ((CelluloRobotWrapper*) robot)->robot->getConnectionStatus();
}
/*
* Check number of robots
*/
int64_t robotsRemaining() {
if (client == nullptr) {
qDebug() << "Cannot call robotsRemaining() because initialize() was not called";
return 0;
}
return client->robots_N - used_robots;
}
int64_t totalRobots() {
if (client == nullptr) {
qDebug() << "Cannot call totalRobots() because initialize() was not called";
return 0;
}
return client->robots_N;
}

Event Timeline