Skip to main content

GmCompat

Trait GmCompat 

Source
pub trait GmCompat {
    const LAYOUT_HASH: u64;
    const TOTAL_SIZE: usize;
}
Expand description

The trait that defines a control program’s logic.

Implement this trait to create your control program. The associated Memory type should be the generated GlobalMemory struct from your project.

§Memory Type Requirements

The Memory type must implement Copy to allow efficient synchronization between shared memory and local buffers. This is automatically satisfied by the generated GlobalMemory struct.

§Lifecycle

  1. initialize is called once at startup
  2. process_tick is called repeatedly in the control loop with a TickContext that provides shared memory, the IPC client, and the current cycle number.

§Example

use autocore_std::{ControlProgram, TickContext};

mod gm;
use gm::GlobalMemory;

pub struct MyController {
    cycle_counter: u64,
}

impl MyController {
    pub fn new() -> Self {
        Self { cycle_counter: 0 }
    }
}

impl ControlProgram for MyController {
    type Memory = GlobalMemory;

    fn initialize(&mut self, mem: &mut GlobalMemory) {
        // Set initial output states
        mem.outputs.ready = true;
        log::info!("Controller initialized");
    }

    fn process_tick(&mut self, ctx: &mut TickContext<Self::Memory>) {
        self.cycle_counter = ctx.cycle;

        // Your control logic here
        if ctx.gm.inputs.start && !ctx.gm.inputs.estop {
            ctx.gm.outputs.running = true;
        }
    }
}

Compatibility constants for the GM segment a control program was generated against. Code-generated gm.rs implements this for GlobalMemory, so every control program carries the fingerprint of the exact layout it was built for. The runner checks these against the server’s SHM header before reading a single byte of the variable region (see ControlRunner::run).

Required Associated Constants§

Source

const LAYOUT_HASH: u64

Fingerprint of the GM layout (must equal the server’s SHM header hash).

Source

const TOTAL_SIZE: usize

Total GM segment size in bytes, including the header.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§