Doly C++ SDK v1.00
Loading...
Searching...
No Matches
DriveControl/main.cpp

Demonstrates:

#include <chrono>
#include <thread>
#include <spdlog/spdlog.h>
#include <DriveControl.h>
#include <DriveEvent.h>
void onDriveComplete(uint16_t id) {
spdlog::info("Drive complete id={}", id);
}
void onDriveError(std::uint16_t id, DriveMotorSide side, DriveErrorType type) {
spdlog::error("Drive error id={} side={} type={}",
id, (int)side, (int)type);
}
void onDriveStateChange(DriveType driveType, DriveState state) {
spdlog::info("Drive state type={} state={}", (int)driveType, (int)state);
}
int main() {
// Setup spdlog
spdlog::set_level(spdlog::level::info); // Set as needed
spdlog::flush_on(spdlog::level::trace); // flush everything
// *** IMPORTANT ***
// Stop doly service if running,
// otherwise instance of libraries cause conflict
spdlog::error("Doly service stop failed");
return -1;
}
// get DriveControl version
spdlog::info("DriveControl Version:{:.3f}", DriveControl::getVersion());
// Register event listeners
// Initialize DriveControl with zero IMU offsets (example)
// For better performance provide actual calibrated IMU offsets.
// Check HelperExample for reading offsets from settings
if (DriveControl::init() != 0) {
spdlog::error("DriveControl init failed");
return -2;
}
uint8_t speed = 50; // example speed % (0..100)
// Example 1: go 100mm forward, brake at end (non-blocking)
DriveControl::goDistance(1, 100, speed, true, true);
// Example 2: Rotate 45 degrees counterclockwise on wheel, with brake at end (non-blocking)
//DriveControl::goRotate(2, -45, false, speed, true, true);
// Example 3: Go to X= -100mm, Y= 200mm, approaching forward with accelaration, without brake at end (non-blocking)
// DriveControl::goXY(1, -100, 200, speed, true, false, 50);
// Example 4: Free drive left and right wheels forward at specified speed for 2 seconds (non-blocking)
// DriveControl::freeDrive(speed, false, true); // Low-level drive right
// DriveControl::freeDrive(speed, true, true); // Low-level drive left
// drive for 2 seconds
// std::this_thread::sleep_for(std::chrono::seconds(2));
// Stop driving wheels
// DriveControl::freeDrive(0, false, true);
// DriveControl::freeDrive(0, true, true);
// Wait until done (simple polling example)
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
// Get final position
// Position calculated based on wheel odometry and IMU data,
// for better accuracy use predefined offsets in DriveControl::init()
spdlog::info("Robot pos x={} y={} head={}", pos.x, pos.y, pos.head);
// Cleanup
DriveControl::dispose(true); // dispose IMU as well
return 0;
}
Public API for Doly drive motion control.
Event dispatcher API for the Doly DriveControl subsystem.
DriveErrorType
Classification of a drive error.
Definition DriveEventListener.h:28
DriveType
Type/category of a drive operation.
Definition DriveEventListener.h:76
DriveState
High-level state of a drive operation.
Definition DriveEventListener.h:61
DriveMotorSide
Which motor side is associated with an error/event.
Definition DriveEventListener.h:46
@ RUNNING
Definition DriveEventListener.h:63
int8_t dispose(bool dispose_IMU)
Dispose/shutdown drive module and release resources.
Position getPosition()
Get current estimated position.
DriveState getState()
Get current drive state.
float getVersion()
Get current library version.
int8_t init(int16_t imu_off_gx=0, int16_t imu_off_gy=0, int16_t imu_off_gz=0, int16_t imu_off_ax=0, int16_t imu_off_ay=0, int16_t imu_off_az=0)
Initialize the drive control module.
bool goDistance(uint16_t id, uint16_t mm, uint8_t speed, bool toForward, bool with_brake=false, uint8_t acceleration_interval=0, bool control_speed=false, bool control_force=true)
Autonomous drive for a fixed distance (high-level).
void RemoveListenerOnError(void(*onError)(uint16_t id, DriveMotorSide side, DriveErrorType type))
Remove a previously registered error callback.
void AddListenerOnComplete(void(*onComplete)(uint16_t id))
Register a C-style callback for “command complete” events.
void RemoveListenerOnStateChange(void(*onChange)(DriveType drive_type, DriveState state))
Remove a previously registered state-change callback.
void AddListenerOnError(void(*onError)(uint16_t id, DriveMotorSide side, DriveErrorType type))
Register a C-style callback for error events.
void RemoveListenerOnComplete(void(*onComplete)(uint16_t id))
Remove a previously registered “command complete” callback.
void AddListenerOnStateChange(void(*onChange)(DriveType drive_type, DriveState state))
Register a C-style callback for drive state changes.
int8_t stopDolyService()
Stop the background Doly service (if running).