From 7f59364d3162c919d0eb20aa4af26739b637a0c9 Mon Sep 17 00:00:00 2001 From: Joseph Coffland Date: Wed, 23 Mar 2016 14:23:49 -0700 Subject: [PATCH] command.{c,h} cleanup --- src/canonical_machine.c | 3 ++- src/plan/command.c | 53 +++++++++++++++++------------------------ src/plan/command.h | 33 +++++++++++++++++++++++++ src/plan/planner.h | 4 ---- src/spindle.c | 2 +- src/stepper.c | 5 +++- src/stepper.h | 4 ---- 7 files changed, 62 insertions(+), 42 deletions(-) create mode 100644 src/plan/command.h diff --git a/src/canonical_machine.c b/src/canonical_machine.c index df55fd0..55f4058 100644 --- a/src/canonical_machine.c +++ b/src/canonical_machine.c @@ -109,8 +109,9 @@ #include "util.h" #include "usart.h" // for serial queue flush -#include "plan/arc.h" #include "plan/planner.h" +#include "plan/arc.h" +#include "plan/command.h" #include #include diff --git a/src/plan/command.c b/src/plan/command.c index 3cee846..6de36df 100644 --- a/src/plan/command.c +++ b/src/plan/command.c @@ -28,37 +28,28 @@ \******************************************************************************/ /* How this works: - * - The command is called by the Gcode interpreter (cm_, e.g. an M - * code) + * - A command is called by the Gcode interpreter (cm_, e.g. M code) * - cm_ function calls mp_queue_command which puts it in the planning queue - * (bf buffer). - * This involves setting some parameters and registering a callback to the - * execution function in the canonical machine - * - the planning queue gets to the function and calls _exec_command() - * - ...which puts a pointer to the bf buffer in the prep struct (st_pre) + * (bf buffer) which sets some parameters and registers a callback to the + * execution function in the canonical machine. + * - When the planning queue gets to the function it calls _exec_command() + * which loads a pointer to the bf buffer in stepper.c's next move. * - When the runtime gets to the end of the current activity (sending steps, - * counting a dwell) - * if executes mp_runtime_command... - * - ...which uses the callback function in the bf and the saved parameters in - * the vectors - * - To finish up mp_runtime_command() needs to free the bf buffer + * counting a dwell) it executes mp_runtime_command which uses the callback + * function in the bf and the saved parameters in the vectors. + * - To finish up mp_runtime_command() frees the bf buffer. * * Doing it this way instead of synchronizing on queue empty simplifies the * handling of feedholds, feed overrides, buffer flushes, and thread blocking, - * and makes keeping the queue full much easier - therefore avoiding starvation + * and makes keeping the queue full and avoiding starvation much easier. */ -#include "planner.h" +#include "command.h" #include "canonical_machine.h" #include "stepper.h" -#define spindle_speed move_time // local alias for spindle_speed to time var -#define value_vector gm.target // alias for vector of values -#define flag_vector unit // alias for vector of flags - - -/// callback to execute command +/// Callback to execute command static stat_t _exec_command(mpBuf_t *bf) { st_prep_command(bf); return STAT_OK; @@ -67,32 +58,32 @@ static stat_t _exec_command(mpBuf_t *bf) { /// Queue a synchronous Mcode, program control, or other command void mp_queue_command(cm_exec_t cm_exec, float *value, float *flag) { - mpBuf_t *bf; + mpBuf_t *bf = mp_get_write_buffer(); - // Never supposed to fail as buffer availability was checked upstream in the - // controller - if (!(bf = mp_get_write_buffer())) { + if (!bf) { cm_hard_alarm(STAT_BUFFER_FULL_FATAL); - return; + return; // Shouldn't happen, buffer availability was checked upstream. } bf->move_type = MOVE_TYPE_COMMAND; bf->bf_func = _exec_command; // callback to planner queue exec function bf->cm_func = cm_exec; // callback to canonical machine exec function - for (uint8_t axis = AXIS_X; axis < AXES; axis++) { - bf->value_vector[axis] = value[axis]; - bf->flag_vector[axis] = flag[axis]; + // Store values and flags in planner buffer + for (int axis = 0; axis < AXES; axis++) { + bf->gm.target[axis] = value[axis]; + bf->unit[axis] = flag[axis]; // flag vector in unit } - // must be final operation before exit + // Must be final operation before exit mp_commit_write_buffer(MOVE_TYPE_COMMAND); } void mp_runtime_command(mpBuf_t *bf) { - bf->cm_func(bf->value_vector, bf->flag_vector); // 2 vectors used by callbacks + // Use values & flags stored in mp_queue_command() + bf->cm_func(bf->gm.target, bf->unit); - // free buffer & perform cycle_end if planner is empty + // Free buffer & perform cycle_end if planner is empty if (mp_free_run_buffer()) cm_cycle_end(); } diff --git a/src/plan/command.h b/src/plan/command.h new file mode 100644 index 0000000..07272b1 --- /dev/null +++ b/src/plan/command.h @@ -0,0 +1,33 @@ +/******************************************************************************\ + + This file is part of the Buildbotics firmware. + + Copyright (c) 2015 - 2016 Buildbotics LLC + All rights reserved. + + This file ("the software") is free software: you can redistribute it + and/or modify it under the terms of the GNU General Public License, + version 2 as published by the Free Software Foundation. You should + have received a copy of the GNU General Public License, version 2 + along with the software. If not, see . + + The software is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the software. If not, see + . + + For information regarding this software email: + "Joseph Coffland" + +\******************************************************************************/ + +#pragma once + +#include "plan/planner.h" + +void mp_queue_command(cm_exec_t cm_exec, float *value, float *flag); +void mp_runtime_command(mpBuf_t *bf); diff --git a/src/plan/planner.h b/src/plan/planner.h index a7b3de4..cf9ee0b 100644 --- a/src/plan/planner.h +++ b/src/plan/planner.h @@ -307,7 +307,3 @@ void mp_copy_buffer(mpBuf_t *bf, const mpBuf_t *bp); // dwell.c functions stat_t mp_dwell(const float seconds); - -// command.c functions -void mp_queue_command(cm_exec_t cm_exec, float *value, float *flag); -void mp_runtime_command(mpBuf_t *bf); diff --git a/src/spindle.c b/src/spindle.c index d316c94..7cb4626 100644 --- a/src/spindle.c +++ b/src/spindle.c @@ -33,7 +33,7 @@ #include "hardware.h" #include "pwm.h" -#include "plan/planner.h" +#include "plan/command.h" static void _exec_spindle_control(float *value, float *flag); diff --git a/src/stepper.c b/src/stepper.c index f4f731f..450374b 100644 --- a/src/stepper.c +++ b/src/stepper.c @@ -29,8 +29,11 @@ #include "stepper.h" -#include "motor.h" #include "config.h" +#include "canonical_machine.h" +#include "plan/planner.h" +#include "plan/command.h" +#include "motor.h" #include "hardware.h" #include "util.h" #include "cpp_magic.h" diff --git a/src/stepper.h b/src/stepper.h index cca16f7..eafe195 100644 --- a/src/stepper.h +++ b/src/stepper.h @@ -29,11 +29,7 @@ #pragma once - -#include "config.h" #include "status.h" -#include "canonical_machine.h" -#include "plan/planner.h" #include -- 2.27.0