ClearCore Library
Loading...
Searching...
No Matches
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
37namespace ClearCore
38{
39
44#define FRACT_BITS 15
45
58 {
59 friend class MotorManager;
60 friend class TestIO;
61
62 public:
63#ifndef HIDE_FROM_DOXYGEN
65#endif
66
67 typedef enum
68 {
69 MOVE_TARGET_ABSOLUTE,
70 MOVE_TARGET_REL_END_POSN,
71 } MoveTarget;
72
98 virtual bool Move(int32_t dist,
99 MoveTarget moveTarget = MOVE_TARGET_REL_END_POSN);
100
116 virtual bool MoveVelocity(int32_t velocity);
117
129
150 void MoveStopDecel(uint32_t decelMax = 0);
151
164 void PositionRefSet(int32_t posn)
165 {
166 m_posnAbsolute = posn;
167 }
168
182 volatile const int32_t &PositionRefCommanded()
183 {
184 return m_posnAbsolute;
185 }
186
201
215 void VelMax(uint32_t velMax);
216
231 void AccelMax(uint32_t accelMax);
232
248 void EStopDecelMax(uint32_t decelMax);
249
266 {
267 return MoveStateGet() == MS_IDLE;
268 }
269
286 {
287 return MoveStateGet() == MS_CRUISE;
288 }
289
290 protected:
291 struct LimitStatus
292 {
293 uint32_t LimitRampPos : 1; // True if we are ramping into the positive limit
294 uint32_t LimitRampNeg : 1; // True if we are ramping into the negative limit
295 uint32_t EnterHWLimit : 1; // True when entering HW limits
296 uint32_t InPosHWLimit : 1; // True if we are in the positive HW limit
297 uint32_t InNegHWLimit : 1; // True if we are in the negative HW limit
298 uint32_t InPosHWLimitLast : 1;
299 uint32_t InNegHWLimitLast : 1;
300
301 public:
302 LimitStatus()
303 : LimitRampPos(0),
304 LimitRampNeg(0),
305 EnterHWLimit(0),
306 InPosHWLimit(0),
307 InNegHWLimit(0),
308 InPosHWLimitLast(0),
309 InNegHWLimitLast(0) {}
310 };
311
312 typedef enum
313 {
314 MS_IDLE,
315 MS_START,
316 MS_ACCEL,
317 MS_CRUISE,
318 MS_DECEL,
319 MS_DECEL_VEL,
320 MS_END,
321 MS_CHANGE_DIR,
322 } MoveStates;
323
324 uint32_t m_stepsPrevious;
325 uint32_t m_stepsPerSampleMax;
326 MoveStates m_moveState;
327 bool m_direction;
328 // True if the last move commanded was a positional move (latched)
329 bool m_lastMoveWasPositional;
330
331 LimitStatus m_limitInfo;
332
333 int32_t m_posnAbsolute;
334
335 volatile const bool &Direction()
336 {
337 return m_direction;
338 }
339
340 volatile const MoveStates &MoveStateGet()
341 {
342 return m_moveState;
343 }
344
345 void StepsCalculated();
346
347 uint32_t StepsPrevious()
348 {
349 return m_stepsPrevious;
350 }
351
352 bool CheckTravelLimits();
353
354 void PosLimitActive(bool isActive)
355 {
356 m_limitInfo.InPosHWLimit = isActive;
357 }
358
359 void NegLimitActive(bool isActive)
360 {
361 m_limitInfo.InNegHWLimit = isActive;
362 }
363
364 private:
365 int32_t m_stepsCommanded;
366 int32_t m_stepsSent; // Accumulated integer position
367
368 bool m_velocityMove; // A Velocity move is active
369 bool m_moveDirChange; // The move is changing direction
370 bool m_dirCommanded; // The direction of the commanded move
371
372 // All of the position, velocity and acceleration parameters are signed and
373 // in Q format, with all arithmetic performed in fixed point.
374 // FRACT_BITS defines the Q value - the number of bits that are treated as
375 // fractional values.
376
377 int32_t m_velLimitQx; // Velocity limit
378 int32_t m_altVelLimitQx; // Velocity move Velocity limit
379 int32_t m_accelLimitQx; // Acceleration limit
380 int32_t m_altDecelLimitQx; // E-Stop Deceleration limit
381 int64_t m_posnCurrentQx; // Current position
382 int32_t m_velCurrentQx; // Current velocity
383 int32_t m_accelCurrentQx; // Current acceleration
384 int64_t m_posnTargetQx; // Move length
385 int32_t m_velTargetQx; // Adjusted velocity limit
386 int64_t m_posnDecelQx; // Position to start decelerating
387
388 // Pending velocity and acceleration parameters that shouldn't be applied
389 // until a Move function is called again
390 int32_t m_velLimitPendingQx; // Velocity limit
391 int32_t m_altVelLimitPendingQx; // Velocity move Velocity limit
392 int32_t m_accelLimitPendingQx; // Acceleration limit
393 int32_t m_altDecelLimitPendingQx; // E-Stop Deceleration limit
394
395 virtual void OutputDirection() = 0;
396 void StepsPerSampleMaxSet(uint32_t maxSteps);
397
398 void AltVelMax(int32_t velMax);
399
407 void UpdatePendingMoveLimits()
408 {
409 m_velLimitQx = m_velLimitPendingQx;
410 m_altVelLimitQx = m_altVelLimitPendingQx;
411 m_accelLimitQx = m_accelLimitPendingQx;
412 m_altDecelLimitQx = m_altDecelLimitPendingQx;
413 }
414 };
415
416} // ClearCore namespace
417
418#endif // __STEPGENERATOR_H__
ClearCore motor-connector manager.
Definition MotorManager.h:46
ClearCore Step and Direction generator class.
Definition StepGenerator.h:58
void PositionRefSet(int32_t posn)
Sets the absolute commanded position to the given value.
Definition StepGenerator.h:164
void MoveStopDecel(uint32_t decelMax=0)
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:285
void AccelMax(uint32_t accelMax)
Sets the maximum acceleration in step pulses per second^2.
void EStopDecelMax(uint32_t decelMax)
Sets the maximum deceleration for E-stop Deceleration 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.
volatile const int32_t & PositionRefCommanded()
Accessor for the StepGenerator's position reference.
Definition StepGenerator.h:182
void VelMax(uint32_t velMax)
Sets the maximum velocity for position moves, in step pulses per second.
int32_t VelocityRefCommanded()
Accessor for the StepGenerator's momentary velocity.
bool StepsComplete()
Function to check if no steps are currently being commanded to the motor.
Definition StepGenerator.h:265
Namespace to encompass the ClearCore board API.
Definition AdcManager.h:36