Skip to content

Commit 694ab4c

Browse files
committed
Control motor speed with threshold
1 parent b8cd62e commit 694ab4c

File tree

5 files changed

+89
-62
lines changed

5 files changed

+89
-62
lines changed
Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
const int pwm = 3; // PWM control for motor outputs 1 and 2
22
const int dir = 2; // direction control for motor outputs 1 and 2
3-
const int forwardControlPin = 7,
3+
const int motorSlowPin = 5,
4+
forwardControlPin = 7,
45
backwardControlPin = 6;
56

7+
const int slowSpeed = 200, // in [0,1000]
8+
fastSpeed = 1000; // in [0, 1000]
9+
10+
uint8_t motorSpeed = 200; // in [0,255]
11+
612
void setup()
713
{
814
pinMode(forwardControlPin, INPUT_PULLUP);
915
pinMode(backwardControlPin, INPUT_PULLUP);
16+
pinMode(motorSlowPin, INPUT_PULLUP);
1017

1118
// set control pins to be outputs
1219
pinMode(pwm, OUTPUT);
@@ -15,23 +22,40 @@ void setup()
1522

1623
void loop()
1724
{
18-
bool fwd = digitalRead(forwardControlPin) == HIGH;
19-
bool bwd = digitalRead(backwardControlPin) == HIGH;
20-
21-
if (fwd && !bwd) {
22-
// forward
23-
digitalWrite(dir, LOW);
24-
analogWrite(pwm, 255);
25-
}
26-
else if (bwd && !fwd) {
27-
// backward
28-
digitalWrite(dir, HIGH);
29-
analogWrite(pwm, 255);
30-
}
31-
else {
32-
// neutral
33-
analogWrite(pwm, 0);
34-
}
25+
unsigned long lastMillis = 0;
26+
while (true) {
27+
unsigned long currentMillis = millis();
28+
if (currentMillis < lastMillis) {
29+
lastMillis = currentMillis;
30+
continue;
31+
}
32+
lastMillis = currentMillis;
3533

36-
delay(100);
34+
bool fwd = digitalRead(forwardControlPin) == HIGH;
35+
bool bwd = digitalRead(backwardControlPin) == HIGH;
36+
bool slow = digitalRead(motorSlowPin) == HIGH;
37+
38+
if (fwd && !bwd) {
39+
// forward
40+
digitalWrite(dir, LOW);
41+
analogWrite(pwm, motorSpeed);
42+
}
43+
else if (bwd && !fwd) {
44+
// backward
45+
digitalWrite(dir, HIGH);
46+
analogWrite(pwm, motorSpeed);
47+
}
48+
else {
49+
// neutral
50+
analogWrite(pwm, 0);
51+
}
52+
53+
// turn motor only for a certain period of time
54+
int onDelay = slow ? slowSpeed : fastSpeed;
55+
delay(onDelay);
56+
if (onDelay < 1000) {
57+
analogWrite(pwm, 0);
58+
delay(1000-onDelay);
59+
}
60+
}
3761
}

Parameters.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
// valve controller
55
const float toleratedPsiDelta = 0.1;
6+
const float lowSpeedPsiDelta = 0.5;
67
const float valveSpeed = 0.01; // disabled
78

89
// motor control
910
const float openingTolerance = 0.1; // disabled
10-
const int motorSpeed = 100; // value in (0,1000]
1111

1212
const float minOpening = 0.05; // disabled
1313
const float maxOpeningAtPsiDelta = 1.5; // disabled

PressureControlSoftware.ino

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,47 +12,36 @@ void setup() {
1212
#endif
1313
Display::init();
1414
Valve::init();
15+
pinMode(13, OUTPUT);
1516
}
1617

1718
void loop() {
18-
unsigned long valveLastUpdatedMillis = millis();
19+
digitalWrite(13, !digitalRead(13)); // activity indicator
1920

20-
while (true) {
21-
unsigned long currentMillis = millis();
22-
if (currentMillis < valveLastUpdatedMillis) {
23-
// handle millis overflow case
24-
continue;
25-
}
26-
27-
float targetPsi = getTargetPressureDelta();
28-
float measuredPsi = getPressureMeasuredDelta();
29-
float offPsi = targetPsi - measuredPsi;
30-
31-
float valveOpening = Valve::getOpening();
32-
33-
#ifndef SERIAL_DEBUG
34-
Display::showPressureSelection(measuredPsi, targetPsi);
35-
Display::showValveOpening(valveOpening, 0.f);
36-
Display::submit();
37-
#endif
38-
39-
#ifdef SERIAL_DEBUG
40-
Serial.print("Off by: ");
41-
Serial.print(offPsi, 2);
42-
Serial.println(" psi");
43-
#endif
21+
float targetPsi = getTargetPressureDelta();
22+
float measuredPsi = getPressureMeasuredDelta();
23+
float offPsi = targetPsi - measuredPsi;
24+
25+
float valveOpening = Valve::getOpening();
26+
27+
#ifndef SERIAL_DEBUG
28+
Display::showPressureSelection(measuredPsi, targetPsi);
29+
Display::showValveOpening(valveOpening, 0.f);
30+
Display::submit();
31+
#endif
4432

45-
// motor movement
46-
// stop the motor depending on the motor speed
47-
if (currentMillis % 1000 > motorSpeed) {
48-
Valve::stop();
49-
continue;
50-
}
51-
if (abs(offPsi) > toleratedPsiDelta) {
52-
Valve::move(offPsi < 0); // open or close depending on inequality
53-
}
54-
else {
55-
Valve::stop();
56-
}
33+
#ifdef SERIAL_DEBUG
34+
Serial.print("Off by: ");
35+
Serial.print(offPsi, 2);
36+
Serial.println(" psi");
37+
#endif
38+
39+
// motor movement
40+
if (abs(offPsi) > toleratedPsiDelta) {
41+
Valve::move(offPsi < 0); // open or close depending on inequality
42+
}
43+
else {
44+
Valve::stop();
5745
}
46+
Valve::setSlow(abs(offPsi) < lowSpeedPsiDelta); // control speed
5847
}

README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ This Arduino software was developed by Denk Development in Mar 2019 for the micr
1010

1111
_as-is_ + the following
1212
* Connect a second Arduino to the motor through a motor shield (shield pins 1 and 2, as well as GND and VCC).
13-
* Connect the Main-Arduino to the Motor-Arduino by hooking up the digital pins 6 <--> 6 and 7 <--> 7, as well as GND <--> GND.
13+
* Connect the Main-Arduino to the Motor-Arduino by hooking up the digital pins 5 <--> 5, 6 <--> 6, and 7 <--> 7, as well as GND <--> GND.
1414

1515
## Software Architecture
1616

@@ -32,14 +32,21 @@ There are two `.ino` file, one for the Motor-Arduino, one for the Main-Arduino.
3232

3333
Parameters can be adjusted in the header file `Parameters.h`. Not that instead of using a comma `,`, a dot `.` must be used for decimals. Lines are terminated with a semicolon `;`.
3434

35-
* **Tolerated PSI delta** `toleratedPsiDelta`, _akzeptierte Differenz von Soll- und Ist-PSI_, permitted value range [0,maxPSI]. After which difference in preassure levels (target vs. actual) a motion is being triggered. Note that there is no tolerance for motion because this is already caught by the motor positioning tolerance.
36-
* **Motor speed** `motorSpeed`, permitted values range (0,1000]. Defines how many milliseconds within one second the motor is permitted to move. When set to 1000, for instance, the motor may more all the time. When set to 200, it will move for (at most) 200 ms and will stay still for the next 800 ms.
35+
* **Tolerated PSI delta** `toleratedPsiDelta`, _akzeptierte Differenz von Soll- und Ist-PSI_, permitted value range [0, maxPSI]. After which difference in preassure levels (target vs. actual) a motion is being triggered. Note that there is no tolerance for motion because this is already caught by the motor positioning tolerance.
36+
* **Low speed PSI delta** `lowSpeedPsiDelta`, permitted value range [0, maxPSI]. Threshold after which the motor will turn slower.
37+
* ~~**Motor speed** `motorSpeed`, permitted values range (0,1000]. Defines how many milliseconds within one second the motor is permitted to move. When set to 1000, for instance, the motor may more all the time. When set to 200, it will move for (at most) 200 ms and will stay still for the next 800 ms.~~
3738
* [disabled] ~~**Valve adjustment speed** `valveSpeed`, _Geschwindigkeit der Ventilanpassung_, permitted value range [0, inf). How quickly the valve adjusts do divergence of actual and target pressure. Per 100 ms.~~
3839
Now controlled through the `MotorController.ino` script or `motorSpeed` variable.
39-
* [disabled] ~~**Opening tolerance** `openingTolerance`, _Motor-Positions-Toleranz_, permitted value range [0,1]. Controls which deviation of target motor position and actual motor position is still tolerated until a motion is being triggered. If the motor target is e.g. `0.2`, and the measured position is `0.25`, nothing will happen with an `openingTolerance` greater than or equal to `0.05`. The tolerance for motion is derived from this value through division by 2. So while adjusting the motor position, movement will not stop unless half of the `openingTolerance` is reached.~~
40+
* [disabled] ~~**Opening tolerance** `openingTolerance`, _Motor-Positions-Toleranz_, permitted value range [0, 1]. Controls which deviation of target motor position and actual motor position is still tolerated until a motion is being triggered. If the motor target is e.g. `0.2`, and the measured position is `0.25`, nothing will happen with an `openingTolerance` greater than or equal to `0.05`. The tolerance for motion is derived from this value through division by 2. So while adjusting the motor position, movement will not stop unless half of the `openingTolerance` is reached.~~
4041
* [disabled] ~~**Minimum valve opening**, `minOpening`, _geringste Öffnung des Ventils_, permitted value range [0,1]. Once a deviation from the target PSI is exceeding `toleratedPsiDelta` the valve will be opened to `minOpening` the least. May be set to `0` when in doubt.~~
4142
* [disabled] ~~**Max opening at PSI delta** `maxOpeningAtPsiDelta`, _Differenz von Soll- und Ist-PSI, bei der das Ventil komplett offen steht_, permitted value range [0,maxPSI). At what PSI difference the valve is openend entirely.~~
4243

44+
Parameters in `MotorController.ino`
45+
46+
* **Motor slow speed** `slowSpeed`, values in range [0, 1000]. How many milliseconds of a second the motor is turning in slow speed mode.
47+
* **Motor fast speed** `fastSpeed`, values in range [0, 1000]. How many milliseconds of a second the motor is turning in fast speed mode.
48+
* **Motor speed** `motorSpeed`, values in range [0, 255]. How fast the motor is actually turning during the movement phases; correlates with motor torque.
49+
4350
## Warranty
4451

4552
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Valve.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,20 @@
1010
// https://www.velleman.eu/support/downloads/?code=VMA03
1111

1212
const uint8_t openingPin = A0,
13+
motorSlowPin = 5,
1314
motorBackwardPin = 6,
14-
motorForwardPin = 7;
15+
motorForwardPin = 7;
1516

1617
class Valve
1718
{
1819
public:
1920
static void init() {
2021
pinMode(motorBackwardPin, OUTPUT);
2122
pinMode(motorForwardPin, OUTPUT);
23+
pinMode(motorSlowPin, OUTPUT);
2224
digitalWrite(motorBackwardPin, HIGH);
2325
digitalWrite(motorForwardPin, HIGH);
26+
pinMode(motorSlowPin, LOW);
2427
moving = false;
2528
};
2629

@@ -96,6 +99,10 @@ class Valve
9699
digitalWrite(motorForwardPin, HIGH);
97100
};
98101

102+
static void setSlow(bool slow) {
103+
digitalWrite(motorSlowPin, slow);
104+
};
105+
99106
private:
100107
static bool moving;
101108
};

0 commit comments

Comments
 (0)