-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamic_control.cpp
More file actions
71 lines (71 loc) · 2.08 KB
/
Dynamic_control.cpp
File metadata and controls
71 lines (71 loc) · 2.08 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
/*
Dynamic_control.cpp - Класс для плавного управления системой из двух двигателей.
Created by Smirnov Artem, September 3, 2014.
*/
#include <Motor.h>
#include <Arduino.h>
Dynamic_control::Dynamic_control(Motor M1, Motor M2)
{
_M1 = M1;
_M2 = M2;
};
void Dynamic_control::_dSmooth(int lSpeed, int rSpeed, int time)
{
//может быть надо оба сажать до определенной скорости, а не до нуля
_lSpeed = lSpeed;
_rSpeed = rSpeed;
//надо бы добавить изначальное чтение значений в регистрах,
//чтобы разгон делать на от постоянной величины (50),
//либо задавать величину от которой будет вестись разгон
int maxSpeed;
if (_lSpeed < _rSpeed) maxSpeed = _rSpeed; else maxSpeed = _lSpeed;
for (int i = 50; i<=maxSpeed; i++)
{
if (i<=_lSpeed) _M1.setSpeed(i);
if (i<=_rSpeed) _M2.setSpeed(i);
delay(time); //чем больше это значение, тем медленнее разгоняется двигатель и наоборот
}
};
void Dynamic_control::dSmoothRun(bool direction, int lSpeed, int rSpeed)
{
_lSpeed = lSpeed;
_rSpeed = rSpeed;
if (direction)
{
_M1.setDirection(1);
_M2.setDirection(1);
}
else
{
_M1.setDirection(0);
_M2.setDirection(0);
}
_dSmooth(_lSpeed, _rSpeed, 30);
};
void Dynamic_control::dReversal(bool direction, int lSpeed, int rSpeed)
{
_lSpeed = lSpeed;
_rSpeed = rSpeed;
if (direction)
{
_M1.setDirection(1);
_M2.setDirection(0);
}
else
{
_M1.setDirection(0);
_M2.setDirection(1);
}
_dSmooth(_lSpeed, _rSpeed, 30);
};
void Dynamic_control::dSmoothStop(int time)
{
int maxSpeed;
if (_lSpeed < _rSpeed) maxSpeed = _rSpeed; else maxSpeed = _lSpeed;
for (int i = maxSpeed; i>0; i--)
{
if (i<_lSpeed) _M1.setSpeed(i);
if (i<_rSpeed) _M2.setSpeed(i);
delay(time); //чем больше это значение, тем медленнее разгоняется двигатель и наоборот
}
};