-
Notifications
You must be signed in to change notification settings - Fork 0
/
YK31_Azimuth1.ino
175 lines (152 loc) · 4.38 KB
/
YK31_Azimuth1.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
/*
This C++ code is for the YK-31 azimuth capture unit
comprising an ATMEGA328PB processor running at 16 MHz.
The processor captures azimuth with a 1 degree resolution,
from a magnetic encoder yielding 90 quadrature cycles per
revolution, fully decoded to 360 pulses per revolution,
plus one index pulse per revolution. All captured data
is sent serially at 115200 bps to a Processing app on a PC.
There is a 1:15 gearing ratio between the Missile Guidance
radar (MGR)and the azimuth encoder. One revolution of the MGR
equals 15 revolutions of the azimuth encoder.
(c)2017-2019 Rajiv Tyagi
(c)2017-2019 T&C Technology (India) Pvt. Ltd.
ALL RIGHTS RESERVED
This code is perpetually licensed to the Indian Air Force
for use exclusively in the YK-31 Missile Guidance Radar Console
of the Pechora Surface to Air Guided Missile system.
*/
#include <EEPROMex.h>
#include <EEPROMVar.h>
//#include <EEPROM.h>
#include<Encoder.h>
#include <PinChangeInt.h>
//#include <PinChangeIntConfig.h>
#include<VSync.h>
#include "Arduino.h"
ValueSender<2> sender;
#define indexIntPin 12
#define toggleSw 4
#define powerFailPin 5
#define ledPin 6
#define buttonPin 7
#define TSV 9
#define LR_Pin 10 //Long Range mode
Encoder myEnc(2, 3);
volatile boolean isIndex = false;
volatile boolean rawIndex = true;
volatile boolean toggle;
volatile boolean powerFail;
volatile boolean buttonPressed = HIGH;
volatile int indexCount = 0;
boolean testBool = HIGH;
boolean isRawOffsetRecorded = true;
int beamPos;
int startPos;
int indexPos;
int counter = 0;
int addressBeamPos = 0; //EEPROM storing address for beamPos
int addressOffset = 10; //EEPROM storage address of offset
int rawOffset = 0;
int offset1 = 0;
int Mode;
int LR;
void setup() {
pinMode(2, INPUT); // Encoder chanel 1
pinMode(3, INPUT); // Encoder channel 2
pinMode(toggleSw, INPUT_PULLUP); // toggle switch to change beam rotation direction
pinMode(powerFailPin, INPUT); //when system turns off, save position
pinMode(ledPin, OUTPUT); //LED
//LED should blink when button is pressed
pinMode(buttonPin, INPUT_PULLUP); //button
pinMode(TSV, INPUT);
pinMode(LR_Pin,INPUT);
//to know antenna is at north
attachPinChangeInterrupt(indexIntPin, indexISR, RISING);
Serial.begin(115200);
toggle = digitalRead(4);
startPos = readIntEEPROM(addressBeamPos); // starPos is in degrees
rawOffset = readIntEEPROM(addressOffset); //raw offset position
myEnc.write(startPos * 15);
beamPos = myEnc.read();
sender.sync();
sender.observe(beamPos);
sender.observe(Mode);
sender.observe(LR);
}
void loop()
{
powerFail = digitalRead(5);
digitalWrite(ledPin, HIGH);
Mode = digitalRead(TSV);
LR = digitalRead(LR_Pin); // Long Range Mode
if(rawIndex){
digitalWrite(ledPin, !testBool);
rawIndex = false;
}
if (powerFail == LOW) {
//save position
writeIntEEPROM(addressBeamPos, myEnc.read());
writeIntEEPROM(addressOffset, rawOffset);
powerFail = HIGH;
}
//buttonpress
if (digitalRead(7) == LOW) {
delay(10);
if (digitalRead(7) == LOW) {
isRawOffsetRecorded = false; // Raw offset is offset not divided by 15
myEnc.write(0);
beamPos = 0;
for (int i = 0; i <= 5; i++) {
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
}
}
}
if (isIndex && !isRawOffsetRecorded) {
rawOffset = myEnc.read();
isRawOffsetRecorded = true;
writeIntEEPROM(addressOffset, rawOffset);
digitalWrite(ledPin, HIGH);
isIndex = false;
}
if (isIndex){
myEnc.write(0);
beamPos = 0;
isIndex = false;
}
if ((myEnc.read() / 15) == -1) {
myEnc.write(359 * 15);
//isIndex = false;
}
if ((myEnc.read() / 15) >= 360) {
myEnc.write(0); // Read the encoder while interrupts are enabled.
//isIndex = false;
}
if (toggle == HIGH) {
beamPos = abs((myEnc.read() / 15) - 360); // to rotate in opposite direction
}
if (toggle == LOW) {
beamPos = myEnc.read() / 15;
}
//beamPos=beamPos/15;
sender.sync();
}
void indexISR() {
indexCount++;
rawIndex = true;
if (indexCount == 15) {
isIndex = true;
indexCount = 0;
}
}
int readIntEEPROM(int addressInt) {
int data = EEPROM.readInt(addressInt);
return data;
}
/********************************************************/
void writeIntEEPROM(int addressInt, int data) {
EEPROM.writeInt(addressInt, data);
}