Skip to content

C++ Motor Control: Integrating SOLO into Your Application

Knowledge & Insights

Introduction

A single blocking serial read inside your control loop can stall a motor for 50 ms or more. On the desk that looks fine. Inside a real machine, that stall is a jerk, a missed deadline, or a fault. Running a SOLO example is one thing. Integrating the drive into a production C++ motor control application is another.
This guide is about that second part: linking the library into your build, running the control loop in its own thread, handling communication loss, and shutting down the drive cleanly. We focus on the integration patterns, not the hello-world. For the entry-level walkthrough, start with our C++ brushless motor guide and come back when you are ready to ship.

The SOLO C++ Library

SOLO ships a C++ library that wraps the drive’s command set behind plain method calls. You set a target torque, speed, or position, you read back current, voltage, and faults, and the library handles the framing on the wire. It speaks both UART and CAN, so the same API works whether you talk over a serial port or a CAN bus.
The library is the integration layer you build on. It is small on purpose. It does not run threads, manage reconnection, or decide your control strategy. Those choices belong to your application, which is the point of this article. If you have not built a first working sketch yet, the C++ brushless motor guide covers installation and a minimal program. Here we assume you can already command a motor and want to do it well inside a larger system.
Takeaway: the library gives you clean access to the drive, and leaves the architecture to you.

Adding SOLO to Your Build

Before any threading or error handling, the library has to compile and link into your project. How you do that depends on your build system and the interface you pick.

CMake / Linking

Most production C++ projects use CMake, so wire the library in there. Add the SOLO sources or the installed library to your target, and expose its headers on your include path. A typical setup looks like this.

text
add_subdirectory(SOLOMotorControllersCppLibrary)

add_executable(my_app src/main.cpp src/motor_driver.cpp)

target_link_libraries(my_app PRIVATE SOLOMotorControllers)

Keep the SOLO library as its own target rather than copying source files into your tree. That makes updates a single pull instead of a manual merge. On Linux you also link the serial or CAN dependencies the library needs, which we cover in our SOLO on Linux guide.

UART vs CAN Backend (incl. KVASER)

The library can reach SOLO over UART or CAN, and that choice shapes the rest of your integration. UART is the simplest: one port, one drive, easy to debug. CAN scales to many drives on one bus and handles noise and distance better, which is why most multi-axis and industrial setups use it.

Diagram of versatile communication topology over UART, CAN and EtherCAT for a motor controller

On a PC you usually reach the CAN bus through a USB adapter. The library supports the KVASER USB-to-CAN family, so you open the Kvaser channel and pass it to the driver instead of a serial port. The rest of your code stays the same, which is the benefit of going through the library rather than the raw bus.

Kvaser USB-to-CAN interface adapter used to connect a PC to the SOLO CAN bus

As a rule, prototype on UART for fast feedback, then move to CAN for the deployed system. For the converter hardware and wiring, see our guide on connecting SOLO to a PC over USB.
Takeaway: pick UART for simple single-drive work, CAN for scale and robustness, and let the library hide the difference.

Structuring the Integration

A clean integration keeps the motor code separate from the rest of your app. The drive should look like a small, well-behaved object to the code around it, not a tangle of serial calls spread through your logic.

Closed-loop block diagram showing the program, controller, driver, motor and encoder

A Thin Wrapper Around the Driver

Wrap the SOLO object in a thin class of your own. Expose only the actions your application needs, like setTorque(), getSpeed(), or safeStop(), and hide the raw library calls behind them. This gives you one place to add logging, retries, and unit conversions, and it keeps the SOLO API from leaking into the rest of your code.
The wrapper also makes testing easier. You can swap in a mock that returns canned values, so the rest of your app can be tested without a motor on the bench. Keep it small. Its job is to translate your application’s intent into SOLO commands, nothing more.

Threading the Control Loop

Run the control loop in its own thread. A motor needs commands and feedback at a steady rate, often 100 Hz to 1 kHz, and you cannot get that timing if the loop shares a thread with your UI or network code. Give the loop a dedicated thread, set a fixed period, and let it command the drive and read feedback on every tick.
Pass setpoints into that thread through a lock-free queue or an atomic variable, and copy telemetry out the same way. The rest of your app then talks to a shared state, not to the serial port. This is the core pattern of real-time C++ motor control: one thread owns the drive, everyone else talks to it through data, not direct calls.

Keeping I/O off the Hot Path

The control loop must not block. Every UART or CAN transaction takes time, and if you wait on slow or optional I/O inside the loop, you blow the loop period. Keep logging, file writes, and network sends out of the hot path.
A simple rule works well: the loop does only what it must do this tick, which is read feedback, compute, and send one command. Push everything else, like logs and dashboards, to a separate lower-priority thread that drains a buffer. If a log write stalls, the motor keeps running.
Takeaway: one thread owns the drive at a fixed rate, and all slow work lives somewhere else.

Robust Error Handling

Communication will drop at some point. A cable works loose, a connector vibrates, a USB adapter resets. A production integration plans for that instead of crashing. The goal is to detect the problem fast, put the motor in a safe state, and recover when the link returns.

Detecting Communication Loss

Check the result of every read and write. The SOLO library reports whether a command succeeded, so do not ignore that return value. If a read fails, or several reads in a row time out, treat the link as down rather than trusting stale data.
A watchdog makes this clean. Track the time of the last good exchange, and if it passes your limit, say 100 ms, raise a communication-loss event. Acting on a counter of consecutive failures is generally more stable than reacting to a single timeout, which can be a transient glitch.

Reading and Clearing Faults

SOLO keeps an error register that tells you why the drive stopped, such as over-current, over-voltage, or over-temperature. Read it whenever a command fails or the motor behaves oddly. The register turns a vague “it stopped” into a specific cause you can log and act on.
Do not clear a fault blindly in a loop. Read the error first, decide whether the condition is safe to reset, and only then clear it. Clearing over and over without fixing the cause just hides a real problem, like a motor that is genuinely too hot.

Reconnect and Safe Stop

When the link drops, command a safe stop first, then try to reconnect. Do not hammer the port. Back off between attempts, for example retry after 100 ms, then 500 ms, then a second, so you do not flood a recovering bus. When the connection returns, re-read the drive’s state and clear any faults before you resume control.
Reconnect logic belongs in your wrapper, not scattered through the app. The control loop asks for a command, the wrapper handles the broken link underneath, and the rest of your code only sees “connected” or “stopped.” That keeps failure handling in one tested place.
Takeaway: watch every transaction, read the error register for the real cause, and stop the motor before you reconnect.

Lifecycle and Clean Shutdown

Starting and stopping the drive in the right order matters as much as the control loop. On startup, open the port or CAN channel, configure the motor parameters, clear any stale faults, then enable the drive. Enabling before the configuration is set can produce a jerk or an immediate fault.
On shutdown, reverse it. Ramp the command to zero, disable the drive, then close the link. Here you make a real choice: freewheel or hold. Disabling the drive lets the rotor freewheel, which is fine for a fan or wheel that can coast. If the load must not move, command a controlled stop and keep the drive holding until the system is safe. Either way, never kill the process with the drive still enabled, because an unmanaged stop can damage the load or the motor.
Wrap this in your class. A constructor that brings the drive up and a destructor that takes it down means a clean shutdown happens by default, even when an exception unwinds the stack.

Mapping App Commands to SOLO

Your application thinks in its own terms: “move to position X,” “hold torque Y,” “run at speed Z.” The integration layer maps those intents onto SOLO’s control modes. Pick the mode that matches the command, set the matching target, and let the drive close the loop.

App commandSOLO control modeWhat you set
Hold a force or loadTorque modeTarget current / torque
Run at a set speedSpeed modeTarget speed (RPM)
Move to a pointPosition modeTarget position

Switch modes deliberately, not mid-motion, because changing mode while the motor is loaded can cause a step in the command. Ramp down, change mode, then ramp the new target. If you drive SOLO over CANopen, these modes map onto the standard CiA 402 drive profile, which gives you a portable, vendor-neutral state machine.
Takeaway: choose the control mode per command, set one target, and switch modes only at rest.

How SOLO Supports C++ Integration

SOLO is built to be integrated, not just operated. The C++ library gives you a documented API over UART and CAN, the same controller handles DC, BLDC, PMSM, and AC induction motors, and the command set stays consistent across the range. That means the integration patterns in this article, the thin wrapper, the dedicated loop thread, the error-register checks, carry over as your project grows from one axis to many.
The library, examples, and protocol docs live together so your team can move from a first test to a deployed app without guessing. You can browse the libraries and documentation on the SOLO resources page and pull the C++ library straight into your build.

Conclusion

Integrating SOLO into a real C++ application comes down to a few disciplined choices. Link the library cleanly through CMake, and pick UART for simple work or CAN, including a KVASER adapter, for scale. Run the control loop in its own thread at a fixed rate, and keep all slow I/O off that hot path. Watch every transaction, read the error register for the real cause of a fault, and command a safe stop before you reconnect. Bring the drive up and down in order, and decide between freewheel and hold on shutdown. Get those right and the motor becomes a reliable part of your system instead of a source of surprises. To pull the C++ library and the docs into your next build, start from the SOLO resources page.

FAQ

How do I integrate the SOLO C++ library into my project?

Add the SOLO library as its own target in CMake, link it into your executable, and expose its headers on your include path. Then wrap the SOLO object in a thin class that exposes only the actions your app needs. On Linux you also link the serial or CAN dependencies the library uses.

How do I run the SOLO control loop in its own thread?

Give the loop a dedicated thread with a fixed period, often between 100 Hz and 1 kHz. On every tick it reads feedback, computes, and sends one command. Pass setpoints in and telemetry out through a lock-free queue or atomic variables, so the rest of your app talks to shared state instead of the port.

How do I recover from a dropped CAN/UART connection in C++?

Check the return value of every read and write, and use a watchdog on the time since the last good exchange. When the link drops, command a safe stop, then reconnect with a backoff between attempts. On reconnect, re-read the drive state and clear any faults before you resume control.

Can I use a KVASER adapter with the C++ library?

Yes. The library supports KVASER USB-to-CAN adapters. You open the Kvaser channel and pass it to the driver instead of a serial port, and the rest of your control code stays the same.

Tags:C++IntegrationSDK

Browse all categories

Drive your next innovation today

Discover our High-Performance Motor Controllers, Test Benches, and Services trusted worldwide.