/* ((C))PA3BNX SuperSimpleArduinoDoppler/RDF Trying to create a SuperSimpleArduinoDoppler on Arduino Uno R3 Arduino Uno REV 3 Sketch + (With dopplertone amplitude averaged) Look at http://www.pi4wag.nl/projecten/doppler-peiler-radio-direction-finder ======================================================================================== Why because there are so many laptops with just mono mic inputs This can eliminate the external USB stereo input Soundcard with MyMapping.exe software ======================================================================================== For this there is a special Arduino Com/USB input in program MyMapping to show the Agrelo(%bbb) headings on maps No adjustments on the arduino shield it self except Referency audio output for soundcard. All adjustments like Frequency, CW/CCW and Calibrate-offset through Serial Port Doppler frequency 500-700-900 Hz Antenna test 1,2,3,4 Wait 3 seconds then restart scanning Remote CMD's ANT1 ANT2 ANT3 ANT4 OFF CW CCW LOW MID HIGH (bbb Calibrate) AV AV does switch averaging off/on but it does always start up in Average On mode In the Arduino IDE Serial monitor you can input cmd's easy with a z like ANT1z, CCWz, 335z or AVz @12-04-2015 @08-06-2015 @26-06-2015 Bug fixed CW/CCW @01-07-2015 Average bug fixed? @22-07-2015 Doppler tone Amplitude &ddd [T] @27-07-2015 CW/CCW for AM plate antenna @02-08-2015 MultiPath bug Fixed Not quite ready yet but working! Arrays are zero based !!!! Explaining functions Arduino pins: antPins[4] Connect to antenna switcher pins refpa3bnx Connect to audio lowpass filter for soundcard referency input kPins[4] Connect to kwadrant C's 4u7 kPinAnalog Connect to sum point of 4 cap's max232Pin[2] for Am Plate Whatson Watt DF through max232 external chip + and - 12 V It stores Calibrate, Freq, CW/CCW and EEPROM ! frequency <> startTimer values 500 131 700 167 900 187 1200 203 1500 214 2000 225 2500 231 Byte= 0>255 Int= -32768>32767 Int = 2 bytes */ #include //EEPROM Adresses //Max 512 Adress bytes const byte ID=100; //Eeprom ID always on adress 0 const int cFreq=1; //1 byte const int cCW=2; //1 byte const int cCal=3; //2 bytes //String Serial params String inputString=""; //Received serial string //Boolean boolean bStringComplete=false; //Complete '\n' rxed serial string volatile boolean bNoAntScan=false; //Do not change antPins while testing one antenna boolean bAverage = true; //Default Average boolean bMultiPathAverage=false; //Multipath heading found in received headings from Averaged //Const Integer //Must be modified to right pin number arduino and arduino shield hardware //Antenna Control Pins & refpa3bnx //Never use pins 0 and 1 because the UART must also function //So I started with pin nbr 2 byte antPin[4]={2,3,4,5}; //Set antenna control pins (Not const because it can be CCW/CW) const byte max232Pin[2]={7,8}; //Set Max232Pin vor AM plate antenna const byte refpa3bnx=6; //Pin for Referency frequency output to soundcard //Integer byte mLOW=0; //Reverse max232Pins for CW/CCW AM plate antenna byte mHIGH=1; //Kwadrant Capicity switches & analog SCF read pin const byte kPin[4]={9,10,11,12}; //4 pins caps 4u7 const byte kPinAnalog=0; //A0 read voltage across one 4u7 cap pin A0 to A5 const int cOffset=511; // .5 * 1023 caps offset //Squelch Signals const byte cSqlclosed=0; const byte cSqlclosed2open=1; const byte cSqlOpenflutter=2; const byte cSqlopen2closed=3; const byte cSqloverflow=4; //Capicitors are full //NoDegreesMaxMinValues const int cNodegrees=999; //Squelch No Signal or cSqlopen2closed Clear avDegrees and set avDegrees to cNodegrees const int cReset=888; //Clear Average and and put last avDegrees back in Average const int cMaxTime=12; //15 Sec/cMainLoopDelay Max time 60 * .250 = 15 Sec 30 * .5 Sec =15 sec 75 * .200 = 15 sec 12 * .250 = 3 Sec const int cMainLoopDelay=250; //How fast the main loop runs in mSec 500mSec const int cMinAudioAmplitude=15; //There is enough signal to SinCosDetect const int cMaxSymmetric=175; //Max Sym in MultiPathSCF const byte cMaxFlutterCount=3; //Max keep loop rounds open after sql closing //Function Squelch Range 1 to 5 orso ? //Float const const float rad1=180/PI; //57.295827 //Integer //Array with the 4 kwandrant analog cap tensions These can be -511 to +511 byte StartTimer=131; //500 Hz by default // 131, 167 and 187 500, 700 and 900 Hz byte sqlstatus=cSqlclosed; int Corg[4]={0,0,0,0}; //Direct cap values filled in ISR int Cap[4]={0,0,0,0}; //+ and - values from analog read -511>0>511 int Calibrate=0; //Calibrate degrees default 0-360 //------------ISR Main Interrupt //This runs always-------------------- ISR(TIMER2_OVF_vect) { TCNT2=StartTimer; //Preload timer every time the ISR runs //Set the pins Todo further volatile static int c; if (bNoAntScan==false){ if (c==4){ c=0; } //All switching Pin Settings getting here Corg[c]=analogRead(kPinAnalog)-cOffset; switch (c){ case 0: digitalWrite(antPin[0],HIGH); digitalWrite(antPin[3],LOW); digitalWrite(kPin[0],LOW); digitalWrite(kPin[3],HIGH); digitalWrite(refpa3bnx,HIGH); digitalWrite(max232Pin[0],mLOW); digitalWrite(max232Pin[1],mLOW); break; case 1: digitalWrite(antPin[1],HIGH); digitalWrite(antPin[0],LOW); digitalWrite(kPin[1],LOW); digitalWrite(kPin[0],HIGH); digitalWrite(max232Pin[0],mHIGH); digitalWrite(max232Pin[1],mLOW); break; case 2: digitalWrite(antPin[2],HIGH); digitalWrite(antPin[1],LOW); digitalWrite(kPin[2],LOW); digitalWrite(kPin[1],HIGH); digitalWrite(refpa3bnx,LOW); digitalWrite(max232Pin[0],mLOW); digitalWrite(max232Pin[1],mHIGH); break; case 3: digitalWrite(antPin[3],HIGH); digitalWrite(antPin[2],LOW); digitalWrite(kPin[3],LOW); digitalWrite(kPin[2],HIGH); digitalWrite(max232Pin[0],mHIGH); digitalWrite(max232Pin[1],mHIGH); break; } c++; } } //Last bracket ISR-OVF //Set ISR StartTimer void ISRset(int f) { //Hint How to calc StartTime value //Arduino doesn't like float calcs with small numbers hi //t=1/freq //x=256/16000000 //StartTime = 256-(t/x) //---------------------- //Standard values 500, 700 and 900 Hz // 131, 167 and 187 switch (f){ case 500: StartTimer=131; break; case 700: StartTimer=167; break; case 900: StartTimer=187; break; } EEPROM.write(cFreq,StartTimer); Serial.print(f); Serial.println(F(" Hz")); } void SetAllPins(){ }//Last Bracket SetAllPins //---------Functions now------------------- //Listen/Test on each antenna separate for about 3 sec's void AntTest(byte a){ bNoAntScan=true; byte i; //Switch all Low for (i=0;i<4;i++) { digitalWrite(antPin[i],LOW); } if (a==255) { //Leave all Ant Low Serial.println(F("Start All Ant Low")); } else if (a<4) { //Switch one Doppler antenna High Serial.print(F("Start Ant Test ")); Serial.println(a+1); digitalWrite(antPin[a],HIGH); } //Plate antenna direction switch(a) { case 0: digitalWrite(max232Pin[0],mLOW); digitalWrite(max232Pin[1],mLOW); break; case 1: digitalWrite(max232Pin[0],mHIGH); digitalWrite(max232Pin[1],mLOW); break; case 2: digitalWrite(max232Pin[0],mLOW); digitalWrite(max232Pin[1],mHIGH); break; case 3: digitalWrite(max232Pin[0],mHIGH); digitalWrite(max232Pin[1],mHIGH); break; } delay(3000); //3 Seconds //Switch doppler antenna again to Low if (a<4) { digitalWrite(antPin[a],LOW); } bNoAntScan=false; } //---------------Empty the 4 capicitors by discharging to 1/2 U------ //This function leaves all kPin[] = HIGH void EmptyCaps(){ byte i; for (i=0;i<4;i++) { digitalWrite(kPin[i],LOW); } delay(1000); for (i=0;i<4;i++) { digitalWrite(kPin[i],HIGH); } } //-------------Squelch----------------------- byte Squelch(){ //Return the sqlstatus //Try Find a full cap static byte flutterCount; int x=0; //Max int x1=0; //Min int z=0; //Find cap with highest voltage/tension top-top value //Biggest value = cOffset (511) for(byte i=0;i<4;i++) { z=(Cap[i]); if (z>x) { x=z;//Max } if (z=cOffset) { return cSqloverflow; //Caps are full so need discharged } else if (x>=cMinAudioAmplitude) { flutterCount=0; //Reset AverageTone(x);//Average Doppler Amplitude return cSqlclosed2open; //Squelch Open } else if (xcMaxSymmetric){ return true; } else { return false; } } //Calibrate So add Calibrate offset to degrees int Cali(int d){ return LimitDegrees360(Calibrate+d); } //Return Agrelo formatted string % and 3 digits always String Format3Degrees(int d){ String str; int digits[3]; int reminder; digits[0]=d/100; reminder=d%100; digits[1]=reminder/10; reminder=reminder%10; digits[2]=reminder; str+='%'; str+=digits[0]; str+=digits[1]; str+=digits[2]; return str; } //Limit Degrees 0 to 360 degrees int LimitDegrees360(int d){ //Limit degrees 0 to 360 here //If 999 then return 999 if (d==cNodegrees) { return d; } else { int x; x = d%360; if (x<0) { x+=360; return x; } } } //--Average int Average(int d){ //If d = 999 (cNoDegrees) then reset average and return last avDegrees witch is 999 //If d = 888 (cReset) then Reset all and put last avDegrees back in arrays //Always return avDegrees and Set bMultiPath //Integer static int sum[4]={0,0,0,0}; //Sum of counts for each kwadrant static int c[4]={0,0,0,0}; //Cap tension/voltage in each kwadrant static int avDegrees=cNodegrees; //Averaged degrees int xx[4]={0,0,0,0}; //Adjacent kwadrants counts int z=0; //Most adjacent headings int z1=0; //Other two kwandants headings Multipath int y=0; //Index where the most headings are //Reset/clear and return cNodegrees or last avDegrees //01-06-2015 No Averaging if (bAverage==false){return d;} switch (d){ case cNodegrees: //Reset and Clear for (byte i=0;i<4;i++) { sum[i]=0; c[i]=0; } bMultiPathAverage = false; avDegrees=cNodegrees; return avDegrees; //Exit now case cReset: //Reset and fill with avDegrees //Reset for (byte i=0;i<4;i++) { sum[i]=0; c[i]=0; } //Fill with last avDegrees d=avDegrees;//Put last degrees in again } if (d>=0 && d<90){ sum[0]+=d; c[0]++; } if (d>=90 && d<180){ sum[1]+=d; c[1]++; } if (d>=180 && d<270){ sum[2]+=d; c[2]++; } if (d>=270 && d<=360){ sum[3]+=d; c[3]++; } //No degrees found so exit function if (c[0]+c[1]+c[2]+c[3]==0) { bMultiPathAverage=false; avDegrees=cNodegrees; return avDegrees; } //Find 2 adjacent kwadrants with hold most heading counts //Count all adjacents kwadrants xx[0]=c[0]+c[1]; xx[1]=c[1]+c[2]; xx[2]=c[2]+c[3]; xx[3]=c[3]+c[0]; //Find out in witch two kwadrants the most headings are for (byte i=0;i<4;i++){ if (xx[i]>z){ z=xx[i]; //Most adjacent Headings counts y=i; //Witch 2 adjacent Array Index from xx[1] } } //Check for multipath kwadrants switch(z){ case 0: z1=2; break; case 1: z1=3; break; case 2: z1=0; break; case 3: z1=1; break; } if (xx[z]==xx[z1]) { bMultiPathAverage=true; } else { bMultiPathAverage=false; } //Average the 2 most counts kwadrants switch(y){ case 0: avDegrees=(sum[0]+sum[1])/xx[y]; break; case 1: avDegrees=(sum[1]+sum[2])/xx[y]; break; case 2: avDegrees=(sum[2]+sum[3])/xx[y]; break; case 3: if (c[3]==0 && c[1]>0) { avDegrees=sum[1]/c[1]; } else if (c[1]==0 && c[3]>0) { avDegrees=sum[3]/c[3]; } else if (c[3]>0 && c[1]>0) { avDegrees=LimitDegrees360(((sum[3]/c[3])+((sum[0]/c[0])+360)/2)); } break; } return avDegrees; } //Last Bracket Average //--------------ToDo-----Doppler tone Amplitude--23-07-2015 //---Called from Fn Squelch and Sub Loop int AverageTone(int d) { static int AvTone=cNodegrees; static int sum=0; static int count=0; if (d==cNodegrees) { AvTone=0; sum=0; count=0; return 0; } else if (d==cReset) { sum=AvTone; count=1; return AvTone; } sum +=d; count +=1; AvTone=sum/count; return AvTone; }//---------- //--SinCosDetector Find the degrees from the 4 Cap[] tensions int SinCosDetector(){ int SinSum; int CosSum; int effe; //In Cap[] is the tension value of 1 of the 4 C'S SinSum=Cap[0]-Cap[2]; CosSum=Cap[1]-Cap[3]; if (SinSum==0 && CosSum==0){ return cNodegrees;//No Signal } else { //Serial.println(atan2(SinSum,CosSum)*rad1); //Added 90 degrees so it's the same as zerocrossdetection return LimitDegrees360((atan2(SinSum,CosSum)*rad1)+90); } } //=============SerialRx Event=========================== void serialEvent() { while (Serial.available()) { //get the new byte: char inChar=(char)Serial.read(); // if the incoming character is a newline, set a flag // so the main loop can do something about it: if (inChar=='z'||inChar=='\r'||inChar=='\n'||inputString.length() > 4 ){ bStringComplete=true; } else { //add it to the inputString: inputString+=inChar; } } } //======================================================= //========================================= boolean CheckOnlyNbr(String str){ //Check for only numbers 0 to 9 and -(asc 45) in the string //48=0 57=9 for (unsigned j=0;j57)) { return false; } } return true; } //======================================== //------------------------------------Main Functions----------------------------------------- //SetUp void setup() { Serial.begin(9600); // Opens serial port, sets data rate to 9600 bps Serial.println(F("((C))PA3BNX Arduino RDF")); pinMode(refpa3bnx,OUTPUT); digitalWrite(refpa3bnx,0); pinMode(max232Pin[0],OUTPUT); pinMode(max232Pin[1],OUTPUT); digitalWrite(max232Pin[0],LOW); digitalWrite(max232Pin[1],LOW); for (byte i=0;i<4;i++) { pinMode(antPin[i],OUTPUT); pinMode(kPin[i] ,OUTPUT); digitalWrite(antPin[i],LOW); //antPin LOW = OFF //digitalWrite(kPin[i],LOW); //Set Cap pins to low so the can charge to 1/2U } EmptyCaps(); //1 sec charge to 1/2 U //EEprom values: StartTimer, CW and Calibrate if (EEPROM.read(0)==ID) { StartTimer=EEPROM.read(cFreq); if( EEPROM.read(cCW)>0) { byte swap; swap=antPin[3]; antPin[3]=antPin[1]; antPin[1]=swap; mLOW=1; mHIGH=0; } byte hi=EEPROM.read(cCal); byte lo=EEPROM.read(cCal+1); Calibrate=word(hi,lo); if (Calibrate<0 || Calibrate>360){Calibrate=0;} } else { //Default EEPROM now EEPROM.write(0,ID); EEPROM.write(cFreq,StartTimer); EEPROM.write(cCW,0); EEPROM.write(cCal,0); EEPROM.write(cCal+1,0); } //Info on Screen Serial.print(F("Calibrate=")); Serial.println(Calibrate); switch (StartTimer){ case 131: Serial.print(F("500")); break; case 167: Serial.print(F("700")); break; case 187: Serial.print(F("900")); } Serial.println(F(" Hz")); if (EEPROM.read(cCW)==0){ Serial.println(F("CW")); } else { Serial.println(F("CCW")); } //End Info on Screen Serial.println(F("%999")); //Squelch always closed at start just before starting the loop //Initial Setup Timer2 noInterrupts(); TCCR2A=0; TCCR2B=0; TCNT2 = StartTimer; //Begin value in timer count register TCCR2B |= (1< 0x00 interrupts(); } //Main Loop void loop(){ // ToDo // GetCapValues // Squelch status // OverFlow Reset the 4 kwadrant capicitors EmptyCaps // Calibrate routine (add an degrees offset) // SinCosDetector // Send (averaged) and calibrated degrees out through the serialport // Receive remote serial cmd's like CW/CCW ANT1,2,3,4 CW CCW LOW MID HIGH and Degrees //Local variables boolean bSCF; //MuliPath SCF byte swap; int x1; //SinCosDegrees 0-360 static int x2=cNodegrees; //Averaged Degrees can be 999 static int x3=cNodegrees; //Keep old degrees value so reduce Serial output static int tMaxTime=0; //Max Average count before reset noInterrupts(); //GetCapValues so the are not changed from here Cap[0]=Corg[3]; Cap[1]=Corg[2]; Cap[2]=Corg[1]; Cap[3]=Corg[0]; interrupts(); sqlstatus=Squelch(); //Squelch status check uses Cap[] values //Now Just Serial event and delay if (sqlstatus==cSqlOpenflutter) { //Serial.print("Flutter"); tMaxTime+=1; goto further; } if (sqlstatus==cSqlclosed) { tMaxTime=0; } else { tMaxTime+=1; //Total loops to compared against cMaxTime } if (sqlstatus==cSqloverflow) { bNoAntScan=true; EmptyCaps();//Leaves all kPins HIGH bNoAntScan=false; x3=cNodegrees; } else { //Relaxed sending degrees to serialport and Pelorus //static byte x3 = cNodegrees; //Keep old value so reduce serial and Pelorus output //Never add a MultiPath SCF value to Average //Get Average degrees if (sqlstatus==cSqlclosed2open) { bSCF=MultiPathDetectSCF(); if (bSCF==false) { x1=SinCosDetector(); x2=Average(x1); //x2=x1; } } //Squelch closed if (sqlstatus==cSqlopen2closed) { //Always Squelch Closed and Reset Average if (x3!=cNodegrees) { Serial.println(Format3Degrees(cNodegrees)); //Squelch Closed %999 Average(cNodegrees); //Reset and set avDegrees to cNodegrees AverageTone(cNodegrees);//Reset doppler tone amplitude } tMaxTime=0; x3=cNodegrees; } //Squelch open else if (sqlstatus==cSqlclosed2open) { //Show actuel degrees from average if (bMultiPathAverage==false) { //Never show twice the same average degrees if (x3!=x2) { //Never show cNodegrees here if (x2!=cNodegrees){ Serial.print(Format3Degrees(Cali(x2)));//Show actuel degrees from average Serial.print(F(" [")); Serial.print(AverageTone(cReset));//Show actuel Doppler tone amplitude Serial.println(F("]")); } } x3=x2;//Backup old degrees value } } } //Last Bracket Degrees further: //Try to read Calibration, CW/CCW, AntTest, Timing(LMH), Calibrate Parameters //000 from MyMapping.exe as calibrate cmd serialEvent(); //Call SerialEvent once in a while if (bStringComplete==true){ //Swap 90 and 270 degrees pins if (inputString=="CW" ) { Serial.println(inputString); if (EEPROM.read(cCW)==1) { swap=antPin[1]; antPin[1]=antPin[3]; antPin[3]=swap; mLOW=1; mHIGH=0; //How switch max232pins? EEPROM.write(cCW,0); Average(cNodegrees); tMaxTime=0; } } else if (inputString=="CCW" ) { Serial.println(inputString); if (EEPROM.read(cCW)==0) { swap=antPin[3]; antPin[3]=antPin[1]; antPin[1]=swap; mLOW=0; mHIGH=1; //How switch max232pins? EEPROM.write(cCW,1); Average(cNodegrees); tMaxTime=0; } } else if (inputString=="ANT1") { AntTest(0); } else if (inputString=="ANT2") { AntTest(1); } else if (inputString=="ANT3") { AntTest(2); } else if (inputString=="ANT4") { AntTest(3); } else if (inputString=="OFF") { AntTest(255); } else if (inputString=="LOW") { ISRset(500); //ToDo ISR 500 Hz } else if(inputString=="MID") { ISRset(700); //ToDo ISR 700 Hz } else if (inputString=="HIGH") { ISRset(900); //ToDo ISR 900 Hz } //Test input Averaging on/off else if (inputString=="AV") { if (bAverage==false) {Serial.println(F("Average")); bAverage=true; Average(cNodegrees); } else { Serial.println(F("No Average")); bAverage=false; bMultiPathAverage=false; } } else //Now just numbers { //Try Calibrate offset if (CheckOnlyNbr(inputString)==true) { //------------------ int x; x=inputString.toInt(); if (x>=0 && x<=360) { Calibrate=x; x3=cNodegrees; //Next round always output degrees byte hi = highByte(Calibrate); byte lo = lowByte(Calibrate); EEPROM.write(cCal,hi); EEPROM.write(cCal+1,lo); Serial.print(F("Calibrate=")); Serial.print(Calibrate); //Also show the CW/CCW 21-07-2015 if (EEPROM.read(cCW)==0){ Serial.println(F(" CW"));} else{ Serial.println(F(" CCW"));} //------------------ } } } //Now clear inputString because all done inputString=""; bStringComplete=false; } //Do always every loop //Reset if(tMaxTime>=cMaxTime) { tMaxTime=0; if (sqlstatus==cSqlclosed2open) { Average(cReset); //Clear and Reset avDegrees with last value AverageTone(cReset);//Clear and Reset Doppler Amplitude Elevation } } //Wait 250 a 1000 mSec for next try ? fast enough I think delay(cMainLoopDelay); } //End of sketch Last bracket of Loop