forked from vedderb/vesc_tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.cpp
230 lines (199 loc) · 6.88 KB
/
utility.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
Copyright 2017 - 2018 Benjamin Vedder [email protected]
This file is part of VESC Tool.
VESC Tool 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.
VESC Tool 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 <http://www.gnu.org/licenses/>.
*/
#include "utility.h"
#include <cmath>
#include <QProgressDialog>
#include <QEventLoop>
#include <QNetworkAccessManager>
#include <QUrl>
#include <QNetworkRequest>
#include <QDebug>
#include <QNetworkReply>
#include <QMessageBox>
#include <QFile>
#ifdef Q_OS_ANDROID
#include <QtAndroid>
#include <QAndroidJniObject>
#endif
Utility::Utility(QObject *parent) : QObject(parent)
{
}
double Utility::map(double x, double in_min, double in_max, double out_min, double out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
float Utility::throttle_curve(float val, float curve_acc, float curve_brake, int mode)
{
float ret = 0.0;
float val_a = fabsf(val);
if (val < -1.0) {
val = -1.0;
}
if (val > 1.0) {
val = 1.0;
}
float curve;
if (val >= 0.0) {
curve = curve_acc;
} else {
curve = curve_brake;
}
// See
// http://math.stackexchange.com/questions/297768/how-would-i-create-a-exponential-ramp-function-from-0-0-to-1-1-with-a-single-val
if (mode == 0) { // Power
if (curve >= 0.0) {
ret = 1.0 - powf(1.0 - val_a, 1.0 + curve);
} else {
ret = powf(val_a, 1.0 - curve);
}
} else if (mode == 1) { // Exponential
if (fabsf(curve) < 1e-10) {
ret = val_a;
} else {
if (curve >= 0.0) {
ret = 1.0 - ((expf(curve * (1.0 - val_a)) - 1.0) / (expf(curve) - 1.0));
} else {
ret = (expf(-curve * val_a) - 1.0) / (expf(-curve) - 1.0);
}
}
} else if (mode == 2) { // Polynomial
if (curve >= 0.0) {
ret = 1.0 - ((1.0 - val_a) / (1.0 + curve * val_a));
} else {
ret = val_a / (1.0 - curve * (1.0 - val_a));
}
} else { // Linear
ret = val_a;
}
if (val < 0.0) {
ret = -ret;
}
return ret;
}
bool Utility::autoconnectBlockingWithProgress(VescInterface *vesc, QWidget *parent)
{
if (!vesc) {
return false;
}
QProgressDialog dialog("Autoconnecting...", QString(), 0, 0, parent);
dialog.setWindowModality(Qt::WindowModal);
dialog.show();
bool res = vesc->autoconnect();
if (!res) {
vesc->emitMessageDialog(QObject::tr("Autoconnect"),
QObject::tr("Could not autoconnect. Make sure that the USB cable is plugged in "
"and that the VESC is powered."),
false);
}
return res;
}
void Utility::checkVersion(VescInterface *vesc)
{
QString version = QString::number(VT_VERSION);
QUrl url("https://vesc-project.com/vesctool-version.html");
QNetworkAccessManager manager;
QNetworkRequest request(url);
QNetworkReply *reply = manager.get(request);
QEventLoop loop;
QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
QString res = QString::fromUtf8(reply->readAll());
if (res.startsWith("vesctoolversion")) {
res.remove(0, 15);
res.remove(res.indexOf("vesctoolversion"), res.size());
if (res.toDouble() > version.toDouble()) {
if (vesc) {
vesc->emitStatusMessage("A new version of VESC Tool is available", true);
vesc->emitMessageDialog(QObject::tr("New Software Available"),
QObject::tr("A new version of VESC Tool is available. Go to "
"<a href=\"http://vesc-project.com/\">http://vesc-project.com/</a>"
" to download it and get all the latest features."),
true);
} else {
qDebug() << "A new version of VESC Tool is available. Go to vesc-project.com to download it "
"and get all the latest features.";
}
}
} else {
qWarning() << res;
}
}
QString Utility::fwChangeLog()
{
QFile cl("://res/firmwares/CHANGELOG");
if (cl.open(QIODevice::ReadOnly | QIODevice::Text)) {
return QString::fromUtf8(cl.readAll());
} else {
return "";
}
}
QString Utility::vescToolChangeLog()
{
QFile cl("://res/CHANGELOG");
if (cl.open(QIODevice::ReadOnly | QIODevice::Text)) {
return QString::fromUtf8(cl.readAll());
} else {
return "";
}
}
QString Utility::aboutText()
{
return tr("<b>VESC® Tool %1</b><br>"
#if defined(VER_ORIGINAL)
"Original Version<br>"
#elif defined(VER_PLATINUM)
"Platinum Version<br>"
#elif defined(VER_GOLD)
"Gold Version<br>"
#elif defined(VER_SILVER)
"Silver Version<br>"
#elif defined(VER_BRONZE)
"Bronze Version<br>"
#elif defined(VER_FREE)
"Free of Charge Version<br>"
#endif
"© Benjamin Vedder 2016 - 2018<br>"
"<a href=\"mailto:[email protected]\">[email protected]</a><br>"
"<a href=\"https://vesc-project.com/\">https://vesc-project.com/</a>").
arg(QString::number(VT_VERSION));
}
QString Utility::uuid2Str(QByteArray uuid, bool space)
{
QString strUuid;
for (int i = 0;i < uuid.size();i++) {
QString str = QString::number(uuid.at(i), 16).
rightJustified(2, '0').toUpper();
strUuid.append(((i > 0 && space) ? " " : "") + str);
}
return strUuid;
}
bool Utility::requestFilePermission()
{
#ifdef Q_OS_ANDROID
// Note: The following should work on Qt 5.10
// https://codereview.qt-project.org/#/c/199162/
// QtAndroid::PermissionResult r = QtAndroid::checkPermission("android.permission.WRITE_EXTERNAL_STORAGE");
// if(r == QtAndroid::PermissionResult::Denied) {
// QtAndroid::requestPermissionsSync( QStringList() << "android.permission.WRITE_EXTERNAL_STORAGE" );
// r = QtAndroid::checkPermission("android.permission.WRITE_EXTERNAL_STORAGE");
// if(r == QtAndroid::PermissionResult::Denied) {
// return false;
// }
// }
return true;
#else
return true;
#endif
}