-
Notifications
You must be signed in to change notification settings - Fork 0
/
turret-mcu.ino
executable file
·229 lines (195 loc) · 5.82 KB
/
turret-mcu.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* Turret Control
* Created by: Naufalino Fadel Hutomo, anharaf
* Apr2018
*/
#include <WiFiUdp.h>
#include <WiFiServer.h>
#include <WiFiClientSecure.h>
#include <WiFiClient.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <string.h>
#include <Wire.h>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define stepPin 2 //pulse pin for controlling turret stepper
#define dirPin 4 //pulse pin for controlling turret stepper
//CONSTANT
const float yaw_RES = 1.8/4 ; //nema 17 resolution is 1.8 degree, with gear ratio between motor and turret is 1:4
String readStr;
float yaw_set = 0; //for turret yaw set condition
float yaw_act=0; //heading measurement of yaw
bool step_dir = HIGH; //CW is HIGH
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Update these with values suitable for your network.
const char* ssid = "Titanic";
const char* password = "samakayakhotspotitb";
const char* mqtt_server = "192.168.0.111";
const char* mqtt_userName = "anharaf";
const char* mqtt_password = "anhar1234";
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
//Serial.print("Message arrived [");
//Serial.print(topic);
//Serial.print("] ");
for (int i = 0; i < length; i++) {
//Serial.print((char)payload[i]);
}
Serial.println();
//payload[length] = '\0';
//Serial.print("payload : [");
//Serial.print((char *)payload);
//Serial.print("]");
StaticJsonBuffer<300> JSONBuffer; //Memory pool
JsonObject& parsed = JSONBuffer.parseObject((char *)payload); //Parse message
if (!parsed.success()) {
Serial.println("parseObject() failed");
return;
}
int yaw = parsed["yaw"];
int pitch= parsed["pitch"];
int fire=parsed["fire"];
yaw_set= yaw;
move_all(); // move all actuator
Serial.println(yaw_set);
// Serial.println(pitch);
// Serial.println(fire);
}
void setup() {
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Sets pins Mode
//pinMode(stepPin,OUTPUT);
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
Serial.print("Setup start");
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
client.setCallback(callback);
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client", mqtt_userName, mqtt_password)) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("outTopic", "hello world");
// ... and resubscribe
client.subscribe("inTopic");
}
else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
long now = millis();
if (now - lastMsg > 2000) {
lastMsg = now;
++value;
snprintf(msg, 75, "hello world #%ld", value);
Serial.print("Publish message: ");
/*
snprintf(msg,8,"y%d\n",yaw_act); //the message transmitted to raspi is the alue of yaw act as a feedback
*/
Serial.println(msg);
client.publish("outTopic", msg);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//bool get_setPoint(){ }
void move_turret(float degree){
//Function to move turret with nema 17 stepper
float err = normalDeg(degree - yaw_act);
int step_count;
if (err>0)
{
digitalWrite(dirPin,LOW); // Enables the motor to move with vector direction upward the axis. In this case, move gear ccw -> move turret in clockwise direction
step_count = round(err / yaw_RES);
}else
{
digitalWrite(dirPin,HIGH); // Enables the motor to move turret in counterclockwise direction
step_count = round(-err / yaw_RES);
}
for (int i=0; i < step_count; i++)
{
digitalWrite(stepPin,HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin,LOW);
delayMicroseconds(1000);
}
if (err>0)
{
yaw_act = normalDeg(yaw_act + step_count * yaw_RES);
}else
{
yaw_act = normalDeg(yaw_act - step_count * yaw_RES);
}
/*
Serial.print("yaw_act = ");
Serial.print(yaw_act);
*/
}
void move_all(){
if(yaw_set != yaw_act) //if the value change
{
move_turret(yaw_set);
}
}
float normalDeg(float deg)
{
//normalized degree value into -179.99 until 180
while (deg <= -180)
{
deg += 360;
}
while (deg > 180)
{
deg -= 360;
}
return deg;
}
bool isTolerant(float ex, float real, float tol)
{
// check whether the value is under accuracy of measurement tool
bool ret = false;
if ( ((real - ex) < tol) && ((ex-real) < tol) )
{
ret = true;
}
return ret;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////