-
Notifications
You must be signed in to change notification settings - Fork 11
/
Tic.h
1478 lines (1357 loc) · 41.7 KB
/
Tic.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (C) Pololu Corporation. See LICENSE.txt for details.
/// \file Tic.h
///
/// This is the main header file for the Tic Stepper Motor Controller library
/// for Arduino.
///
/// For more information about the library, see the main repository at:
/// https://github.com/pololu/tic-arduino
#pragma once
#include <Arduino.h>
#include <Stream.h>
#include <Wire.h>
enum class TicProduct
{
Unknown = 0,
T825 = 1,
T834 = 2,
T500 = 3,
T249 = 4,
Tic36v4 = 5,
};
/// This constant is used by the library to convert between milliamps and the
/// Tic's native current unit, which is 32 mA. This is only valid for the Tic
/// T825 and Tic T834.
const uint8_t TicCurrentUnits = 32;
/// This constant is used by the library to convert between milliamps and the
/// Tic T249 native current unit, which is 40 mA.
const uint8_t TicT249CurrentUnits = 40;
/// This is used to represent a null or missing value for some of the Tic's
/// 16-bit input variables.
const uint16_t TicInputNull = 0xFFFF;
/// This enum defines the Tic's error bits. See the "Error handling" section of
/// the Tic user's guide for more information about what these errors mean.
///
/// See TicBase::getErrorStatus() and TicBase::getErrorsOccurred().
enum class TicError
{
IntentionallyDeenergized = 0,
MotorDriverError = 1,
LowVin = 2,
KillSwitch = 3,
RequiredInputInvalid = 4,
SerialError = 5,
CommandTimeout = 6,
SafeStartViolation = 7,
ErrLineHigh = 8,
SerialFraming = 16,
RxOverrun = 17,
Format = 18,
Crc = 19,
EncoderSkip = 20,
};
/// This enum defines the Tic command codes which are used for its serial, I2C,
/// and USB interface. These codes are used by the library and you should not
/// need to use them.
enum class TicCommand
{
SetTargetPosition = 0xE0,
SetTargetVelocity = 0xE3,
HaltAndSetPosition = 0xEC,
HaltAndHold = 0x89,
GoHome = 0x97,
ResetCommandTimeout = 0x8C,
Deenergize = 0x86,
Energize = 0x85,
ExitSafeStart = 0x83,
EnterSafeStart = 0x8F,
Reset = 0xB0,
ClearDriverError = 0x8A,
SetSpeedMax = 0xE6,
SetStartingSpeed = 0xE5,
SetAccelMax = 0xEA,
SetDecelMax = 0xE9,
SetStepMode = 0x94,
SetCurrentLimit = 0x91,
SetDecayMode = 0x92,
SetAgcOption = 0x98,
GetVariable = 0xA1,
GetVariableAndClearErrorsOccurred = 0xA2,
GetSetting = 0xA8,
};
/// This enum defines the possible operation states for the Tic.
///
/// See TicBase::getOperationState().
enum class TicOperationState
{
Reset = 0,
Deenergized = 2,
SoftError = 4,
WaitingForErrLine = 6,
StartingUp = 8,
Normal = 10,
};
/// This enum defines the possible planning modes for the Tic's step generation
/// code.
///
/// See TicBase::getPlanningMode().
enum class TicPlanningMode
{
Off = 0,
TargetPosition = 1,
TargetVelocity = 2,
};
/// This enum defines the possible causes of a full microcontroller reset for
/// the Tic.
///
/// See TicBase::getDeviceReset().
enum class TicReset
{
PowerUp = 0,
Brownout = 1,
ResetLine = 2,
Watchdog = 4,
Software = 8,
StackOverflow = 16,
StackUnderflow = 32,
};
/// This enum defines the possible decay modes.
///
/// See TicBase::getDecayMode() and TicBase::setDecayMode().
enum class TicDecayMode
{
/// This specifies "Mixed" decay mode on the Tic T825
/// and "Mixed 50%" on the Tic T824.
Mixed = 0,
/// This specifies "Slow" decay mode.
Slow = 1,
/// This specifies "Fast" decay mode.
Fast = 2,
/// This is the same as TicDecayMode::Mixed, but better expresses your
/// intent if you want to use "Mixed 50%' mode on a Tic T834.
Mixed50 = 0,
/// This specifies "Mixed 25%" decay mode on the Tic T824
/// and is the same as TicDecayMode::Mixed on the Tic T825.
Mixed25 = 3,
/// This specifies "Mixed 75%" decay mode on the Tic T824
/// and is the same as TicDecayMode::Mixed on the Tic T825.
Mixed75 = 4,
};
/// This enum defines the possible step modes.
///
/// See TicBase::getStepMode() and TicBase::setStepMode().
enum class TicStepMode
{
Full = 0,
Half = 1,
Microstep1 = 0,
Microstep2 = 1,
Microstep4 = 2,
Microstep8 = 3,
Microstep16 = 4,
Microstep32 = 5,
Microstep2_100p = 6,
Microstep64 = 7,
Microstep128 = 8,
Microstep256 = 9,
};
/// This enum defines possible AGC modes.
///
/// See TicBase::setAgcMode() and TicBase::getAgcMode().
enum class TicAgcMode
{
Off = 0,
On = 1,
ActiveOff = 2,
};
/// This enum defines possible AGC buttom current limit percentages.
///
/// See TicBase::setAgcBottomCurrentLimit() and
/// TicBase:getAgcBottomCurrentLimit().
enum class TicAgcBottomCurrentLimit
{
P45 = 0,
P50 = 1,
P55 = 2,
P60 = 3,
P65 = 4,
P70 = 5,
P75 = 6,
P80 = 7,
};
/// This enum defines possible AGC current boost steps values.
///
/// See TicBase::setAgcCurrentBoostSteps() and
/// TicBase::getAgcCurrentBoostSteps().
enum class TicAgcCurrentBoostSteps
{
S5 = 0,
S7 = 1,
S9 = 2,
S11 = 3,
};
/// This enuam defines possible AGC frequency limit values.
///
/// See TicBase::setAgcFrequencyLimit() and TicBase::getAgcFrequencyLimit().
enum class TicAgcFrequencyLimit
{
Off = 0,
F225Hz = 1,
F450Hz = 2,
F675Hz = 3,
};
/// This enum defines the Tic's control pins.
enum class TicPin
{
SCL = 0,
SDA = 1,
TX = 2,
RX = 3,
RC = 4,
};
/// This enum defines the Tic's pin states.
///
/// See TicBase::getPinState().
enum class TicPinState
{
HighImpedance = 0,
InputPullUp = 1,
OutputLow = 2,
OutputHigh = 3,
};
/// This enum defines the possible states of the Tic's main input.
enum class TicInputState
{
/// The input is not ready yet. More samples are needed, or a command has not
/// been received yet.
NotReady = 0,
/// The input is invalid.
Invalid = 1,
/// The input is valid and is telling the Tic to halt the motor.
Halt = 2,
/// The input is valid and is telling the Tic to go to a target position,
/// which you can get with TicBase::getInputAfterScaling().
Position = 3,
/// The input is valid and is telling the Tic to go to a target velocity,
/// which you can get with TicBase::getInputAfterScaling().
Velocity = 4,
};
/// This enum defines the bits in the Tic's Misc Flags 1 register. You should
/// not need to use this directly. See TicBase::getEnergized() and
/// TicBase::getPositionUncertain().
enum class TicMiscFlags1
{
Energized = 0,
PositionUncertain = 1,
ForwardLimitActive = 2,
ReverseLimitActive = 3,
HomingActive = 4,
};
/// This enum defines possible motor driver errors for the Tic T249.
///
/// See TicBase::getLastMotorDriverError().
enum class TicMotorDriverError
{
None = 0,
OverCurrent = 1,
OverTemperature = 2,
};
/// This enum defines the bits in the "Last HP driver errors" variable.
///
/// See TicBase::getLastHpDriverErrors().
enum class TicHpDriverError
{
OverTemperature = 0,
OverCurrentA = 1,
OverCurrentB = 2,
PreDriverFaultA = 3,
PreDriverFaultB = 4,
UnderVoltage = 5,
Verify = 7,
};
/// This is a base class used to represent a connection to a Tic. This class
/// provides high-level functions for sending commands to the Tic and reading
/// data from it.
///
/// See the subclasses of this class, TicSerial and TicI2C.
class TicBase
{
public:
/// You can use this function to specify what type of Tic you are using.
///
/// Example usage (pick one of the following):
/// ```
/// tic.setProduct(TicProduct::T500);
/// tic.setProduct(TicProduct::T834);
/// tic.setProduct(TicProduct::T825);
/// tic.setProduct(TicProduct::T249);
/// tic.setProduct(TicProduct::Tic36v4);
/// ```
///
/// This changes the behavior of the setCurrentLimit() function.
void setProduct(TicProduct product)
{
this->product = product;
}
/// Sets the target position of the Tic, in microsteps.
///
/// Example usage:
/// ```
/// tic.setTargetPosition(100);
/// ```
///
/// This function sends a "Set target position" to the Tic. If the Control
/// mode is set to Serial/I2C/USB, the Tic will start moving the motor to
/// reach the target position. If the control mode is something other than
/// Serial, this command will be silently ignored.
///
/// See also getTargetPosition().
void setTargetPosition(int32_t position)
{
commandW32(TicCommand::SetTargetPosition, position);
}
/// Sets the target velocity of the Tic, in microsteps per 10000 seconds.
///
/// Example usage:
/// ```
/// tic.setTargetVelocity(-1800000); // -180 steps per second
/// ```
///
/// This function sends a "Set target velocity" command to the Tic. If the
/// Control mode is set to Serial/I2C/USB, the Tic will start accelerating or
/// decelerating to reach the target velocity.
///
/// If the control mode is something other than Serial, this command will be
/// silently ignored.
///
/// See also getTargetVelocity().
void setTargetVelocity(int32_t velocity)
{
commandW32(TicCommand::SetTargetVelocity, velocity);
}
/// Stops the motor abruptly without respecting the deceleration limit and
/// sets the "Current position" variable, which represents where the Tic
/// currently thinks the motor's output is.
///
/// Example usage:
/// ```
/// tic.haltAndSetPosition(0);
/// ```
///
/// This function sends a "Halt and set position" command to the Tic. Besides
/// stopping the motor and setting the current position, this command also
/// clears the "Postion uncertain" flag, sets the "Input state" to "halt", and
/// clears the "Input after scaling" variable.
///
/// If the control mode is something other than Serial, this command will
/// be silently ignored.
void haltAndSetPosition(int32_t position)
{
commandW32(TicCommand::HaltAndSetPosition, position);
}
/// Stops the motor abruptly without respecting the deceleration limit.
///
/// Example usage:
/// ```
/// tic.haltAndHold();
/// ```
///
/// This function sends a "Halt and hold" command to the Tic. Besides stopping
/// the motor, this command also sets the "Position uncertain" flag (because
/// the abrupt stop might cause steps to be missed), sets the "Input state" to
/// "halt", and clears the "Input after scaling" variable.
///
/// If the control mode is something other than Serial/I2C/USB, ths
/// command will be silently ignored.
///
/// See also deenergize().
void haltAndHold()
{
commandQuick(TicCommand::HaltAndHold);
}
/// Tells the Tic to start its homing procedure in the reverse direction.
///
/// See the "Homing" section of the Tic user's guide for details.
///
/// See also goHomeForward().
void goHomeReverse()
{
commandW7(TicCommand::GoHome, 0);
}
/// Tells the Tic to start its homing procedure in the forward direction.
///
/// See the "Homing" section of the Tic user's guide for details.
///
/// See also goHomeReverse().
void goHomeForward()
{
commandW7(TicCommand::GoHome, 1);
}
/// Prevents the "Command timeout" error from happening for some time.
///
/// Example usage:
/// ```
/// tic.resetCommandTimeout();
/// ```
///
/// This function sends a "Reset command timeout" command to the Tic.
void resetCommandTimeout()
{
commandQuick(TicCommand::ResetCommandTimeout);
}
/// De-energizes the stepper motor coils.
///
/// Example usage:
/// ```
/// tic.deenergize();
/// ```
///
/// This function sends a De-energize command to the Tic, causing it to disable
/// its stepper motor driver. The motor will stop moving and consuming power.
/// The Tic will set the "Intentionally de-energized" error bit, turn on its
/// red LED, and drive its ERR line high. This command also sets the
/// "Position uncertain" flag (because the Tic is no longer in control of the
/// motor's position).
///
/// Note that the Energize command, which can be sent with energize(), will
/// undo the effect of this command (except it will leave the "Position
/// uncertain" flag set) and could make the system start up again.
///
/// See also haltAndHold().
void deenergize()
{
commandQuick(TicCommand::Deenergize);
}
/// Sends the Energize command.
///
/// Example usage:
/// ```
/// tic.energize();
/// ```
///
/// This function sends an Energize command to the Tic, clearing the
/// "Intentionally de-energized" error bit. If there are no other errors,
/// this allows the system to start up.
void energize()
{
commandQuick(TicCommand::Energize);
}
/// Sends the "Exit safe start" command.
///
/// Example usage:
/// ```
/// tic.exitSafeStart();
/// ```
///
/// In Serial/I2C/USB control mode, this command causes the safe start
/// violation error to be cleared for 200 ms. If there are no other errors,
/// this allows the system to start up.
void exitSafeStart()
{
commandQuick(TicCommand::ExitSafeStart);
}
/// Sends the "Enter safe start" command.
///
/// Example usage:
/// ```
/// tic.enterSafeStart();
/// ```
///
/// This command has no effect if safe-start is disabled in the Tic's settings.
///
/// In Serial/I2C/USB control mode, this command causes the Tic to stop the
/// motor and set its safe start violation error bit. An "Exit safe start"
/// command is required before the Tic will move the motor again.
///
/// See the Tic user's guide for information about what this command does in
/// the other control modes.
void enterSafeStart()
{
commandQuick(TicCommand::EnterSafeStart);
}
/// Sends the Reset command.
///
/// Example usage:
/// ```
/// tic.reset();
/// ```
///
/// This command makes the Tic forget most parts of its current state. For
/// more information, see the Tic user's guide.
void reset()
{
commandQuick(TicCommand::Reset);
// The Tic's serial and I2C interfaces will be unreliable for a brief period
// after the Tic receives the Reset command, so we delay 10 ms here.
delay(10);
}
/// Attempts to clear a motor driver error.
///
/// Example usage:
/// ```
/// tic.clearDriverError();
/// ```
///
/// This function sends a "Clear driver error" command to the Tic. For more
/// information, see the Tic user's guide.
void clearDriverError()
{
commandQuick(TicCommand::ClearDriverError);
}
/// Temporarily sets the maximum speed, in units of steps per 10000 seconds.
///
/// Example usage:
/// ```
/// tic.setMaxSpeed(5550000); // 555 steps per second
/// ```
///
/// This function sends a "Set max speed" command to the Tic. For more
/// information, see the Tic user's guide.
///
/// See also getMaxSpeed().
void setMaxSpeed(uint32_t speed)
{
commandW32(TicCommand::SetSpeedMax, speed);
}
/// Temporarily sets the starting speed, in units of steps per 10000 seconds.
///
/// Example usage:
/// ```
/// tic.setStartingSpeed(500000); // 50 steps per second
/// ```
///
/// This function sends a "Set starting speed" command to the Tic. For more
/// information, see the Tic user's guide.
///
/// See also getStartingSpeed().
void setStartingSpeed(uint32_t speed)
{
commandW32(TicCommand::SetStartingSpeed, speed);
}
/// Temporarily sets the maximum acceleration, in units of steps per second
/// per 100 seconds.
///
/// Example usage:
/// ```
/// tic.setMaxAccel(10000); // 100 steps per second per second
/// ```
///
/// This function sends a "Set max acceleration" command to the Tic. For more
/// information, see the Tic user's guide.
///
/// See also getMaxAccel().
void setMaxAccel(uint32_t accel)
{
commandW32(TicCommand::SetAccelMax, accel);
}
/// Temporarily sets the maximum deceleration, in units of steps per second
/// per 100 seconds.
///
/// Example usage:
/// ```
/// tic.setMaxDecel(10000); // 100 steps per second per second
/// ```
///
/// This function sends a "Set max deceleration" command to the Tic. For more
/// information, see the Tic user's guide.
///
/// See also getMaxDecel().
void setMaxDecel(uint32_t decel)
{
commandW32(TicCommand::SetDecelMax, decel);
}
/// Temporarily sets the stepper motor's step mode, which defines how many
/// microsteps correspond to one full step.
///
/// Example usage:
/// ```
/// tic.setStepMode(TicStepMode::Microstep8);
/// ```
///
/// This function sends a "Set step mode" command to the Tic. For more
/// information, see the Tic user's guide.
///
/// See also getStepMode().
void setStepMode(TicStepMode mode)
{
commandW7(TicCommand::SetStepMode, (uint8_t)mode);
}
/// Temporarily sets the stepper motor coil current limit in milliamps. If
/// the desired current limit is not available, this function uses the closest
/// current limit that is lower than the desired one.
///
/// When converting the current limit from milliamps to a code to send to the
/// Tic, this function needs to know what kind of Tic you are using. By
/// default, this function assumes you are using a Tic T825 or Tic T834. If
/// you are using a different kind of Tic, we recommend calling setProduct()
/// some time before calling setCurrentLimit().
///
/// Example usage:
/// ```
/// tic.setCurrentLimit(500); // 500 mA
/// ```
///
/// This function sends a "Set current limit" command to the Tic. For more
/// information about this command and how to choose a good current limit, see
/// the Tic user's guide.
///
/// See also getCurrentLimit().
void setCurrentLimit(uint16_t limit);
/// Temporarily sets the stepper motor driver's decay mode.
///
/// Example usage:
/// ```
/// tic.setDecayMode(TicDecayMode::Slow);
/// ```
///
/// The decay modes are documented in the Tic user's guide.
///
/// See also getDecayMode().
void setDecayMode(TicDecayMode mode)
{
commandW7(TicCommand::SetDecayMode, (uint8_t)mode);
}
/// Temporarily sets the AGC mode.
///
/// This is only valid for the Tic T249.
///
/// See also getAgcMode().
void setAgcMode(TicAgcMode mode)
{
commandW7(TicCommand::SetAgcOption, (uint8_t)mode & 0xF);
}
/// Temporarily sets the AGC bottom current limit.
///
/// This is only valid for the Tic T249.
///
/// See also getAgcBottomCurrentLimit().
void setAgcBottomCurrentLimit(TicAgcBottomCurrentLimit limit)
{
commandW7(TicCommand::SetAgcOption, 0x10 | ((uint8_t)limit & 0xF));
}
/// Temporarily sets the AGC current boost steps.
///
/// This is only valid for the Tic T249.
///
/// See also getAgcCurrentBoostSteps().
void setAgcCurrentBoostSteps(TicAgcCurrentBoostSteps steps)
{
commandW7(TicCommand::SetAgcOption, 0x20 | ((uint8_t)steps & 0xF));
}
/// Temporarily sets the AGC frequency limit.
///
/// This is only valid for the Tic T249.
///
/// See also getAgcFrequencyLimit().
void setAgcFrequencyLimit(TicAgcFrequencyLimit limit)
{
commandW7(TicCommand::SetAgcOption, 0x30 | ((uint8_t)limit & 0xF));
}
/// Gets the Tic's current operation state, which indicates whether it is
/// operating normally or in an error state.
///
/// Example usage:
/// ```
/// if (tic.getOperationState() != TicOperationState::Normal)
/// {
/// // There is an error, or the Tic is starting up.
/// }
/// ```
///
/// For more information, see the "Error handling" section of the Tic user's
/// guide.
TicOperationState getOperationState()
{
return (TicOperationState)getVar8(VarOffset::OperationState);
}
/// Returns true if the motor driver is energized (trying to send current to
/// its outputs).
bool getEnergized()
{
return getVar8(VarOffset::MiscFlags1) >>
(uint8_t)TicMiscFlags1::Energized & 1;
}
/// Gets a flag that indicates whether there has been external confirmation that
/// the value of the Tic's "Current position" variable is correct.
///
/// For more information, see the "Error handling" section of the Tic user's
/// guide.
bool getPositionUncertain()
{
return getVar8(VarOffset::MiscFlags1) >>
(uint8_t)TicMiscFlags1::PositionUncertain & 1;
}
/// Returns true if one of the forward limit switches is active.
bool getForwardLimitActive()
{
return getVar8(VarOffset::MiscFlags1) >>
(uint8_t)TicMiscFlags1::ForwardLimitActive & 1;
}
/// Returns true if one of the reverse limit switches is active.
bool getReverseLimitActive()
{
return getVar8(VarOffset::MiscFlags1) >>
(uint8_t)TicMiscFlags1::ReverseLimitActive & 1;
}
/// Returns true if the Tic's homing procedure is running.
bool getHomingActive()
{
return getVar8(VarOffset::MiscFlags1) >>
(uint8_t)TicMiscFlags1::HomingActive & 1;
}
/// Gets the errors that are currently stopping the motor.
///
/// Each bit in the returned register represents a different error. The bits
/// are defined in ::TicError enum.
///
/// Example usage:
/// ```
/// uint16_t errors = tic.getErrorStatus();
/// if (errors & (1 << (uint8_t)TicError::LowVin))
/// {
/// // handle loss of power
/// }
/// ```
uint16_t getErrorStatus()
{
return getVar16(VarOffset::ErrorStatus);
}
/// Gets the errors that have occurred since the last time this function was called.
///
/// Note that the Tic Control Center constantly clears the bits in this
/// register, so if you are running the Tic Control Center then you will not
/// be able to reliably detect errors with this function.
///
/// Each bit in the returned register represents a different error. The bits
/// are defined in ::TicError enum.
///
/// Example usage:
/// ```
/// uint32_t errors = tic.getErrorsOccurred();
/// if (errors & (1 << (uint8_t)TicError::MotorDriverError))
/// {
/// // handle a motor driver error
/// }
/// ```
uint32_t getErrorsOccurred()
{
uint32_t result;
getSegment(TicCommand::GetVariableAndClearErrorsOccurred,
VarOffset::ErrorsOccurred, 4, &result);
return result;
}
/// Returns the current planning mode for the Tic's step generation code.
///
/// This tells us whether the Tic is sending steps, and if it is sending
/// steps, tells us whether it is in Target Position or Target Velocity mode.
///
/// Example usage:
/// ```
/// if (tic.getPlanningMode() == TicPlanningMode::TargetPosition)
/// {
/// // The Tic is moving the stepper motor to a target position, or has
/// // already reached it and is at rest.
/// }
/// ```
TicPlanningMode getPlanningMode()
{
return (TicPlanningMode)getVar8(VarOffset::PlanningMode);
}
/// Gets the target position, in microsteps.
///
/// This is only relevant if the planning mode from getPlanningMode() is
/// TicPlanningMode::Position.
///
/// See also setTargetPosition().
int32_t getTargetPosition()
{
return getVar32(VarOffset::TargetPosition);
}
/// Gets the target velocity, in microsteps per 10000 seconds.
///
/// This is only relevant if the planning mode from getPlanningMode() is
/// TicPlanningMode::Velocity.
///
/// See also setTargetVelocity().
int32_t getTargetVelocity()
{
return getVar32(VarOffset::TargetVelocity);
}
/// Gets the current maximum speed, in microsteps per 10000 seconds.
///
/// This is the current value, which could differ from the value in the Tic's
/// settings.
///
/// See also setMaxSpeed().
uint32_t getMaxSpeed()
{
return getVar32(VarOffset::SpeedMax);
}
/// Gets the starting speed in microsteps per 10000 seconds.
///
/// This is the current value, which could differ from the value in the
/// Tic's settings.
///
/// Example usage:
/// ```
/// uint32_t startingSpeed = tic.getStartingSpeed();
/// ```
///
/// See also setStartingSpeed().
uint32_t getStartingSpeed()
{
return getVar32(VarOffset::StartingSpeed);
}
/// Gets the maximum acceleration, in microsteps per second per 100 seconds.
///
/// This is the current value, which could differ from the value in the Tic's
/// settings.
///
/// Example usage:
/// ```
/// uint32_t accelMax = tic.getMaxAccel();
/// ```
///
/// See also setMaxAccel().
uint32_t getMaxAccel()
{
return getVar32(VarOffset::AccelMax);
}
/// Gets the maximum deceleration, in microsteps per second per 100 seconds.
///
/// This is the current value, which could differ from the value in the Tic's
/// settings.
///
/// Example usage:
/// ```
/// uint32_t decelMax = tic.getMaxDecel();
/// ```
///
/// See also setMaxDecel().
uint32_t getMaxDecel()
{
return getVar32(VarOffset::DecelMax);
}
/// Gets the current position of the stepper motor, in microsteps.
///
/// Note that this just tracks steps that the Tic has commanded the stepper
/// driver to take; it could be different from the actual position of the
/// motor for various reasons.
///
/// For an example of how to use this this, see the SerialPositionControl
/// example or the I2CPositionControl exmaple.
///
/// See also haltAndSetPosition().
int32_t getCurrentPosition()
{
return getVar32(VarOffset::CurrentPosition);
}
/// Gets the current velocity of the stepper motor, in microsteps per 10000
/// seconds.
///
/// Note that this is just the velocity used in the Tic's step planning
/// algorithms, and it might not correspond to the actual velocity of the
/// motor for various reasons.
///
/// Example usage:
/// ```
/// int32_t velocity = tic.getCurrentVelocity();
/// ```
int32_t getCurrentVelocity()
{
return getVar32(VarOffset::CurrentVelocity);
}
/// Gets the acting target position, in microsteps.
///
/// This is a variable used in the Tic's target position step planning
/// algorithm, and it could be invalid while the motor is stopped.
///
/// This is mainly intended for getting insight into how the Tic's algorithms
/// work or troubleshooting issues, and most people should not use this.
uint32_t getActingTargetPosition()
{
return getVar32(VarOffset::ActingTargetPosition);
}
/// Gets the time since the last step, in timer ticks.
///
/// Each timer tick represents one third of a microsecond. The Tic only
/// updates this variable every 5 milliseconds or so, and it could be invalid
/// while the motor is stopped.
///
/// This is mainly intended for getting insight into how the Tic's algorithms
/// work or troubleshooting issues, and most people should not use this.
uint32_t getTimeSinceLastStep()
{
return getVar32(VarOffset::TimeSinceLastStep);
}
/// Gets the cause of the controller's last full microcontroller reset.
///
/// Example usage:
/// ```
/// if (tic.getDeviceReset() == TicReset::Brownout)
/// {
/// // There was a brownout reset; the power supply could not keep up.
/// }
/// ```
///
/// The Reset command (reset()) does not affect this variable.
TicReset getDeviceReset()
{
return (TicReset)getVar8(VarOffset::DeviceReset);
}
/// Gets the current measurement of the VIN voltage, in millivolts.
///
/// Example usage:
/// ```
/// uint16_t power = tic.getVinVoltage();
/// ```
uint16_t getVinVoltage()
{
return getVar16(VarOffset::VinVoltage);
}
/// Gets the time since the last full reset of the Tic's microcontroller, in
/// milliseconds.
///
/// Example usage:
/// ```
/// uint32_t upTime = tic.getUpTime();
/// ```