-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoat_Steering_3.ino
153 lines (120 loc) · 4.19 KB
/
Boat_Steering_3.ino
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
147
148
149
150
151
152
153
#include <Encoder.h>
#include <FlexyStepper.h>
#include <Arduino.h>
#include <Wire.h>
#include <HMC5883L_Simple.h>
#include <PID_v1.h>
double Setpoint = 0;
double Input, Output;
int limit = 5000;
int count = 0;
const int stepPin = 3;
const int dirPin = 6;
const int Enable = 5;
const int Heading_Hold = 7;
const int SET = 8;
double SteeringPosition = 0;
double Steering_Rotations = 0;
float lastMicros = 0;
int Mode = 0;
float heading = 0;
float point_bearing =0;
int Hold_Heading = 0;
int Hold_Reset = 0;
int error = 0;
int wrap360(int directio) {
while (directio > 359) directio -= 360;
while (directio < 0) directio += 360;
return directio;
}
int heading_error(int bearing, int current_heading){
int error = current_heading - bearing;
if (error > 180) error -= 360;
if (error < -180) error += 360;
return error;
}
Encoder myEnc(2, 4);
FlexyStepper stepper;
//HMC5883L_Simple Compass;
//PID myPID(&Input, &Output, &Setpoint,2,5,1, DIRECT);
void setup() {
// put your setup code here, to run once:
pinMode(Heading_Hold, INPUT_PULLUP);
pinMode(SET, INPUT_PULLUP);
pinMode(Enable,OUTPUT);
digitalWrite(Enable,LOW);
//Wire.begin();
//myPID.SetOutputLimits(-1000, 1000);
stepper.connectToPins(stepPin,dirPin);
stepper.setSpeedInStepsPerSecond(50000*16);
stepper.setAccelerationInStepsPerSecondPerSecond(2000*16);
stepper.setStepsPerRevolution(1600);
//Compass.SetSamplingMode(COMPASS_CONTINUOUS);
//Compass.SetDeclination(-0, 16, 'W');
//Compass.SetScale(COMPASS_SCALE_810);
//myPID.SetMode(AUTOMATIC);
Serial.begin(9600);
//Serial.println("Encoder Test:");
}
long oldPosition = -999;
void loop() {
if (count > limit){
count = 0;
//heading = Compass.GetHeadingDegrees();
if (Mode = 1 ){
//myPID.Compute();
}
//Serial.println(Input);
} else {
count = count + 1;
}
if (digitalRead(Heading_Hold) == false){
Mode = 1;
Hold_Reset = 0;
if (Hold_Heading == 0){
//point_bearing = Compass.GetHeadingDegrees();
Hold_Heading = 1;
}
//Input = heading_error(point_bearing, heading);
//SteeringPosition = map(Input,-180,180,-2000,2000);
}
if (digitalRead(SET) == false){
Mode = 2;
Hold_Heading = 0;
if (Hold_Reset == 0){
myEnc.write(0);
stepper.setCurrentPositionInRevolutions(0);
Hold_Reset = 1;
}
}
if (digitalRead(SET) == true & digitalRead(Heading_Hold) == true){ ///--------------Manual Steering Mode
Mode = 0;
Hold_Heading = 0;
Hold_Reset = 0;
//### Steering Input ###//
long newPosition = myEnc.read();
if (newPosition != oldPosition) {
if (newPosition < 2000) {
if (newPosition > -2000) {
oldPosition = newPosition;
SteeringPosition = oldPosition;
//Serial.println(newPosition);
} else {
myEnc.write(oldPosition);
}
} else {
myEnc.write(oldPosition);
}
}
//### End of Steering Input ###//
}
//### Command Stepper Motor ###//
Steering_Rotations = map(SteeringPosition,-2000,2000,-800,800);
Steering_Rotations = Steering_Rotations/100;
stepper.setTargetPositionInRevolutions(Steering_Rotations);
if(!stepper.motionComplete()){ // if stepper motor is not complete its move
stepper.processMovement(); // Command stepper to move to target
}
//## End of Stepper Command ###//
//Serial.println(Mode);
}