This repository has been archived by the owner on Jul 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMotor.cpp
146 lines (119 loc) · 2.6 KB
/
Motor.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
/*
* Motor.cpp
* mindstormssimulation
*
* Created by Torsten Kammer on 19.11.10.
* Copyright 2010 __MyCompanyName__. All rights reserved.
*
*/
#include "Motor.h"
#include "Constants.h"
#include <cmath>
#include <algorithm>
Motor::Motor()
: power(0.0f),turnLimit(0.0f), turnedSinceTurnLimitReset(0.0f), blockCounter(0.0f), rotationCounter(0.0f), syncState(NotSynced)
{
}
float Motor::turn(float delta) throw()
{
float turned = getSpeed() * delta;
turnedSinceTurnLimitReset += turned;
blockCounter += turned;
rotationCounter += turned;
return turned;
}
void Motor::resetAllCounters() throw()
{
resetBlockCounter();
resetRotationCounter();
setTurnTarget(0.0f);
}
void Motor::resetBlockCounter() throw()
{
blockCounter = 0.0f;
}
void Motor::resetRotationCounter() throw()
{
rotationCounter = 0.0f;
}
void Motor::setBlockCounterValue(float val) throw()
{
blockCounter = val;
}
void Motor::setTargetCounterValue(float val) throw()
{
turnedSinceTurnLimitReset = val;
}
void Motor::setRotationCounterValue(float val) throw()
{
rotationCounter = val;
}
float Motor::getBlockCounterValue() const throw()
{
return blockCounter;
}
float Motor::getRotationCounterValue() const throw()
{
return rotationCounter;
}
float Motor::getTargetCounterValue() const throw()
{
return turnedSinceTurnLimitReset;
}
void Motor::setTurnTarget(float target) throw()
{
turnLimit = fabsf(target);
turnedSinceTurnLimitReset = 0.0f;
}
float Motor::getTurnTarget() const throw()
{
return turnLimit;
}
void Motor::setTurnFactor(float factor) throw()
{
robotTurnFactor = factor;
}
float Motor::getTurnFactor() const throw()
{
return robotTurnFactor;
}
void Motor::setPower(float value) throw()
{
power = value;
}
float Motor::getPower() const throw()
{
return power;
}
float Motor::getSpeed() const throw()
{
float effectivePower = power;
if (turnLimit > 0.0f && fabsf(turnedSinceTurnLimitReset) >= turnLimit)
effectivePower = 0.0f;
else if (syncState == IsLeft)
effectivePower = power * std::min(std::max(-2.0f*robotTurnFactor + 1.f, -1.f), 1.f);
else if (syncState == IsRight)
effectivePower = power * std::min(std::max(2.0f*robotTurnFactor + 1.f, -1.f), 1.f);
return effectivePower * MotorConstant::powerToSpeedFactor;
}
bool Motor::isRunning() const throw()
{
if (turnLimit > 0.0f && fabsf(turnedSinceTurnLimitReset) >= turnLimit)
return false;
if (power == 0.0f)
return false;
return true;
}
// Connections
void Motor::setIsLeftMotor() throw()
{
syncState = IsLeft;
}
void Motor::setIsRightMotor() throw()
{
syncState = IsRight;
}
void Motor::setIsNotSynchronized() throw()
{
syncState = NotSynced;
}