When it comes to controlling the Position of a Brushless DC (BLDC) or a Permanent Magnet Synchronous Motor (PMSM) that are also known as BLAC using Raspberry Pi, you will need a Motor controller like SOLO that can deal with High powers injection into the Motors while controlling the Motor by getting commanded by Raspberry Pi through data lines like ( UART, SPI, …) that Raspberry Pi offers, as you might know, Raspberry Pi by its original hardware is not capable of driving Electric motors for two main reasons:
1 Raspberry Pi modules are just little computers or let’s say brains of your systems, they are great in processing arithmetic tasks and for programming, but by no means they have the capability or power to drive a relatively powerful motor electrically.
2 Raspberry Pi modules or their recent chips, are not well suited for offering extremely fast and real-time processing in the range of microseconds, remember that here we are talking about execution of thousands of lines of codes in C language, un-interrupted with fixed sampling rate for Analogue readings, so it’s not the same story as the total MIPS or RISC processing power of a CPU and SOLO manages all this by offering a unique Dual-core architecture with parallel processing, so finally Raspberry Pi modules are great for high level programming which are by nature not suitable for very fast and timely deterministic systems ( like a motor controller), and even if we imagine they can, implementing a modern motor controlling algorithms on them like FOC will be very challenging and time consuming.
For the mentioned reasons, a Raspberry Pi will be able to control a Motor once it comes alongside with a Motor controller like SOLO, that offers UART communication which exist on any Raspberry Pi modules with an extensive library of Python that offers all the available functions to basically read or set any available variable that is offered by SOLO inside a Python IDE or Shell within the Raspberry Pi Operating System, this Python library will enable your Raspberry Pi to do it’s main task which is more at operating system level by sending high level commands to SOLO like setting the desired Torque, Speed or Position of the Motor and SOLO will take care of the rest itself. So basically Raspberry Pi can play a supervisory and high-level role and SOLO will do all the hard work to keep the Motor under control and follow the commands from Raspberry Pi.
In this Article, we are going to demonstrate how to enable your Raspberry Pi to control the Position of the shaft a PMSM motor or any types of Brushless motors using Incremental Encoders while controlling at the same time the Torque and Speed of the trajectory while it’s following the Position Reference, so let’s put it in this way, for Brushless Motors of any type (AC or DC) using FOC algorithm as SOLO offers, whenever you want to control the Position of the shaft of a Motor, the Controller (here SOLO) has to take care of Torque and Speed as well as Position control so as the result you will have control over all these variable or let’s say you will have three degrees of freedom, so you can ask the Motor Controller to go to a Fixed Position reference, while keeping the Speed and Torque constant ( or variable), this is a great feature as it gives the maximum flexibility to the users to virtually control any aspect of their systems.
to achieve Position Controlling with Raspberry Pi and SOLO you need to deal with 3 control loops that are participating in this architecture and each of them are having their own part as explained in brief below:
1- The Torque Loop: This is the closest controller to the motor and the fastest one, it will only control the Torque of the Motor regardless of the Speed, The Speed depends on how much Load you have on the shaft of the Motor, so the difference between the Motor’s Torque and Load’s Torque will define the final Speed of the rotor of the motor.
2- The Speed Loop: The Speed loop comes behind the Torque Loop, it is slower than the Torque controller (at least around 10X) and it basically tries to fix the Speed on a desired value, this controller will keep the speed fixed regardless of the load variation and it will adjust the torque accordingly. You might ask why the Speed controller comes after the Torque controller, this is a big topic, but to simplify it drastically the in principle an Electrical Motor is a Torque Generation machine, even the Speed is controlled finally by controlling the Torque, so the speed controller when it tries to command, it commands to Torque Controller to increase or decrease the Torque on the Motor to stabilize the motor on a specific Speed that it desires!
3- The Position Loop: this is the last loop coming behind the speed loop, it basically tries to set the exact position of the Motor on a specific value, so to make this loop functional, you need to make sure the Torque and Speed loop are firstly tuned and ready, then you can do the Position Controlling.
If you are interested to know more about this technique and what is happening behind the scene you can have a look on the video and article below:
https://www.solomotorcontrollers.com/position-controlling-brushless-motor-incremental-encoders-using-solo/
The Wiring
The only wiring you need to apply will be the connection of the Motor to SOLO and by just connecting UART_TX, UART_RX and GND lines between SOLO and Raspberry Pi as below, however you also need to Calibrate and Connected properly your Encoder and Motor’s wiring to SOLO based on this Article before proceeding to code execution on Raspberry Pi.
SOLO UNO Data-line Wiring to Raspberry Pi
SOLO UNO wiring
SOLO MINI wiring
SOLO’s Setup
![IMG3_Closed_loop_Config_SOLO Raspberry Pi Brushless Motor Position Control using SOLO and Incremental Encoders [+ Code] 3](https://www.solomotorcontrollers.com/wp-content/uploads/2021/01/IMG3_Closed_loop_Config_SOLO-scaled.jpg)
The Motor
The Motor used in this example is “m-2310P-LN-04K” and you can download its datasheet from here. Practically this motor is a brushless motor with sinusoidal shape BEMF and they are known also as Brushless AC or PMSM motors. This Motor has 8 Poles with an internal Encoder with 1000 physical lines pre-quad, meaning that in 1 turn rotation SOLO will read 4000 pulses generated by this motor’s encoder in quadrature manner ( 4 times more)
![IMG1_BLAC_Motor_IMG Raspberry Pi Brushless Motor Position Control using SOLO and Incremental Encoders [+ Code] 4](https://www.solomotorcontrollers.com/wp-content/uploads/2021/01/IMG1_BLAC_Motor_IMG.jpg)
The Encoder and Motor Calibration
If this is the first time you are connecting this motor to your SOLO, you need to make sure at least for 1 time, you have correctly calibrated the Encoder with correct connection of the Motor’s windings to ABC outputs of SOLO, to do that you need to read the article below and do the steps there at least 1 time, and as long as your Motor or the Encoder is the same or the Encoder is not mechanically displaced with respect to the Motor ( in case of using portable encoders). All the calibrated parameters will reside in long term memory of SOLO and they will be remembered after power recycling.
https://www.solomotorcontrollers.com/how-to-connect-calibrate-incremental-encoder-with-solo/
The Code
The Results
In This code as you can see, after setting up the essential parameters like the Motor’s number or Poles, Encoder lines and etc within the Python code in Raspberry Pi, we are periodically asking SOLO from the Raspberry Pi to spin the motor into two different positions of +50000 pulses (Green Plot) with 5000 RPM of Speed (Red Plot) and stay there for a short time, then turning back the Motor into -32559 pulses with 1500 RPM of speed.
It worth mentioning that when SOLO is in Position Mode, it counts the pulses in quadrature manner, so the feedbacks and the references are all in quadrature form, for instance in this example when we ask SOLO to traverse the shaft of the motor into +50000 pulses, if we consider the initial starting point is at zero, we are spinning the motor for (+50000 / ( 10000 * 4)) = 12.5 turns or 4500.0 degrees, so like this you can calculate the exact end point and then provide sufficient and desired reference to SOLO to reach your goal. As you can see in Figure 1, the Motor went all the way up to near +50000 pulses (Green Plot) with requested speed of 5000 RPM (Red Plot), however on Figure 2 the next goal with different speed of 1500 RPM and final position of -32559 is achieved.
![IMG1_Position_Control_Brushless_1 Raspberry Pi Brushless Motor Position Control using SOLO and Incremental Encoders [+ Code] 5](https://www.solomotorcontrollers.com/wp-content/uploads/2021/04/IMG1_Position_Control_Brushless_1.png)
Figure 1 – Position Controlling with fixed speed of 5000 RPM and goal of +50000 pulses.
![IMG2_Position_Control_Brushless_2 Raspberry Pi Brushless Motor Position Control using SOLO and Incremental Encoders [+ Code] 6](https://www.solomotorcontrollers.com/wp-content/uploads/2021/04/IMG2_Position_Control_Brushless_2.png)
Figure 2 – Position Controlling with fixed speed of 1500 RPM and goal of -32559 pulses.
Another thing we discussed before was the simultaneous Torque, Speed and Position control that is offered by SOLO since it operates in Field Oriented Control (FOC) manner, and as can be seen in Figure 3 below, once SOLO asked through Raspberry Pi to follow the position reference, there were some Torque disturbances on the shaft of the Motor that applied artificially by us, and as you can see SOLO automatically rose the amount of Torque ( Blue Plot) while trying to keep the Speed constant at 5000 RPM and followed the trajectory with maximum available and allowed Torque, you can always limit the Max Torque allowed by SOLO by limiting the Current through CurrentLimit variable or through Motion Terminal.
![IMG1_PositionControl_RaspberryPI_DisturbanceRejection Raspberry Pi Brushless Motor Position Control using SOLO and Incremental Encoders [+ Code] 7](https://www.solomotorcontrollers.com/wp-content/uploads/2021/06/IMG1_PositionControl_RaspberryPI_DisturbanceRejection.png)
Figure 3- Simultaneous Torque, Speed and Position Control offered by SOLO commanded by Raspberry Pi.
If you have any questions or doubts regarding this article you can ask us in our Forum.