-
Notifications
You must be signed in to change notification settings - Fork 19
/
Da.cpp
92 lines (81 loc) · 2.57 KB
/
Da.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
/*
****************************************************************************
* Copyright (c) 2015 Dark Guan <[email protected]> *
* This file is part of Ananas. *
* *
* Ananas 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, or *
* (at your option) any later version. *
* *
* Ananas 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 Ananas. If not, see <http://www.gnu.org/licenses/>. *
****************************************************************************
*/
/*
* Da.cpp
*
* Created on: 2015年12月14日
* Author: Dark
*/
#include "Da.h"
#include <TLC5615.h>
/*
*initial the DA,and set a initial voltage
*avoid the driver shake
*/
TLC5615 DA(DA_DIN, DA_SCLK, DA_CS);
uint16_t maxvol;
uint16_t minvol;
uint16_t volfactor;
uint8_t errorallow;
uint16_t volstate;
void initial_DA() {
DA.InitTLC5615();
//TODO 这里要加上从EEPROM里面来获取匹配电机信息的步骤
//匹配最大电压和最小电压
//这里要限制最小电压,要不然驱动器会抖
maxvol = MAX_DA;
minvol = DA_initial;
errorallow = MAXERROR;
volfactor = (maxvol - minvol) / errorallow;
//initial DA
DA_SET_V(minvol); //以最小值初始DA 这个值后期将是从EEPROM里面读取到的
}
/*
* set the voltage
*/
void DA_SET_V(unsigned int Vol) {
//the voltage is limited
//the voltage large then
if (Vol > maxvol) {
DA.DAConvert(maxvol);
volstate = maxvol;
} else if (Vol < minvol) {
DA.DAConvert(minvol);
volstate = minvol;
} else {
DA.DAConvert(Vol);
volstate = Vol; //store state
}
}
/*
* adjust the voltage
*/
void managerVoltage(int error) {
//change the voltage base on the difference between the pulse and the encoder count
//max error
if (error >= errorallow) {
DA_SET_V(maxvol);
} else {
DA_SET_V(minvol + error * volfactor);
}
}
uint16_t getvol() {
return volstate;
}