-
Notifications
You must be signed in to change notification settings - Fork 177
/
planner.cpp
207 lines (155 loc) · 4.92 KB
/
planner.cpp
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
/**********************************************************************
* Author: tstern
*
Copyright (C) 2018 MisfitTech, All rights reserved.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
Written by Trampas Stern for MisfitTech.
Misfit Tech invests time and resources providing this open source code,
please support MisfitTech and open-source hardware by purchasing
products from MisfitTech, www.misifittech.net!
*********************************************************************/
#include "planner.h"
#include "board.h"
#include "wiring_private.h"
#include "syslog.h"
#include "angle.h"
#include "Arduino.h"
#define WAIT_TC16_REGS_SYNC(x) while(x->COUNT16.STATUS.bit.SYNCBUSY);
//define the planner class as being global
Planner SmartPlanner;
static bool enterTC3CriticalSection()
{
bool state=NVIC_IS_IRQ_ENABLED(TC3_IRQn);
NVIC_DisableIRQ(TC3_IRQn);
return state;
}
static void exitTC3CriticalSection(bool prevState)
{
if (prevState)
{
NVIC_EnableIRQ(TC3_IRQn);
} //else do nothing
}
void TC3_Init(void)
{
// Enable GCLK for TC3
GCLK->CLKCTRL.reg = (uint16_t) (GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK0 | GCLK_CLKCTRL_ID( GCM_TCC2_TC3 )) ;
while (GCLK->STATUS.bit.SYNCBUSY);
TC3->COUNT16.CTRLA.reg &= ~TC_CTRLA_ENABLE; // Disable TCx
WAIT_TC16_REGS_SYNC(TC3) // wait for sync
TC3->COUNT16.CTRLA.reg |= TC_CTRLA_MODE_COUNT16; // Set Timer counter Mode to 16 bits
WAIT_TC16_REGS_SYNC(TC3)
TC3->COUNT16.CTRLA.reg |= TC_CTRLA_WAVEGEN_MFRQ; // Set TC as normal Normal Frq
WAIT_TC16_REGS_SYNC(TC3)
TC3->COUNT16.CTRLA.reg |= TC_CTRLA_PRESCALER_DIV2; // Set perscaler
WAIT_TC16_REGS_SYNC(TC3)
TC3->COUNT16.CC[0].reg = F_CPU/PLANNER_UPDATE_RATE_HZ/2; //divide by two because of prescaler
WAIT_TC16_REGS_SYNC(TC3)
TC3->COUNT16.INTENSET.reg = 0; // disable all interrupts
TC3->COUNT16.INTENSET.bit.OVF = 1; // enable overfollow
NVIC_SetPriority(TC3_IRQn, 3);
// Enable InterruptVector
NVIC_EnableIRQ(TC3_IRQn);
// Enable TC
TC3->COUNT16.CTRLA.reg |= TC_CTRLA_ENABLE;
WAIT_TC16_REGS_SYNC(TC3);
}
void TC3_Handler(void)
{
interrupts(); //allow other interrupts
//do the planner tick
SmartPlanner.tick();
//SerialUSB.println('x');
TC3->COUNT16.INTFLAG.bit.OVF = 1; //clear interrupt by writing 1 to flag
}
void Planner::begin(StepperCtrl *ptrStepper)
{
ptrStepperCtrl=ptrStepper;
currentMode=PLANNER_NONE;
//setup the timer and interrupt as the last thing
TC3_Init();
}
void Planner::tick(void)
{
if (currentMode == PLANNER_NONE)
{
return; //do nothing
}
if (PLANNER_CV == currentMode)
{
// SerialUSB.println(currentSetAngle);
// SerialUSB.println(endAngle);
// SerialUSB.println(tickIncrement);
// SerialUSB.println(fabs(currentSetAngle-endAngle));
// SerialUSB.println(fabs(tickIncrement*2));
// SerialUSB.println();
int32_t x;
if (fabs(currentSetAngle-endAngle) >= fabs(tickIncrement))
{
currentSetAngle+=tickIncrement;
x=ANGLE_FROM_DEGREES(currentSetAngle);
ptrStepperCtrl->moveToAbsAngle(x);
}else
{
//we are done, make sure we end at the right point
//SerialUSB.println("done");
x=ANGLE_FROM_DEGREES(endAngle);
ptrStepperCtrl->moveToAbsAngle(x);
currentMode=PLANNER_NONE;
}
}
}
void Planner::stop(void)
{
bool state;
state = enterTC3CriticalSection();
currentMode=PLANNER_NONE;
exitTC3CriticalSection(state);
}
bool Planner::moveConstantVelocity(float finalAngle, float rpm)
{
bool state;
state = enterTC3CriticalSection();
//first determine if operation is in progress
if (PLANNER_NONE != currentMode)
{
#ifndef MECHADUINO_HARDWARE
//we are in operation return false
SerialUSB.println("planner operational");
#else
Serial5.println("planner operational");
#endif
exitTC3CriticalSection(state);
return false;
}
//get current posistion
startAngle = ANGLE_T0_DEGREES(ptrStepperCtrl->getCurrentAngle());
//deterime the tick increment
tickIncrement=360.0*fabs(rpm)/60/PLANNER_UPDATE_RATE_HZ;
//set the desired end angle
endAngle=finalAngle;
//set the current angle
currentSetAngle=startAngle;
if (startAngle>endAngle)
{
#ifndef MECHADUINO_HARDWARE
SerialUSB.println("reverse");
#endif
tickIncrement=-tickIncrement;
}
// SerialUSB.println(currentSetAngle);
// SerialUSB.println(endAngle);
// SerialUSB.println(tickIncrement);
// SerialUSB.println();
currentMode=PLANNER_CV;
exitTC3CriticalSection(state);
return true;
}