-
Notifications
You must be signed in to change notification settings - Fork 3
/
actuator.h
132 lines (113 loc) · 3.22 KB
/
actuator.h
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
#ifndef ACTUATOR_MODULE
#define ACTUATOR_MODULE
#include "Arduino.h"
#include "definitions.h"
struct Actuator
{
public:
enum State {
STILL,
PUSH,
PULL
};
private:
PIN_TYPE motor_push_pin;
PIN_TYPE motor_pull_pin;
int max_distance_pwm;
State current_state;
bool change_required;
GlobalState* global_state;
public:
CONST_PIN_TYPE ANALOUGE_PIN = 0;
Actuator (PIN_TYPE motor_push_pin, PIN_TYPE motor_pull_pin,
int max_distance_pwm, GlobalState* global_state)
{
this->current_state = STILL;
this->motor_push_pin = motor_push_pin;
this->motor_pull_pin = motor_pull_pin;
this->max_distance_pwm = max_distance_pwm;
this->global_state = global_state;
}
void setup()
{
this->global_state->actuator_at_max_push = false;
// Serial.println("Setting up actiator");
this->setState(PULL);
// printState();
while (this->current_state != STILL)
{
this->motorLoop();
//Serial.print("Setup loop - current state: ");
//Serial.println(this->current_state);
}
//Serial.println("Setup loop done");
}
void printState()
{
Serial.print("Actuator current state: ");
Serial.print(this->current_state);
Serial.print(" - and PWM value: ");
Serial.println(analogRead(ANALOUGE_PIN));
}
void setState(State state)
{
// Serial.print("Current state: ");
// Serial.println(this->current_state);
if (state != this->current_state)
{
this->current_state = state;
this->change_required = true;
}
}
void motorLoop()
{
// Serial.print("Motor loop - ");
//printState();
if (this->current_state == STILL)
{
return;
}
int sensor_value = analogRead(ANALOUGE_PIN);
if (sensor_value >= this->max_distance_pwm -30) // Leave a bit of buffer
{
this->global_state->actuator_at_max_push = true;
}
else
{
this->global_state->actuator_at_max_push = false;
}
if (this->current_state == PUSH)
{
if (sensor_value >= this->max_distance_pwm)
{
digitalWrite(this->motor_push_pin, LOW);
digitalWrite(this->motor_pull_pin, LOW);
this->current_state = STILL;
this->change_required = false;
}
if (this->change_required)
{
digitalWrite(this->motor_push_pin, HIGH);
digitalWrite(this->motor_pull_pin, LOW);
this->change_required = false;
}
}
else if (this->current_state == PULL)
{
if (sensor_value <= 20) // Doesn't have to be completely retracted
{
digitalWrite(this->motor_push_pin, LOW);
digitalWrite(this->motor_pull_pin, LOW);
this->current_state = STILL;
this->change_required = false;
}
if (this->change_required)
{
digitalWrite(this->motor_push_pin, LOW);
digitalWrite(this->motor_pull_pin, HIGH);
this->change_required = false;
}
}
}
};
#endif