ClearCore Library
StepGenerator.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Teknic, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22 
32 #ifndef __STEPGENERATOR_H__
33 #define __STEPGENERATOR_H__
34 
35 #include <stdint.h>
36 
37 namespace ClearCore {
38 
43 #define FRACT_BITS 15
44 
57  friend class MotorManager;
58  friend class TestIO;
59 
60 public:
61 #ifndef HIDE_FROM_DOXYGEN
62  StepGenerator();
63 #endif
64 
65  typedef enum {
66  MOVE_TARGET_ABSOLUTE,
67  MOVE_TARGET_REL_END_POSN,
68  } MoveTarget;
69 
95  virtual bool Move(int32_t dist,
96  MoveTarget moveTarget = MOVE_TARGET_REL_END_POSN);
97 
113  virtual bool MoveVelocity(int32_t velocity);
114 
125  void MoveStopAbrupt();
126 
147  void MoveStopDecel(uint32_t decelMax = 0);
148 
161  void PositionRefSet(int32_t posn) {
162  m_posnAbsolute = posn;
163  }
164 
178  volatile const int32_t &PositionRefCommanded() {
179  return m_posnAbsolute;
180  }
181 
195  int32_t VelocityRefCommanded();
196 
210  void VelMax(uint32_t velMax);
211 
226  void AccelMax(uint32_t accelMax);
227 
228 
244  void EStopDecelMax(uint32_t decelMax);
245 
261  bool StepsComplete() {
262  return MoveStateGet() == MS_IDLE;
263  }
264 
281  return MoveStateGet() == MS_CRUISE;
282  }
283 
284 protected:
285  struct LimitStatus {
286  uint32_t LimitRampPos : 1; // True if we are ramping into the positive limit
287  uint32_t LimitRampNeg : 1; // True if we are ramping into the negative limit
288  uint32_t EnterHWLimit : 1; // True when entering HW limits
289  uint32_t InPosHWLimit : 1; // True if we are in the positive HW limit
290  uint32_t InNegHWLimit : 1; // True if we are in the negative HW limit
291  uint32_t InPosHWLimitLast : 1;
292  uint32_t InNegHWLimitLast : 1;
293 
294  public:
295  LimitStatus()
296  : LimitRampPos(0),
297  LimitRampNeg(0),
298  EnterHWLimit(0),
299  InPosHWLimit(0),
300  InNegHWLimit(0),
301  InPosHWLimitLast(0),
302  InNegHWLimitLast(0) {}
303  };
304 
305  typedef enum {
306  MS_IDLE,
307  MS_START,
308  MS_ACCEL,
309  MS_CRUISE,
310  MS_DECEL,
311  MS_DECEL_VEL,
312  MS_END,
313  MS_CHANGE_DIR,
314  } MoveStates;
315 
316  uint32_t m_stepsPrevious;
317  uint32_t m_stepsPerSampleMax;
318  MoveStates m_moveState;
319  bool m_direction;
320  // True if the last move commanded was a positional move (latched)
321  bool m_lastMoveWasPositional;
322 
323  LimitStatus m_limitInfo;
324 
325  int32_t m_posnAbsolute;
326 
327  volatile const bool &Direction() {
328  return m_direction;
329  }
330 
331  volatile const MoveStates &MoveStateGet() {
332  return m_moveState;
333  }
334 
335  void StepsCalculated();
336 
337  uint32_t StepsPrevious() {
338  return m_stepsPrevious;
339  }
340 
341  bool CheckTravelLimits();
342 
343  void PosLimitActive(bool isActive) {
344  m_limitInfo.InPosHWLimit = isActive;
345  }
346 
347  void NegLimitActive(bool isActive) {
348  m_limitInfo.InNegHWLimit = isActive;
349  }
350 
351 private:
352 
353  int32_t m_stepsCommanded;
354  int32_t m_stepsSent; // Accumulated integer position
355 
356  bool m_velocityMove; // A Velocity move is active
357  bool m_moveDirChange; // The move is changing direction
358  bool m_dirCommanded; // The direction of the commanded move
359 
360 
361  // All of the position, velocity and acceleration parameters are signed and
362  // in Q format, with all arithmetic performed in fixed point.
363  // FRACT_BITS defines the Q value - the number of bits that are treated as
364  // fractional values.
365 
366  int32_t m_velLimitQx; // Velocity limit
367  int32_t m_altVelLimitQx; // Velocity move Velocity limit
368  int32_t m_accelLimitQx; // Acceleration limit
369  int32_t m_altDecelLimitQx;// E-Stop Deceleration limit
370  int64_t m_posnCurrentQx; // Current position
371  int32_t m_velCurrentQx; // Current velocity
372  int32_t m_accelCurrentQx; // Current acceleration
373  int64_t m_posnTargetQx; // Move length
374  int32_t m_velTargetQx; // Adjusted velocity limit
375  int64_t m_posnDecelQx; // Position to start decelerating
376 
377  // Pending velocity and acceleration parameters that shouldn't be applied
378  // until a Move function is called again
379  int32_t m_velLimitPendingQx; // Velocity limit
380  int32_t m_altVelLimitPendingQx; // Velocity move Velocity limit
381  int32_t m_accelLimitPendingQx; // Acceleration limit
382  int32_t m_altDecelLimitPendingQx;// E-Stop Deceleration limit
383 
384  virtual void OutputDirection() = 0;
385  void StepsPerSampleMaxSet(uint32_t maxSteps);
386 
387  void AltVelMax(int32_t velMax);
388 
396  void UpdatePendingMoveLimits() {
397  m_velLimitQx = m_velLimitPendingQx;
398  m_altVelLimitQx = m_altVelLimitPendingQx;
399  m_accelLimitQx = m_accelLimitPendingQx;
400  m_altDecelLimitQx = m_altDecelLimitPendingQx;
401  }
402 };
403 
404 } // ClearCore namespace
405 
406 #endif // __STEPGENERATOR_H__
virtual bool MoveVelocity(int32_t velocity)
Issues a velocity move at the specified velocity.
bool CruiseVelocityReached()
Function to check if the commanded move is at the cruising velocity - Acceleration portion of movemen...
Definition: StepGenerator.h:280
bool StepsComplete()
Function to check if no steps are currently being commanded to the motor.
Definition: StepGenerator.h:261
volatile const int32_t & PositionRefCommanded()
Accessor for the StepGenerator&#39;s position reference.
Definition: StepGenerator.h:178
void EStopDecelMax(uint32_t decelMax)
Sets the maximum deceleration for E-stop Deceleration in step pulses per second^2. This is only for MoveStopDecel.
void PositionRefSet(int32_t posn)
Sets the absolute commanded position to the given value.
Definition: StepGenerator.h:161
Namespace to encompass the ClearCore board API.
Definition: AdcManager.h:36
void VelMax(uint32_t velMax)
Sets the maximum velocity for position moves, in step pulses per second.
int32_t VelocityRefCommanded()
Accessor for the StepGenerator&#39;s momentary velocity.
void AccelMax(uint32_t accelMax)
Sets the maximum acceleration in step pulses per second^2.
virtual bool Move(int32_t dist, MoveTarget moveTarget=MOVE_TARGET_REL_END_POSN)
Issues a positional move for the specified distance.
ClearCore Step and Direction generator class.
Definition: StepGenerator.h:56
ClearCore motor-connector manager.
Definition: MotorManager.h:46
void MoveStopDecel(uint32_t decelMax=0)