Magnet Control Software
Requirements
The ramps produced by the pi must be as regular as possible, and react to the trigger signal with minimal, reproducible, delay. While a delay of up to a few milliseconds can be compensated for, this is only true if the delay is at least predictable to within less than 100 microseconds. The pi should also be able to log readback values both during ramp and steady-state, but this must not impact the timing of the ramps. As a standard linux system, the raspberry pi is not ideal for this task, first and foremost because background processes can occupy the CPU at any given time, delaying the execution of other operations. The raspberry pi was chosen anyway because of its ease of use and customization.
Approach
The main approach to achieving these requirements is multithreading. The pi4 has 4 CPU cores, so can run 4 independent threads in parallel without impacting each threads function. The magnet control frontend runs several threads itself, but most of those are not extremely time critical and can share a core with each other or the linux system itself.
Classes
In addition to the MIDAS frontend itself, the code is organized mostly into two classes:
GpioHandler
This object handles one TRIGGER pin, one READY pin, and one FINISHED pin. It can be attached to one or two #MagPs objects.
MagPs
This abstract class has two implementations: AeTech and Sgx. During construction it can be set up to operate in different modes, independently for control and for monitoring. Not all modes are available for both sub-classes. The modes are:
- SCPI: Use the SCPI text interface (Sgx only).
- ADDA: Use the WaveShare AD/DA board (not fully tested for control, only used for readback)
- CAIO: Use the CAIO hat. (Would need hardware change to use for readback.)
Specifically for readback, it's possible to read back SCPI values, even if the general mode is set to ADDA.
Threads
These are the threads running one the pi at any given time, as well as what class they belong to, and whether they get a reserved core. Only the ramping threads get reserved cores, since only they are time-critical.
| Thread | Class | Duration | Purpose | dedicated core |
|---|---|---|---|---|
| Main | frontend | permanent | MIDAS interface, housekeeping | no |
| ReadValues | MagPs | permanent | readback from hardware | no |
| SendData | frontend | periodic | transfer readback values to MIDAS | no |
| ArmedLoop | frontend | while armed/ramping | start (and wait for) ramp threads | no |
| RampOnTrg | GpioHandler | while armed/ramping | ramp PS on trigger | yes |
| WaitThread | GpioHandler | while armed/ramping | ramp PS on trigger (if two PS on same GPIO) | yes |
Reserving cores and Governor
On a linux system like the raspberry pi, running threads on dedicated cores consists of two steps:
- marking a core as unavailable for system-assigned processes
- manually assign this reserved core to a given thread
On the raspberry pi, step 1 is done by adding the isolcpus directive to /boot/firmware/cmdline.txt, e.g.
console=serial0,115200 console=tty1 ip=dhcp rw root=/dev/nfs panic=60 isolcpus=2-3
Step 2 is accomplished with the pthread_setaffinity_np call in the C++ code.
As we discovered when testing the timing of this system, it's helpful to change one more system setting:
By default, the system is set up to scale down the CPU frequency when idle. This means it may "notice" the trigger signal slower than desired. In order to fix this, the so-called governor needs to be switched from this default setting ondemand to the setting performance, which keeps the CPU at maximum frequency regardless of load.
Since this is mostly relevant to the GPIO handling, it made sense to add the relevant function call to the constructor and destructor of GpioHandler. This way the system should revert to ondemand when the frontend is not running, reducing power consumption and heat.