command.{c,h} cleanup
authorJoseph Coffland <joseph@cauldrondevelopment.com>
Wed, 23 Mar 2016 21:23:49 +0000 (14:23 -0700)
committerJoseph Coffland <joseph@cauldrondevelopment.com>
Wed, 23 Mar 2016 21:23:49 +0000 (14:23 -0700)
src/canonical_machine.c
src/plan/command.c
src/plan/command.h [new file with mode: 0644]
src/plan/planner.h
src/spindle.c
src/stepper.c
src/stepper.h

index df55fd026332c42c14c62737e72b798b137aa0fe..55f40586af60f95b0abd06e43493630c0af524be 100644 (file)
 #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 <stdbool.h>
 #include <string.h>
index 3cee84690095a5d277f5d52b8b203cac5fa7bc45..6de36dfbf98f5abb4e432a6f25dabea3ba668476 100644 (file)
 \******************************************************************************/
 
 /* How this works:
- *   - The command is called by the Gcode interpreter (cm_<command>, e.g. an M
- *     code)
+ *   - A command is called by the Gcode interpreter (cm_<command>, 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 (file)
index 0000000..07272b1
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+
+     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
+                       <http://www.gnu.org/licenses/>.
+
+                For information regarding this software email:
+                  "Joseph Coffland" <joseph@buildbotics.com>
+
+\******************************************************************************/
+
+#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);
index a7b3de4d61613aa966660ed6a6cf77a1b2db28c2..cf9ee0b39e88505a5d3c836e6bb37442d97beace 100644 (file)
@@ -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);
index d316c947e7bf750d89ffbc68fbc8e8dbd23fc51b..7cb462637df0fd1bc264c437b1dab50faf2068c5 100644 (file)
@@ -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);
index f4f731ff4d7fae20c28018d43ac5747bc1ac8c53..450374bf641bae9ea4c5e4d68fb4282ebbdbfd7e 100644 (file)
 
 #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"
index cca16f7380e537478b76b096b4d8809a435ea0c4..eafe195ba7c5941e0d4ca5a4cca925527c2cce41 100644 (file)
 
 #pragma once
 
-
-#include "config.h"
 #include "status.h"
-#include "canonical_machine.h"
-#include "plan/planner.h"
 
 #include <stdbool.h>