pub trait ControlProgram {
type Memory: Copy + ChangeTracker;
// Required method
fn process_tick(&mut self, mem: &mut Self::Memory, cycle: u64);
// Provided method
fn initialize(&mut self, _mem: &mut Self::Memory) { ... }
}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
initializeis called once at startupprocess_tickis called repeatedly in the control loop
§Example
use autocore_std::ControlProgram;
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, mem: &mut GlobalMemory, cycle: u64) {
self.cycle_counter = cycle;
// Your control logic here
if mem.inputs.start && !mem.inputs.estop {
mem.outputs.running = true;
}
}
}Required Associated Types§
Sourcetype Memory: Copy + ChangeTracker
type Memory: Copy + ChangeTracker
The shared memory structure type (usually the generated GlobalMemory).
Must implement Copy to allow efficient memory synchronization.
Required Methods§
Sourcefn process_tick(&mut self, mem: &mut Self::Memory, cycle: u64)
fn process_tick(&mut self, mem: &mut Self::Memory, cycle: u64)
The main control loop - called once per scan cycle.
This is where your control logic lives. Read inputs from mem,
perform calculations, and write outputs back to mem.
§Arguments
mem- Mutable reference to a local copy of the shared memory. Changes made here are written back to shared memory after this method returns.cycle- The current cycle number (increments each tick, starting at 1).
§Timing
This method should complete within the scan cycle time. Long-running operations will cause cycle overruns.
Provided Methods§
Sourcefn initialize(&mut self, _mem: &mut Self::Memory)
fn initialize(&mut self, _mem: &mut Self::Memory)
Called once when the control program starts.
Use this to initialize output states, reset counters, or perform any one-time setup. The default implementation does nothing.
§Arguments
mem- Mutable reference to the shared memory. Changes are written back to shared memory after this method returns.