Files
qitech-control/docs/developer-docs/code-style-backend.md
T

3.7 KiB

Code Style

General Code styling

Try to adhere to the rust style guide: https://doc.rust-lang.org/nightly/style-guide/

Beckhoff terminals and Machines

For Beckhoff terminals you should first implement the basic functionality, then an Interpreter of the device/data is built on top. A good example of how to do this is by looking at the following code: el3021.rs , AnalogInputDevice

El3021 implements a so called AnalogInputDevice, which acts like an interface. This allows us to use AnalogInputDevice instead of having to always specify which device should be used. In Essence any Device that implements AnalogInputDevice can be used.

AnalogInputDevice is then used for creating an AnalogInput, which is plug and play for machines.

Then a further layer of Abstraction is used on top, where the data is interpreted and incorporated into a sort of control loop for certain functions, for example:

// ScrewSpeedController controlls the speed (frequency) and direction of the motor
// AnalogInput is used to collect measurements from the pressure sensor.
// screw_speed_controller.rs
#[derive(Debug)]
pub struct ScrewSpeedController {
    pub pid: ClampingTimeagnosticPidController,
    pub target_pressure: Pressure,
    pub target_rpm: AngularVelocity,
    pub inverter: MitsubishiCS80,
    pressure_sensor: AnalogInput,
    last_update: Instant,
    uses_rpm: bool,
    forward_rotation: bool,
    transmission_converter: TransmissionConverter,
    frequency: Frequency,
    maximum_frequency: Frequency,
    minimum_frequency: Frequency,
    motor_on: bool,