forked from ee96242/motorrrrcontrol
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicI2C tic;.cpp
More file actions
97 lines (81 loc) · 2.84 KB
/
TicI2C tic;.cpp
File metadata and controls
97 lines (81 loc) · 2.84 KB
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
TicI2C tic;
// Define the current limit, in milliamps, to use while moving
// the motor.
const uint16_t currentLimitWhileMoving = 500;
// Define the current limit, in milliamps, to use while stopped.
const uint16_t currentLimitWhileStopped = 0;
void setup()
{
// Specify what type of Tic we are talking to. This affects
// how the setCurrentLimit command works.
tic.setProduct(TicProduct::T825);
// tic.setProduct(TicProduct::T834);
// tic.setProduct(TicProduct::T500);
// tic.setProduct(TicProduct::T249);
// tic.setProduct(TicProduct::Tic36v4);
// Set up I2C.
Wire.begin();
// Give the Tic some time to start up.
delay(20);
// Set the Tic's current position to 0, so that when we command
// it to move later, it will move a predictable amount.
tic.haltAndSetPosition(0);
// Tells the Tic that it is OK to start driving the motor. The
// Tic's safe-start feature helps avoid unexpected, accidental
// movement of the motor: if an error happens, the Tic will not
// drive the motor again until it receives the Exit Safe Start
// command. The safe-start feature can be disbled in the Tic
// Control Center.
tic.exitSafeStart();
}
// Sends a "Reset command timeout" command to the Tic. We must
// call this at least once per second, or else a command timeout
// error will happen. The Tic's default command timeout period
// is 1000 ms, but it can be changed or disabled in the Tic
// Control Center.
void resetCommandTimeout()
{
tic.resetCommandTimeout();
}
// Delays for the specified number of milliseconds while
// resetting the Tic's command timeout so that its movement does
// not get interrupted by errors.
void delayWhileResettingCommandTimeout(uint32_t ms)
{
uint32_t start = millis();
do
{
resetCommandTimeout();
} while ((uint32_t)(millis() - start) <= ms);
}
// Polls the Tic, waiting for it to reach the specified target
// position. Note that if the Tic detects an error, the Tic will
// probably go into safe-start mode and never reach its target
// position, so this function will loop infinitely. If that
// happens, you will need to reset your Arduino.
void waitForPosition(int32_t targetPosition)
{
do
{
resetCommandTimeout();
} while (tic.getCurrentPosition() != targetPosition);
}
void loop()
{
// Set the current limit and wait a little bit for it to take effect.
tic.setCurrentLimit(currentLimitWhileMoving);
delay(10);
// Tell the Tic to move to position 400, and wait until it gets
// there.
tic.setTargetPosition(400);
waitForPosition(400);
tic.setCurrentLimit(currentLimitWhileStopped);
delayWhileResettingCommandTimeout(3000);
// Now move back to position 0 the same way.
tic.setCurrentLimit(currentLimitWhileMoving);
delay(10);
tic.setTargetPosition(0);
waitForPosition(0);
tic.setCurrentLimit(currentLimitWhileStopped);
delayWhileResettingCommandTimeout(3000);
}