Fraise
2.1
FRAmework for Interfacing Software and Electronics
Main Page
Modules
Classes
Files
File List
File Members
All
Classes
Files
Functions
Typedefs
Groups
dcmotor.h
1
/*********************************************************************
2
*
3
* DC motor library for Fraise pic18f device
4
*
5
*
6
*********************************************************************
7
* Author Date Comment
8
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9
* Antoine Rousseau 2014 Original.
10
********************************************************************/
11
/*
12
# This program is free software; you can redistribute it and/or
13
# modify it under the terms of the GNU General Public License
14
# as published by the Free Software Foundation; either version 2
15
# of the License, or (at your option) any later version.
16
#
17
# This program is distributed in the hope that it will be useful,
18
# but WITHOUT ANY WARRANTY; without even the implied warranty of
19
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
# GNU General Public License for more details.
21
22
# You should have received a copy of the GNU General Public License
23
# along with this program; if not, write to the Free Software
24
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
25
# MA 02110-1301, USA.
26
*/
27
#ifndef _DCMOTOR_H_
28
#define _DCMOTOR_H_
29
30
#include <
fruit.h
>
31
#include <pid.h>
32
#include <ramp.h>
33
34
typedef
union
{
35
unsigned
char
val;
36
struct
{
37
unsigned
incA:1;
38
unsigned
incB:1;
39
unsigned
lastA:1;
40
unsigned
lastB:1;
41
};
42
}
t_dcmotorIncr
;
43
44
typedef
struct
{
45
int
SpeedConsign;
46
int
PWMConsign;
47
long
int
lastIncDeltaT;
// interval between incs on last Compute() call
48
long
int
lastPosition;
// position on last Compute() call
49
unsigned
stalled:1;
// the motor doesn't run anymore
50
}
t_dcmotorVars
;
51
52
typedef
struct
{
53
long
int
lastIncTime;
// last inc event time
54
long
int
IncDeltaT;
// interval between last captured inc and the one captured just before the last reset.
55
// Must be reset in critical section, when reading Position.
56
long
int
Position;
57
union
{
58
unsigned
char
flags;
59
struct
{
60
unsigned
homed:1 ;
//homing done : end switch has been detected.
61
unsigned
direction:1;
// last measured direction ; 0=position_decrease 1=position_increase
62
unsigned
end:1;
63
};
64
};
65
}
t_dcmotorVolVars
;
66
67
typedef
struct
{
68
unsigned
char
Mode ;
//0:pwm 1:speed 2:pos 3:pos+speed_regulator
69
int
PosWindow;
// tolerated absolute position error
70
int
PwmMin;
// minimum absolute pwm value
71
unsigned
char
StallTime;
// if position has not changed during this period, the motor is considered stopped. unit = 1/8 s.
72
unsigned
char
PosErrorGain;
// position error multiplier before PosPID
73
unsigned
char
reversed;
//1 if positive speed decreases measured position
74
}
t_dcmotorSetting
;
75
76
typedef
struct
{
77
t_dcmotorSetting
Setting;
78
t_dcmotorIncr
Incr;
79
t_dcmotorVars
Vars;
80
volatile
t_dcmotorVolVars
VolVars;
81
82
t_ramp
PosRamp;
83
t_pid
PosPID;
//position pid
84
t_pid
SpeedPID;
//speed pid
85
}
t_dcmotor
;
86
87
extern
int
dcmotor_v,dcmotor_vabs;
88
extern
t_dcmotorVars
dcmotorVars;
89
extern
t_dcmotorVolVars
dcmotorVolVars;
90
91
#define SET_PWM_(pwm,val) do{ CCP##pwm##CONbits.DC##pwm##B1 = val&2; CCP##pwm##CONbits.DC##pwm##B0 = val&1; CCPR##pwm##L=val>>2; } while(0)
92
93
#define SET_PWM(pwm,val) CALL_FUN2(SET_PWM_, pwm, val)
94
95
96
#define DCMOTOR_DECLARE_(motID) t_dcmotor dcmotor##motID
97
#define DCMOTOR_DECLARE(motID) CALL_FUN(DCMOTOR_DECLARE_,motID)
98
99
#define DCMOTOR_CAPTURE_SERVICE_(motID) do{ \
100
dcmotor##motID.Incr.incA = digitalRead(MOT##motID##_A); \
101
if(!digitalRead(MOT##motID##_END)) { \
102
dcmotor##motID.VolVars.Position = 0; \
103
dcmotor##motID.VolVars.homed = 1; \
104
dcmotor##motID.VolVars.end = 1; \
105
} else dcmotor##motID.VolVars.end = 0; \
106
if(dcmotor##motID.Incr.incA != dcmotor##motID.Incr.lastA) { \
107
dcmotor##motID.Incr.lastA = dcmotor##motID.Incr.incA; \
108
dcmotor##motID.VolVars.IncDeltaT -= dcmotor##motID.VolVars.lastIncTime; \
109
dcmotor##motID.VolVars.lastIncTime = timeISR(); \
110
dcmotor##motID.VolVars.IncDeltaT += timeISR(); \
111
dcmotor##motID.Incr.incB = digitalRead(MOT##motID##_B); \
112
if(dcmotor##motID.Incr.incA ^ !dcmotor##motID.Incr.incB) { \
113
dcmotor##motID.VolVars.Position++; \
114
dcmotor##motID.VolVars.direction = 1; \
115
} \
116
else { \
117
dcmotor##motID.VolVars.Position--; \
118
dcmotor##motID.VolVars.direction = 0; \
119
} \
120
} \
121
} while(0)
122
#define DCMOTOR_CAPTURE_SERVICE(motID) CALL_FUN(DCMOTOR_CAPTURE_SERVICE_,motID)
123
124
#define DCMOTOR_CAPTURE_SERVICE_SINGLE_(motID) do{ \
125
dcmotor##motID.Incr.incA = digitalRead(MOT##motID##_A); \
126
if(!digitalRead(MOT##motID##_END)) { \
127
dcmotor##motID.VolVars.Position = 0; \
128
dcmotor##motID.VolVars.homed = 1; \
129
dcmotor##motID.VolVars.end = 1; \
130
} else dcmotor##motID.VolVars.end = 0; \
131
if(dcmotor##motID.Incr.incA != dcmotor##motID.Incr.lastA) { \
132
dcmotor##motID.Incr.lastA = dcmotor##motID.Incr.incA; \
133
dcmotor##motID.VolVars.IncDeltaT -= dcmotor##motID.VolVars.lastIncTime; \
134
dcmotor##motID.VolVars.lastIncTime = timeISR(); \
135
dcmotor##motID.VolVars.IncDeltaT += timeISR(); \
136
dcmotor##motID.Incr.incB = digitalRead(MOT##motID##_B); \
137
if(dcmotor##motID.VolVars.direction) dcmotor##motID.VolVars.Position++; \
138
else dcmotor##motID.VolVars.Position--; \
139
} \
140
} while(0)
141
#define DCMOTOR_CAPTURE_SERVICE(motID) CALL_FUN(DCMOTOR_CAPTURE_SERVICE_,motID)
142
143
144
#define DCMOTOR_INIT_(motID) do{\
145
/*SETPORT_MOT##motID;*/
\
146
pinModeDigitalOut(M##motID##1);\
147
pinModeDigitalOut(M##motID##2);\
148
pinModeDigitalOut(M##motID##EN);\
149
SET_PWM(MOT##motID##_PWM, 0);\
150
/*MOT##motID##_IN1 = 0;*/
digitalClear(M##motID##1);\
151
/*MOT##motID##_IN2 = 0;*/
digitalClear(M##motID##2);\
152
/*MOT##motID##_EN = 1;*/
digitalSet(M##motID##EN);\
153
pinModeDigitalIn(MOT##motID##_END);\
154
pinModeDigitalIn(MOT##motID##_A);\
155
pinModeDigitalIn(MOT##motID##_B);\
156
dcmotor##motID.Setting.Mode = 0;\
157
dcmotor##motID.Setting.PosWindow = 2;\
158
dcmotor##motID.Setting.PwmMin = 10;\
159
dcmotor##motID.Setting.StallTime = 16;\
160
dcmotor##motID.Setting.PosErrorGain = 7;\
161
dcmotor##motID.Setting.reversed = 0;\
162
rampInit(&dcmotor##motID.PosRamp);\
163
pidInit(&dcmotor##motID.SpeedPID);\
164
pidInit(&dcmotor##motID.PosPID);\
165
dcmotor##motID.VolVars.Position = 0;\
166
dcmotor##motID.VolVars.homed = 0;\
167
dcmotor##motID.VolVars.end = 0;\
168
dcmotor##motID.Vars.PWMConsign = dcmotor##motID.Vars.SpeedConsign = 0;\
169
dcmotor##motID.Vars.lastPosition = 0;\
170
} while(0)
171
#define DCMOTOR_INIT(motID) CALL_FUN(DCMOTOR_INIT_,motID)
172
173
#define DCMOTOR_LOAD_dcmotorVolVars_(motID) do{ \
174
__critical {\
175
dcmotorVolVars.lastIncTime = dcmotor##motID.VolVars.lastIncTime;\
176
dcmotorVolVars.IncDeltaT = dcmotor##motID.VolVars.IncDeltaT;\
177
dcmotorVolVars.Position = dcmotor##motID.VolVars.Position;\
178
dcmotorVolVars.flags = dcmotor##motID.VolVars.flags;\
179
dcmotor##motID.VolVars.IncDeltaT = 0;\
180
}\
181
} while(0)
182
183
void
dcmotorCompute(
t_dcmotor
*mot);
184
185
#define DCMOTOR_FORMATPWM(motID) do{ \
186
dcmotor_v = dcmotor##motID.Vars.PWMConsign; \
187
if(dcmotor_v > 1023) dcmotor_v = 1023; \
188
if(dcmotor_v < -1023) dcmotor_v = -1023; \
189
if((dcmotor_v > 0) && (dcmotor_v < dcmotor##motID.Setting.PwmMin)) dcmotor_v = 0; \
190
if((dcmotor_v < 0) && (dcmotor_v > -dcmotor##motID.Setting.PwmMin)) dcmotor_v = 0; \
191
if(dcmotor##motID.VolVars.end && (dcmotor_v < 0)) dcmotor_v = 0;\
192
/*dcmotor_v = (dcmotor##motID.Setting.reversed ? -dcmotor_v : dcmotor_v);*/
\
193
} while(0)
194
195
#define DCMOTOR_UPDATE_ASYM_(motID) do{ \
196
DCMOTOR_FORMATPWM(motID);\
197
dcmotor_vabs = dcmotor_v < 0 ? 1023 + dcmotor_v : dcmotor_v; \
198
SET_PWM(MOT##motID##_PWM, dcmotor_vabs); \
199
if(dcmotor_v < 0) { digitalSet(M##motID##2);}\
200
else { digitalClear(M##motID##2);}\
201
} while(0)
202
#define DCMOTOR_UPDATE_ASYM(motID) CALL_FUN(DCMOTOR_UPDATE_ASYM_,motID)
203
204
#define DCMOTOR_UPDATE_SYM_(motID) do{ \
205
DCMOTOR_FORMATPWM(motID);\
206
dcmotor_vabs = dcmotor_v < 0 ? -dcmotor_v : dcmotor_v; \
207
SET_PWM(MOT##motID##_PWM, dcmotor_vabs); \
208
if(dcmotor_v < 0) { digitalClear(M##motID##1);digitalSet(M##motID##2);}\
209
else { digitalClear(M##motID##2); digitalSet(M##motID##1);}\
210
} while(0)
211
#define DCMOTOR_UPDATE_SYM(motID) CALL_FUN(DCMOTOR_UPDATE_SYM_,motID)
212
213
#define DCMOTOR_COMPUTE(motID, mode) do{\
214
DCMOTOR_LOAD_dcmotorVolVars_(motID);\
215
dcmotorCompute(&dcmotor##motID);\
216
DCMOTOR_UPDATE_##mode(motID);\
217
} while(0)
218
219
#define DCMOTOR_SETDIR_(motID) do{ \
220
if(dcmotor_v < 0) dcmotor##motID.VolVars.direction = 0;\
221
else dcmotor##motID.VolVars.direction = 1;\
222
} while(0)
223
224
#define DCMOTOR_COMPUTE_SINGLE(motID, mode) do{\
225
DCMOTOR_COMPUTE(motID, mode);\
226
DCMOTOR_SETDIR_(motID);\
227
} while(0)
228
229
void
dcmotorInput(
t_dcmotor
*mot);
230
#define DCMOTOR_INPUT(motID) dcmotor_input(&dcmotor##motID)
231
232
void
dcmotorDeclareEE(
t_dcmotor
*mot);
233
#define DCMOTOR_DECLARE_EE(motID) do{ dcmotor_declareEE(&dcmotor##motID);} while(0)
234
235
#define DCMOTOR(motID) (dcmotor##motID)
236
#define DCMOTOR_GETPOS(motID) (DCMOTOR(motID).VolVars.Position)
237
238
#endif // _DCMOTOR_H_
t_dcmotorSetting
Definition:
dcmotor.h:67
t_dcmotorIncr
Definition:
dcmotor.h:34
t_ramp
Definition:
ramp.h:37
t_dcmotor
Definition:
dcmotor.h:76
t_dcmotorVars
Definition:
dcmotor.h:44
fruit.h
t_pid
Definition:
pid.h:48
t_dcmotorVolVars
Definition:
dcmotor.h:52
dcmotor
dcmotor.h
Generated on Mon Jan 18 2016 00:28:53 for Fraise by
1.8.6