# Design and Development of an Embedded Controller for a Hydraulic Walking Robot WLBOT
## 4 Embedded Controller Software Design
### Realization of High-Speed Control Frequency  
PCM-3365 can only send 200 Hz commands via CAN bus, which is far from meeting the requirement of the motion control. Thus, we need to use the previous and the current commands sent by PCM-3365 to perform Lagrangian interpolation in the CAN receive interrupt. The Lagrangian polynomial is obtained and converted into the form required by the embedded controller, as shown in Equation (8).  
\[\theta_{n}=\frac{(\theta_{10}-\theta_{0})}{10}n+\theta_{0} \tag{8}\]  
where \(\theta_{n}\) is the interpolated joint angle command, \(\theta_{0}\) and \(\theta_{n10}\) is the previous and current joint angle command, \(n=1\;\sim\;9\).  
With the help of Lagrangian interpolation, nine values are inserted between the two commands sent by the PCM-3365, and the interval between the new interpolated commands is 500 \(\upmu\)s, which is consistent with the 2 kHz control frequency. The embedded controller utilizes \(\theta_{n}\) (\(n=0\;\sim\;9\)) to perform control algorithm calculation during the control interrupt to achieve high-speed control frequency.  
With the configuration of interrupt priority, it is ensured that the control interrupt has the highest priority, and the control interrupt and CAN receive interrupt can interrupt the ADC1-DMA and ADC3-DMA ISRs. Since DMA interrupt cycle is shorter than control interrupt cycle, the latest data is continuously written in the CAN send array in ADC-DMA ISRs. As a result, in the control interrupt, we make sure that the control algorithm can always utilize the latest information obtained by the sensors, which is vital to the real-time control of the robot.  
Due to the existence of interrupt nesting, we introduce a temporary storage array in ADC-DMA ISRs to reduce the impact of data loss if the filtering program is interrupted. Take ADC1-DMA interrupt as an example. When it comes to the ADC1-DMA ISR, we firstly perform median and mean filtering on the 20 data collected by each channel. Then the processed data for one channel will be temporarily stored in the temporary storage array. Finally, after all the data in seven channels is processed, we transfer all the data in a temporary storage array to the CAN send array for the control algorithm to use. In this case, the data in the CAN send array will be sent to PCM-3365 in the next CAN receive interrupt. The calculation of the control algorithm utilizes the interpolated commands and data in the CAN send array. Even if the executing filtering ISR is interrupted, we can't get the current complete ADC sampling data, we can still use the last ADC sampling data stored in the CAN send array to implement the control algorithm. Considering the current data and the last data just has a difference of about 360 \(\upmu\)s, the influence can be omitted.  
The running time of four ISRs is tested through an oscilloscope, as shown in Table 2. It's obvious that the control interrupt ISR is much shorter than the interrupt interval, which will reserve enough space for more complex control algorithms in the future.