-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockPower.cpp
121 lines (107 loc) · 3.11 KB
/
blockPower.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
#include <DualMC33926MotorShield.h>
#include "blockPower.h"
extern DualMC33926MotorShield motorDriver;
extern Block block[_NUM_BLOCKS];
void Block::init( int motorNum ){
_motorNum = motorNum;
};
void Block::setOccupied(){
_enterTime = millis();
_occupied = true;
};
void Block::setClear(){
setStop();
_occupied = false;
_transitTime = millis() - _enterTime;
if( _transitTime > _MAX_TRANSIT_TIME )
_transitTime = _MAX_TRANSIT_TIME;
Serial.print( "transitTime for motor " );
Serial.println( _motorNum );
Serial.print( ": " );
Serial.println( _transitTime );
_exitTime = millis();
};
void Block::setFull(){
if( _speed != 100.0 ) {
Serial.print( "Set Full Speed motor " );
Serial.println( _motorNum );
}
_setSpeed( 100.0 );
};
void Block::setHalf(){
if( _speed != 50.0 ) {
Serial.print( "Set Half Speed motor " );
Serial.println( _motorNum );
}
_setSpeed( 50.0 );
};
void Block::setStop(){
_setSpeed( 0.0 );
Serial.print( "Set Stop motor " );
Serial.println( _motorNum );
};
void Block::check(){
int targetSpeed;
if( millis() - _exitTime > 2 * _transitTime )
setFull();
else if( millis() - _exitTime > _transitTime )
setHalf();
int reading = analogRead( _IN_VOLTAGE_PIN );
float inVoltage = 1.4 + 43.0 * reading / (204.6 * 10.0); // add in the diode drop
reading = analogRead( _IN_DIRECTION_PIN );
bool inDirection = (reading > 10.0);
reading = analogRead( _IN_SUPPLY_PIN );
float inSupply = 43.0 * reading / (204.6 * 10.0); // small drop since there is no load
float ratio = inVoltage / inSupply;
if( inSupply != 0.0 ) {
targetSpeed = ratio * 400.0 * _speed / 100.0;
if( !inDirection )
targetSpeed *= -1;
} else
targetSpeed = 0;
if( (inSupply > 6.40) && (inVoltage > 1.5) ) {
// accounts for diode voltage drop under load
digitalWrite(_NOT_D2_PIN,HIGH); // turn on outputs
_outState = HIGH;
} else if( (inSupply < 5.0) || (inVoltage < 1.5) ) {
// drive too low or direction measurement inaccurate
digitalWrite(_NOT_D2_PIN,LOW); // turn off outputs
_outState = LOW;
motorDriver.setM1Speed( 0 );
motorDriver.setM2Speed( 0 );
_prevSpeed = 0;
}
int speedIncrement = _SPEED_CHANGE_RATE * (millis() - _lastCheckTime) / 1000;
if( !speedIncrement )
speedIncrement = 1;
if( _outState ) {
if( _prevSpeed < targetSpeed ) {
_prevSpeed += speedIncrement;
if( _prevSpeed > targetSpeed )
_prevSpeed = targetSpeed;
}
else if ( _prevSpeed > targetSpeed ) {
_prevSpeed -= speedIncrement;
if( _prevSpeed < targetSpeed )
_prevSpeed = targetSpeed;
}
if( _motorNum )
motorDriver.setM2Speed( _prevSpeed );
else
motorDriver.setM1Speed( _prevSpeed );
}
_lastCheckTime = millis();
}
void Block::_setSpeed( float speed ){
_speed = speed;
}
void BlockPowerManager::init() {
motorDriver.init();
digitalWrite(_NOT_D2_PIN,LOW); // turn off outputs
for( int i = 0; i < _NUM_BLOCKS; i++ )
block[i].init(i);
}
void BlockPowerManager::check() {
for( int i = 0; i < _NUM_BLOCKS; i++ )
block[i].check();
};