IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
Processor Systems, GNU/Linux and ControlApplications
Pavel [email protected]
CC BY-SA
Czech Technical University in PragueFaculty of Electrical Engineering
Department of Control Engineering
Motion control hardware developed and provided byPiKRON s.r.o.
ppisa,[email protected]
2016-10-09LinuxDays 2016
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
Content of Presentation
1 Introduction
2 Raspberry Pi, Real-time Kernel and DC MotorFully Preemptive KernelDC Motor Control by RPiRapid Prototyping with Matlab/Simulink
3 BLDC/PMSM Motor ControlBLDC Motor BasicBLDC Motor Control Realized by RPi with SPI FPGA PeripheralOther Projects
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
Background Introduction
The motion control and precise positioning is fundamental formany of medical, laboratory and robotic instruments andcontrollers to which development I have contributed to and oftencoordinated at PiKRON company. I use GNU/Linux as my mainhost system for about 20 years. We use it as the only hostdevelopment platform at company. We use it sometimes inembedded applications in parallel to RTEMS and bare metaldevelopment.GNU/Linux is extensively used as core network infrastructure atDepartment of Control Engineering and it is the main environmentfor all seminaries and lab works in subjects which I teach. We useLinux, RTEMS and other real-time operating systems forapplications and infrastructure development done for worldwidecar-makers and industrial partners as well.
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
Previous Presentations
InstallFest 2015
Is Raspberry Pi Usable for Industrial and RoboticApplications?http://installfest.cz/if15/slides/pisa_rpi.pdf
LinuxDays 2015
Linux, RPi and other HW for DC andBrushless/PMSM Motor Controlhttps://www.linuxdays.cz/2015/video/Pavel_
Pisa-Rizeni_stejnosmernych_motoru.pdf
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
Related Articles
RTLWS 2014, OSADL, Sojka, M. – Pısa, P.
Usable Simulink Embedded Coder Target for Linuxhttps://rtime.felk.cvut.cz/publications/public/ert_
linux.pdf
ROOT.CZ 9. 5. 2016
GNU/Linux pro rızenı a rychlost jeho odezvyhttps://www.root.cz/clanky/
gnu-linux-pro-rizeni-a-rychlost-jeho-odezvy/
ROOT.CZ 3. 10. 2016
Linux pro rızenı: minimalisticke resenı rızenıstejnosmerneho motoruhttps://www.root.cz/clanky/
linux-pro-rizeni-minimalisticke-reseni-rizeni-stejnosmerneho-motoru/
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
RPi and Other Experiments
Most recent theses I lead in motion control area
Radek Meciar: Motor control with Raspberry Pi board andLinux, 2014 (PDF)
Martin Meloun: FPGA Based Robotic Motion ControlSystem, 2014 (PDF)
Martin Prudek: Brushless motor control with Raspberry Piboard and Linux, 2015 (PDF)
Nepivoda Tomas: DC Motor Control Peripheral Module forZynq Platform, 2016 (PDF)
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
Electric Motors
main categories:
brushed DC – motor with mechanical commutatorbrushless DC motor (BLDC)/Permanent magnet synchronousmotor PMSM – comutator replaced by control electronics –needs position/sector sensor or position estimationstepper motor – synchronous motor, position usually enforcedby direct driveinduction or asynchronous motor – exact position is not strictlyrequired for control, torque is function of slip of rotor behindrotating magnetic field
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
Raspberry Pi Applications
facts
intended for educationprovides decent performance for video playbackis really cheap
the last point is important
used for many hobby projects, strong communityused even in commercial solutions due to low cost even that itis not intended for such use
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
Alternatives
Many better alternatives exists for full featured GNU/Linuxsystems for industrial applications.There are listed few ones
FreeScale i.MX53 – ARM Cortex A8, ETHERNET, CAN,USB, . . .
FreeScale i.MX6 – ARM Cortex A9
TI AM335x Sitara ARM Cortex-A8 (Beagle Bone black) –adds quadrature encoder inputs, two real time coprocessorunits (PRU)
Ti AM437x – Cortex A9 based, 2×PRU
Ti AM5728 – dual core Cortex A15, PowerVR GPU, 2×Cortex M4 cores, dual core C66x DSP, IVA (H.264), 2×PRU
If we speak about military and space then there even more durablesystems PowerPC, SPARC much broader range if Linux is notrequired/not an option. More about about alternatives later.
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
But We Focus on RPi for While
RPi is hardware bought by many students and hobbyist
quite often more powerful solution is found for initial dreammultimedia applications and boards are free for experimentsRPi can be bridge to a broad world of electronic tinkering,ideas prototyping and can open eyes people that there is muchmore to play with than virtual worlds and clouds
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
Fully Preemptive KernelDC Motor Control by RPiRapid Prototyping with Matlab/Simulink
Outline
1 Introduction
2 Raspberry Pi, Real-time Kernel and DC MotorFully Preemptive KernelDC Motor Control by RPiRapid Prototyping with Matlab/Simulink
3 BLDC/PMSM Motor ControlBLDC Motor BasicBLDC Motor Control Realized by RPi with SPI FPGAPeripheralOther Projects
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
Fully Preemptive KernelDC Motor Control by RPiRapid Prototyping with Matlab/Simulink
RPi 3.18.7-rt2 Latency Plot
OSADL.org – OSADL.org QA Farm Realtime – BCM2835 rack-b-slot-3cyclictest -l50000000 -m -n -a0 -t1 -p99 -i400 -h400 -q
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
Fully Preemptive KernelDC Motor Control by RPiRapid Prototyping with Matlab/Simulink
RPi Latency Long Term 3D
OSADL.org – OSADL.org QA Farm Realtime – BCM2835 rack-b-slot-3Long term latency trancing for organization members available
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
Fully Preemptive KernelDC Motor Control by RPiRapid Prototyping with Matlab/Simulink
Is It Enough?
The standard control application is motor servo control
Small DC and permanent magnet synchronous motorsmechanical time constant is between 3 and 10 msec (electricalone can be under 1 msec, but mechanical is prevalent forcontrol).
System, when controlled for position, is not stable (pureintegrator)⇒controller sampling period should be shorter thantime constant to achieve proper control
Maximal latencies under 200 µsec⇒RPi should be suitable forsuch control
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
Fully Preemptive KernelDC Motor Control by RPiRapid Prototyping with Matlab/Simulink
Outline
1 Introduction
2 Raspberry Pi, Real-time Kernel and DC MotorFully Preemptive KernelDC Motor Control by RPiRapid Prototyping with Matlab/Simulink
3 BLDC/PMSM Motor ControlBLDC Motor BasicBLDC Motor Control Realized by RPi with SPI FPGAPeripheralOther Projects
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
Fully Preemptive KernelDC Motor Control by RPiRapid Prototyping with Matlab/Simulink
System Events Rates
The 1 kHz control loop sampling frequency can be achieved byRPi
But RPi has no hardware for position (usually quadratureincremental) sensor interfacing
Incremental encoder output changes frequency can reach MHzunits
For 500 slots wheel and 4000 RPM the required frequency isabout 150 kHz
This is too much for user-space and even proper kernel spaceprocessing on RPi but it is nice experiment for system stabilityunder load testing
Serious solution requires to extend RPi by incrementalencoder interface implemented in hardware (FPGA)
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
Fully Preemptive KernelDC Motor Control by RPiRapid Prototyping with Matlab/Simulink
GPIO Only Based DC Motor Interfacing
3.3V 1
GPIO2 SDA 3
GPIO3 SCL 5
GPIO4 CLK 7
GND 9
GPIO17 11
GPIO27 13
GPIO22 15
3.3V 17
GPIO10 MOSI 19
GPIO9 MISO 21
GPIO11 SCLK 23
GND 25
2 5V
4 5V
6 GND
8 GPIO14 TX
10 GPIO15 RX
12 GPIO18 PWM
14 GND
16 GPIO23
18 GPIO24
20 GND
22 GPIO25
24 GPIO8 CE0
26 GPIO7 CE1
Raspberry Pi - P1
3.3V UART
1 GND
2 TX
3 3.3V
4 RX
CHB
CHA
IRC
HI DRV
LO DRV
IN
HI DRV
LO DRV
IN
DCMOTOR
Motor Power Supply
Czech Technical University in PragueDepartemet of Control Engineering FEERadek Mečiar and Pavel Píša 2014
RPi Motor Control Interface Prototype
As simple as possible Four NOR gates (SN74HCT02) H-bridge (L6203)
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
Fully Preemptive KernelDC Motor Control by RPiRapid Prototyping with Matlab/Simulink
Wire-wrapped Prototype Design
H-bridge (L6203) is located on R35PSR course DC motor kit
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
Fully Preemptive KernelDC Motor Control by RPiRapid Prototyping with Matlab/Simulink
Software IRC Signals Processing
GPIOs
Rasp
berr
y P
i
CHB
CHA
IRC
Hard IRQ
IRQ threadedhandler
Position calculation works better if derived from the order ofIRQs than from the signal values read in the handler.FIFO run queue preserve order! (Observation from MeciarRadek Motor control with Raspberry Pi board and Linux 2014bachelor thesis)
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
Fully Preemptive KernelDC Motor Control by RPiRapid Prototyping with Matlab/Simulink
IRC Kernel Driver
Simple character device /dev/irc0 which counts motorposition
Order of interrupts is converted to increment andaccumulated in the kernel variable
uint32 t (4-bytes) actual value is read from the device
driver source available at GitHub repositoryhttps://github.com/ppisa/rpi-rt-control
The core is a file kernel/modules/rpi gpio irc module.c
Test from user-space
modprobe rpi_gpio_irc_module
hexdump -e ’"%d" ’ /dev/irc0
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
Fully Preemptive KernelDC Motor Control by RPiRapid Prototyping with Matlab/Simulink
IRC Access from C Code
int irc_dev_fd;
int irc_dev_init(void)
irc_dev_fd = open(irc_dev_name, O_RDONLY);
if (irc_dev_fd == -1)
return -1;
return 0;
int irc_dev_read(uint32_t *irc_val)
if (read(irc_dev_fd, irc_val, sizeof(uint32_t))
!= sizeof(uint32_t))
return -1;
return 0;
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
Fully Preemptive KernelDC Motor Control by RPiRapid Prototyping with Matlab/Simulink
Boost IRC Kernel Threads Priority
The fully preemptive kernel runs even IRQ processing inthread context, all interrupts use priority 50 by default
modprobe rpi_gpio_irc_module
IRC_PIDS=$(ps Hxa -o command,pid | \
sed -n -e ’s/^\[irq\/[0-9]*-irc[0-9]_ir\][ \t]*\([0-
9]*\)$/\1/p’)
for P in $IRC_PIDS ; do
schedtool -F -p 95 $P
done
List threads by real-time priority
ps Hxa --sort rtprio -
o pid,policy,rtprio,state,tname,time,command
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
Fully Preemptive KernelDC Motor Control by RPiRapid Prototyping with Matlab/Simulink
Power Output – PWM
Only single PWM signal generator easily available onRaspberry Pi
Direction has to be controlled by GPIO pin
GPIO access possible through /sys interface
echo 22 >/sys/class/gpio/export
echo out >/sys/class/gpio/gpio22/direction
cat /sys/class/gpio/gpio22/value
echo 1 >/sys/class/gpio/gpio22/value
But access over /sys represent significant overhead
Direct access from users-pace to GPIO and PWM peripheralregisters is used instead
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
Fully Preemptive KernelDC Motor Control by RPiRapid Prototyping with Matlab/Simulink
Direct GPIO and PWM Registers Access
Implemented in int
rpi_peripheral_registers_map(void) function
The physical address range can be accessed from user-spaceby mmap() syscall
mem_fd = open("/dev/mem", O_RDWR|O_SYNC)
gpio_map = mmap(NULL, BLOCK_SIZE, PROT_READ|PROT_WRITE,
MAP_SHARED, mem_fd, GPIO_BASE);
Detailed description athttp://elinux.org/RPi_Low-level_peripherals
Fast GPIO and PWM access functions for controllerapplication can be found in files rpi gpio.c and rpi bidirpwm.cfound in appl/rpi simple dc servo example ofhttps://github.com/ppisa/rpi-rt-control repository
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
Fully Preemptive KernelDC Motor Control by RPiRapid Prototyping with Matlab/Simulink
PID Based Motion Controller
ENI
do_inp
do_genENG
D
P
I
MDerrchk
ME
do_conERC
ENR
MD2E
mutationcom−and
PWM
do_out
+cu
rren
t li
mit
MO
SF
ET
dri
ver
MotorDC
IRCAP
AS
RP
RS
ENE
MA, MSS1, S2 PIRC, PTPER
PTOFS, PTSHIFTPTVANG, PTINDX
PTPTR1/2
EP, GEN_STGEN_INFO
do_deb
DBG
Source: PXMC – Portable, highly eXtendable Motion Controllibrary, http://pxmc.org/, developed and used at PiKRON formore than 15 years already
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
Fully Preemptive KernelDC Motor Control by RPiRapid Prototyping with Matlab/Simulink
Controller Thread Latency Prevention
Implemented in file rpi simple dc servo.c
Next commands are available setpwm, readirc and runspeed.
The real time task cannot be swapped or code paged in ondemand
mlockall(MCL_FUTURE | MCL_CURRENT)
real time priority has to be used for control task
pthread_attr_t attr;
struct sched_param schparam;
pthread_attr_init(&attr);
pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
schparam.sched_priority = sched_get_priority_min(SCHED_FIFO);
pthread_attr_setschedparam(&attr, &schparam);
pthread_create(thread, &attr, start_routine, arg);
pthread_attr_destroy(&attr);
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
Fully Preemptive KernelDC Motor Control by RPiRapid Prototyping with Matlab/Simulink
The Controller Timing
The use CLOCK MONOTONIC for all control related timingis critical, CLOCK REATIME can skip forward and backwarddue to user or NTP adjustment
sample_period_nsec = 20*1000*1000;
clock_gettime(CLOCK_MONOTONIC, &sample_period_time);
do
sample_period_time.tv_nsec += sample_period_nsec;
if (sample_period_time.tv_nsec > 1000*1000*1000)
sample_period_time.tv_nsec -= 1000*1000*1000;
sample_period_time.tv_sec += 1;
clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME,
&sample_period_time, NULL);
/* Run timed actions there */
...
while(1);
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
Fully Preemptive KernelDC Motor Control by RPiRapid Prototyping with Matlab/Simulink
Ensure Proper Stop When Interrupted
void stop_motor(void)
rpi_bidirpwm_set(0);
void sig_handler(int sig)
stop_motor();
exit(1);
...
struct sigaction sigact;
memset(&sigact, 0, sizeof(sigact));
sigact.sa_handler = sig_handler;
sigaction(SIGINT, &sigact, NULL);
sigaction(SIGTERM, &sigact, NULL);
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
Fully Preemptive KernelDC Motor Control by RPiRapid Prototyping with Matlab/Simulink
The Controller Sample Time Step
Please consult application code for complete solution with overflowand anti-windup protection
err = (pos_req - actual_pos);
ctrl_i_sum += err * ctrl_i;
action = ctrl_p * err + ctrl_i_sum + ctrl_d *
(err - ctrl_err_last);
ctrl_err_last = err;
rpi_bidirpwm_set(action >> 8);
More information can at pages of DCE’s Real-Time systemsProgramming course http://support.dce.felk.cvut.cz/psr/
and subject’s Semestral Work – Motor Control pages. Othervaluable information on former subject pagehttps://support.dce.felk.cvut.cz/pos/hlavni_uloha/
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
Fully Preemptive KernelDC Motor Control by RPiRapid Prototyping with Matlab/Simulink
Test Results
Reliable speed control achieved for smaller speedsRPi capable to cope with almost full speed of PSR course DCmotor with low resolution IRC sensorThe speed limit is much lower for industry grade motor usedin real PiKRON’s applicationsThe RPi is capable to process about 28 000 IRQ events persecond but when more arrives the system and controller isoverloaded/blocked and motor runs out of controlBut even in overload case system is stable when motor isexternally braked/hold/slow down controller receives CPUtime again and system recovers from overloadSolution is nice for education but not safe for industrial useThe test with FPGA based IRQ processing in in preparationbut even in this case RPi is not considered by us as platformreasonable for industrial motion control applications
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
Fully Preemptive KernelDC Motor Control by RPiRapid Prototyping with Matlab/Simulink
Outline
1 Introduction
2 Raspberry Pi, Real-time Kernel and DC MotorFully Preemptive KernelDC Motor Control by RPiRapid Prototyping with Matlab/Simulink
3 BLDC/PMSM Motor ControlBLDC Motor BasicBLDC Motor Control Realized by RPi with SPI FPGAPeripheralOther Projects
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
Fully Preemptive KernelDC Motor Control by RPiRapid Prototyping with Matlab/Simulink
Embedded Real Time and the DCE Department
CTU FEE Department of Control Engineering has been and isinvolved in Matlab/Simulink real-time support from itsbeginning (origin of real-time toolbox can be trace to ourdepartment)
We have long term experience with fully preemptive kerneland hardware interfacing
Embedded Real-time Target has been adapted/partiallyrewritten by Michal Sojka to be usable for real applications(MathWork included embedded solutions are often Windowsonly and use POSIX timers and signals which haveuncontrolled latencies during delivery)
The blocks for SocketCAN, Humusoft data acquisition PCIcards and minimal set of RPi periferals has been implemented
COMEDI blockset has been updated and tested with ourLinux ERT version as well
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
Fully Preemptive KernelDC Motor Control by RPiRapid Prototyping with Matlab/Simulink
RPi DC Motor Control Simulink Diagram
sfIRCInput
IRC0
int32 -808
IRC-display
Convert
IRC int32 to Real
double
IRC-scope
-0.01
ManualPWM
double
sfPWMwDirOutput
PWMwDirManualPWM
Control
doubleOpt. PSDController
w
RSTs
r
red I
u
I
PSubsys PSD
double
0
PositionRequest
double
0
RESET
double
ActiveOutputRange
double-K-
AntiWindup
double
Position
double
Convert
To int
uint8
1.219
PWM-display
Pos
Trajectory
double
PWM
Double Clickto run model on RPi
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
Fully Preemptive KernelDC Motor Control by RPiRapid Prototyping with Matlab/Simulink
RPi DC Motor Control Simulink Prototype
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
Fully Preemptive KernelDC Motor Control by RPiRapid Prototyping with Matlab/Simulink
RPi DC Motor Control Simulink Remarks and Pointers
Incremental encoder input implemented as S-functionsfIRCInput.c which opens /dev/irc0 and reads actualposition from kernel driverBidirectional PWM output is implemented in S-functionsfPWMwDirOutput.c and uses same registers direct accessapproach as described in the previous sectionThe whole setup is documented on respective Lintarget/LinuxERT project page http://lintarget.sourceforge.net/
rpi-motor-control/index.html
used ert linux target and CC=arm-rpi-linux-gnueabihf-gcc setfor make rtw. scp and ssh are used to copy and run binary ontarget. Simulink external mode (parameters on-line tune andsignals scope windows) is available.The generated code performance is the same as for handwritten case – limitation is IRC events processing in the kernel
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
BLDC Motor BasicBLDCMotor Control Realized by RPi with SPI FPGA PeripheralOther Projects
Outline
1 Introduction
2 Raspberry Pi, Real-time Kernel and DC MotorFully Preemptive KernelDC Motor Control by RPiRapid Prototyping with Matlab/Simulink
3 BLDC/PMSM Motor ControlBLDC Motor BasicBLDC Motor Control Realized by RPi with SPI FPGAPeripheralOther Projects
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
BLDC Motor BasicBLDCMotor Control Realized by RPi with SPI FPGA PeripheralOther Projects
Three Phase Circuit Transformations
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
BLDC Motor BasicBLDCMotor Control Realized by RPi with SPI FPGA PeripheralOther Projects
Hall Sensors to Sectored Position
011010110
100101001
12
3
346°
16°
46°
76°
105°
134°166°
196°
225°
256°
278°
317° (962)
(45)
(128)
(211)
(292)
(373)
(461)
(544)
(626)
(711)
(798)
(881)
0°(0)
index
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
BLDC Motor BasicBLDCMotor Control Realized by RPi with SPI FPGA PeripheralOther Projects
Simplified PMSM Vector Control
+cu
rren
t li
mit
MO
SF
ET
dri
ver
+cu
rren
t li
mit
MO
SF
ET
dri
ver
ENI
do_inp
do_genENG
uA
uC
uB
+cu
rren
t li
mit
MO
SF
ET
dri
ver
iAi Bi C
PIRC, PTPERPTOFS
PTSHIFT
Forward
Clark
A,B,C
to
beta
alpha,
Forward
Park
alpha,
beta
to
D, Q
AP
AS
RP
RS
MA, MSEP, GEN_STGEN_INFO
PMSM
IRC
HAL
Current
ADC
do_out
betato
alpha
A,B,CPWM
Inv.
Park
to
alpha
beta
D,Q
do_conENR
Speed
and/or
position
control
Q
D
Q compoenent
controller
D component
controller
current D
current Q
Source: PiKRON PXMC library
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
BLDC Motor BasicBLDCMotor Control Realized by RPi with SPI FPGA PeripheralOther Projects
LX ROCON – DC, BLDC/PMSM and Stepper Controller
Source: PiKRONPavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
BLDC Motor BasicBLDCMotor Control Realized by RPi with SPI FPGA PeripheralOther Projects
LX ROCON – Features
Industry-proven Electric Motor Control libraries and system
FPGA-based Based on Cortex-4 MCU and Xilinx Spartan 6FPGA
Fully configurable, 16 power outputs can be assigned up to4Ö BLDC/PMSM, stepper motors and or up to 8Ö DC motor
FPGA design with inferred blocks only
Portable to MicroSemi FPGAs, does not use vendor-specificblocks
CAN, ETHERNET, Serial, RS-485 and USB communication
RTEMS, Nuttx and mbed supported
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
BLDC Motor BasicBLDCMotor Control Realized by RPi with SPI FPGA PeripheralOther Projects
LX ROCON – LX CPU Board
74LVC1G86
U19
LX_CPU2ANALOG IN
CAN term.
JTAG CPU
BOOT
USB PWR
USBhost
I2C
SERIAL
SDETH IRC4IRC3IRC2IRC1
100nC91
74HC1G08
U23
74HC1G08
U24
C89
100nC88
47K
R13
ADUM1201
IO4
ADUM1201
IO1
1u
C87
MBR0530D8JP14
100n
C79
LED-O
D7
LED-R
D6
LED-B
D5
SMBJ5.0AD4 100n
C70
CN19
R126
74HCT04
U21
CN18
100uC63
C59
100n
CN17CN16
32.768K-CFPX217
X2
22RR121
JP7
JP6
74LVC1G86
U20
74LVC112U18
24C64-OT
U17
CN15
CN14
M25P32U16 J
P4
100n
C47
10u
C43
C37100n
22u
C36
1n
C35
MCP1612
U15
22u
C33
DLJ4018-4.7
L4
22uC32
100nC30
MCP1612U14
JP322R
R83
22R
R37
R70
R69
R60 R48
R46
R43
TLC272
IO3
22RR117
CN12
LM3526M-L
U13
100uC24
10u
C22
100n
C21
100n
C20
MT48LC4M32B2PU2
JP2
22K
R95
10RR93
CN10
AM26LV32CD
U12
R81
CN9
AM26LV32CD
U11
22R
R72
R71
CN8
AM26LV32CD
U10
CN7
AM26LV32CD
U9
R47
R45
R44
R42 R41
R40
R39
R38
XC6SLX9-2TQG144
U8
CN6
TJA1050
U7ADUM1201
IO2
CN5
LED-YD3
LED-GD2
CFPS-39-50MU6 10u
C16
DP83848I
U5
4K87
R25
50RR24
50RR23
50RR2250RR21
CN4
BSS83PT2
CN3
12.00M-SMDXT324
X1
10KR18
1K5
R17
BSS138T1
100n
C3
CN2
CN1
22uC2
68KR4
22KR3
DLJ4018-4.7L2
JP1
LPC1788
U1
RS-232
uLANRS-485
CAN
SPITTL I/O
or UARTor SPI
USBdevice
LCDdisplay
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
BLDC Motor BasicBLDCMotor Control Realized by RPi with SPI FPGA PeripheralOther Projects
LX ROCON – Power Stage with CPLD Implemented ADC
Example: up to 4 BDLC/PMSM and IRC equipped or sensor-less stepper motor control (16× 5 A, up to 28 VDC, fully protected phase half-bridges)
One HBR Analog
8× to one AGL1258× PWM and ADC
Fast serial communicationto the second AGL125
and superordinate FPGA and MCUvector control system
DC In connectors– one on each rail end
Half bridge control, sense and power pins
Current and PWM D-Q transformations and commutation completely driven by Xilinx FPGA
Source: PiKRON
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
BLDC Motor BasicBLDCMotor Control Realized by RPi with SPI FPGA PeripheralOther Projects
Outline
1 Introduction
2 Raspberry Pi, Real-time Kernel and DC MotorFully Preemptive KernelDC Motor Control by RPiRapid Prototyping with Matlab/Simulink
3 BLDC/PMSM Motor ControlBLDC Motor BasicBLDC Motor Control Realized by RPi with SPI FPGAPeripheralOther Projects
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
BLDC Motor BasicBLDCMotor Control Realized by RPi with SPI FPGA PeripheralOther Projects
PMSM Experimental Setup
Source: Martin Prudek’s Bachelor Thesis
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
BLDC Motor BasicBLDCMotor Control Realized by RPi with SPI FPGA PeripheralOther Projects
Motor, Boards and Interconnection
MotorMotordriver
Expansion unit with
FPGA
Raspberry Pi+ Linux 3.18-RT
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
BLDC Motor BasicBLDCMotor Control Realized by RPi with SPI FPGA PeripheralOther Projects
Function Placement to the Borads
Raspberry Pi
SPI
Motor
IRC
3 phase
stator windigns
Hall ef ect
sensors
Motor driver
Half-bridges
FPGA
Expansion unit
ADC
Current
measurments
CPU
CLK
generator
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
BLDC Motor BasicBLDCMotor Control Realized by RPi with SPI FPGA PeripheralOther Projects
FPGA Functional Blocks
SPI slave PWMcounter
mcpwm1 mcpwm2 mcpwm3
adc_reader
ADC output Hall ef ect sensors output
input of half-bridges
IRC output
CLK gen. output (50MHz)
qcounter
I/Opins
posit
ion
Hall
1H
all2
Hall3
Ch0
Ch1
Ch2
Irc_A
Irc_B
phase
1
phase
3
phase
2
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
BLDC Motor BasicBLDCMotor Control Realized by RPi with SPI FPGA PeripheralOther Projects
RPi PMSM Motor Control Simulink Diagram
Raspberry Pi Permanent Magnet Synchronous Motor Control
PMSM motor is connected to 3p-motor-driver designed by PiKRON. SPI connected rpi-mi-1 FPGAinterface is used for IRC, HAL and current ADC input and for PWM generation. FPGA designby Martin Prudek. The simplified control algorithm in based on PiKRON's PXMC motion controllibrary sources by Pavel Pisa. More about DCE FEE CTU Linux ERT target and projects at
http://lintarget.sourceforge.net
Simpler version withoutd-componet compensation
-2
IRC-display
IRC-scopesfPMSMonSPI
PMSMonSPI
int32
int32
int32
double (3)
int32
Opt. PSDController
w
RSTs
r
red I
u
I
PSubsys PSD
double
0
PositionRequest
double
0
RESET
double
ActiveOutputRange
double-K-
AntiWindup
double
Position
double
Pos
Trajectory
double
Double Clickto run model on RPi
Current
3
[1x3]
PWM_VAL
double (3)
[1 1 1]
PWM_EN
double (3)
-38
132
-95
Current-display
round
Round1
3
double (3)
[1x3]
Current_offs
double (3)
3
double (3)K*u
Gain
double (3)
ManualPWM SW
3double (3)
[0.0 -0.0]
PWM_VAL1
double (2) alp,beta,b,c
Inv Clarke
double (3)
a, b, calp, bet
Clarke
3
double (2)-56
196
Current-display1
round
Round
2
double (2)
d,q
angle rad
alp,bet
Inv Park
double (2)
ManualAlp bet
2double (2)
[0.0 0.0]
PWM_VAL2
double (2)
ManualD Q
2double (2)
double (2)0
d=0
double
1000
IRC per com
uint32
irc_val
irc_indx
irc_occur
hal
irc_per_com
irc_ph_shift
phase rad
IRC to phase
single[phase_rad]
phase_rad
[phase_rad]
phase_rad 1
single
62
Com phase deg
-K-
Gain1
singleround
Rad 2 Deg
single
0.011204867108941
Current-display2alp, bet
angle rad
d, q
Park
double (2)2
[phase_rad]
phase_rad 2
single
Current1
0
IRC-speed
int32
Convert
IRC int32 to Real
double
z
1
UnitDelay
int32
55
IRC Phase Shift
double
-860
Index Pos
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
BLDC Motor BasicBLDCMotor Control Realized by RPi with SPI FPGA PeripheralOther Projects
RPi PMSM IRC Plot
0 2 4 6 8 10 12 14 16 18 20
−2000
−1500
−1000
−500
0
500
1000
1500
2000
IRC Inputs
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
BLDC Motor BasicBLDCMotor Control Realized by RPi with SPI FPGA PeripheralOther Projects
RPi PMSM Current A, B, C Plot
0 2 4 6 8 10 12 14 16 18 20−1500
−1000
−500
0
500
1000
1500Current A, B, C
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
BLDC Motor BasicBLDCMotor Control Realized by RPi with SPI FPGA PeripheralOther Projects
RPi PMSM IRC Current D, Q
0 2 4 6 8 10 12 14 16 18 20
−2000
−1500
−1000
−500
0
500
1000
1500
2000
Current DQ
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
BLDC Motor BasicBLDCMotor Control Realized by RPi with SPI FPGA PeripheralOther Projects
Outline
1 Introduction
2 Raspberry Pi, Real-time Kernel and DC MotorFully Preemptive KernelDC Motor Control by RPiRapid Prototyping with Matlab/Simulink
3 BLDC/PMSM Motor ControlBLDC Motor BasicBLDC Motor Control Realized by RPi with SPI FPGAPeripheralOther Projects
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
BLDC Motor BasicBLDCMotor Control Realized by RPi with SPI FPGA PeripheralOther Projects
SocketCAN Simulink Blockset
The blockset is quick proof port of the CAN Autosar APIbased blocks developed at DCE initially for own automotivegrade ARM Cortex-R4 based embedded platform
The code is generated under designed control of TLC (TargetLanguage Compiler) blocks description which allows tooptimize blocks code for used data-types and interconnection
For more information about embedded systems rapidprototyping support developed in our group look athttp://rtime.felk.cvut.cz/rpp-tms570/
Notices about more Linux and embedded hardware used,tested and even some designed look athttps://rtime.felk.cvut.cz/hw/
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
BLDC Motor BasicBLDCMotor Control Realized by RPi with SPI FPGA PeripheralOther Projects
x86 Linux ERT and Parallel Kinematic Robot Control
4 DC motors, 4 incremental encoders, other I/Os
Presented at Embedded world 2014
Sampling period 1 ms but complex computations
More reliable that previously used Windows target
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
BLDC Motor BasicBLDCMotor Control Realized by RPi with SPI FPGA PeripheralOther Projects
Cortex-R4 Automotive Platform and Test Board
TMS570LS3137 (EP) N2HET, no IRC specific peripheral
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
BLDC Motor BasicBLDCMotor Control Realized by RPi with SPI FPGA PeripheralOther Projects
TMS570 Motion Control for Safety Applications
TMS570LC4357 / RM57L843 ARM Cortex-R5F, two cores inlockstep, 4 MB Flash, 512 kB SRAM, 3 kB I, 3 kB D Cache,all memory with ECC, 2Ö QEP, N2HET 64TMS570LS1224 / RM46L840 ARM Cortex-R4F, lockstep,1280 kB Flash, 192 kB SRAM, no cache, all memory withECC, 2Ö QEP, N2HET 44No MMU, not reasonable target for Linux, when SDRAMused then it is without ECC and SDRAM is not well suitablefor critical applicationsFreescale/NXP alternative PowerPC based systems MPC57xx(eTPU)Ti tools with FreeRTOS, more or less demo for proprietarySAFERTOS, other uC/OS-II, CoDeSys, SCIOPTARTEMS BSP development started in GSoC frame, supportincludes chip initialization and lwIP based networking (notfully integrated yet)Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
BLDC Motor BasicBLDCMotor Control Realized by RPi with SPI FPGA PeripheralOther Projects
Industrial Motion Control Options
DSP Ti C2000, Freescale/NXP MC56F84XXX, MC56F82xxx
MCUs Ti TM4C ARM Cortex-M4F, STM32Nucleo+IHM02A1, Freescale/NXP Kinetis V ARMCortex-M0+/M4/M7
GNU/Linux capable systems Ti AM335x, Ti AM437x
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications
IntroductionRaspberry Pi, Real-time Kernel and DC Motor
BLDC/PMSM Motor Control
BLDC Motor BasicBLDCMotor Control Realized by RPi with SPI FPGA PeripheralOther Projects
Conclusion
More ready to be uses open-source building blocks for controlapplications have been presented and are available online
We are looking for students who has interest in real-time,operating systems and control/embedded hardware
We cooperate with more industrial partners on many projectsand students can gain experience and valuable knowledgeduring their work on the project in frame of thesis
We offer control related courses Real-Time systemsprogramming and participate on generic computerarchitectures courses at CTU FEE
Thanks for attention and questions
Pavel Pisa [email protected] CC BY-SA CPU, MCU, GNU/Linux and Control Applications