Last active
January 12, 2017 10:16
-
-
Save kinverarity1/9f89eb266676ead39bc43ff5b54bd856 to your computer and use it in GitHub Desktop.
geophysical logger code for Ardunio Uno
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <AltSoftSerial.h> | |
/* | |
Arduino geophysical logger | |
v1 2016-09-30 17:00 | |
Arduino hardware setup: | |
Pin 8 - wire to RS232 Shield RXD pin on left side | |
Pin 9 - wire to RS232 Shield TXD pin on left side | |
Pin 2 - ENC A | |
Pin 3 - ENC B | |
At high speed it seems to miss counts, and interpret them as coming in the wrong direction i.e. out of phase. | |
Columns output to computer (115200 baud RS232 over Arduino USB cable) | |
- depth encoder pulse counting enabled (1=on, 0=off) | |
- current pulses per metre setting (PPM) | |
- current depth | |
- current absolute depth encoder pulse position | |
- change in encoder pulses per interval | |
- time since arduino started in microseconds | |
- time since last interval | |
- speed in encoder pulses per second, showing direction | |
- speed in metres per minute, showing direction | |
- data line received from probe, if end of line has been reached | |
Command lines that can be sent to arduino via USB: | |
dN# - set depth, N is any number including decimal point or negative sign e.g. -0.5 or 3.21 | |
note that it will set the encoder pulse position to zero. | |
pN# - set pulses per metre setting (PPM), n any floating point number as above. | |
c# - start/stop reading encoder pulses | |
i# - flip the direction in which the encoder is reading. This DOES NOT | |
affect the absolute encoder pulse position, only the converted | |
depth | |
*/ | |
// fix bizarre arduino linker bug on XP: | |
int aa; | |
int bb; | |
int cc; | |
int dd; | |
int ee; | |
int ff; | |
int gg; | |
int hh; | |
int ii; | |
int jj; | |
int kk; | |
// end fix | |
// Pin definitions. | |
// - enc_a is ENC Signal A line (Arduino digital pin 2) | |
// - enc_b is ENC Signal B line (Arduino digital pin 3) | |
#define ENC_A 2 | |
#define ENC_B 3 | |
// Main loop refresh period. | |
#define REFRESH_MS 180 | |
// Main serial data connection to computer. | |
#define BAUD_RATE 115200 | |
// RS-232 connection (receive only) | |
#define RS232_BAUD_RATE 2400 | |
// Encoder position | |
int enc_count = 1; // either 1 (count pulses) or 0 (ignore pulses) | |
int enc_polarity = 1; // either 1 or -1 (flip the direction) | |
volatile long enc_pos = 0; | |
int enc_pos_prev = 0; | |
int enc_pos_change = 0; | |
// Speed | |
float sec_change; | |
// Encoder calibration | |
float ppm = 1000; | |
float depth = 0; | |
float depth_change; | |
// Timing | |
unsigned long micros_current = 0; | |
unsigned long micros_prev = 0; | |
long micros_change = 0; | |
// RS-232 data buffer | |
int RS232_buffer[50]; | |
int c = 0; | |
int i = 0; | |
int j = 0; | |
// Computer to Arduino communication buffer | |
String computer_buffer; | |
AltSoftSerial serial_RS232; | |
void setup() | |
{ | |
pinMode(ENC_A, INPUT); | |
pinMode(ENC_B, INPUT); | |
attachInterrupt(0, interrupt_enc_a, RISING); | |
micros_prev = micros(); | |
Serial.begin(BAUD_RATE); | |
serial_RS232.begin(RS232_BAUD_RATE); | |
} | |
void loop() | |
{ | |
// Calculate elapsed time | |
micros_current = micros(); | |
if (micros_current < micros_prev) { | |
micros_change = micros_current + (4294967295 - micros_prev); | |
} else { | |
micros_change = micros_current - micros_prev; | |
} | |
// Calculate change in encoder position. | |
enc_pos_change = (enc_pos - enc_pos_prev);// * enc_polarity * enc_count; | |
depth_change = enc_pos_change / ppm * enc_polarity; | |
depth += depth_change; | |
sec_change = micros_change / 1000000.; | |
// Read data from RS-232 buffer | |
while (serial_RS232.available()) { | |
c = serial_RS232.read(); | |
if (c == 13) { // the other line break character... | |
continue; | |
} else if (c == 10) { // line break | |
break; | |
} else { | |
RS232_buffer[j] = c; | |
if (j < 50) j++; | |
} | |
} | |
// Check for transmission from the computer. | |
while (Serial.available()) { | |
char c2 = Serial.read(); | |
computer_buffer += c2; | |
} | |
// Check to see if we have a complete communication from the computer. | |
computer_buffer.trim(); | |
int n = computer_buffer.length(); | |
if (computer_buffer[n - 1] == '#') { // all commands must end with # | |
if (computer_buffer[0] == 'p') { // Reset PPM value. | |
computer_buffer.remove(0, 1); | |
computer_buffer.remove(n - 1); | |
ppm = computer_buffer.toFloat(); | |
} else if (computer_buffer[0] == 'd') { // set depth | |
computer_buffer.remove(0, 1); | |
computer_buffer.remove(n - 1); | |
depth = computer_buffer.toFloat(); | |
enc_pos = 0; | |
} else if (computer_buffer[0] == 'c') { // Start or stop counting encoder pulses. | |
enc_count = !enc_count; | |
} else if (computer_buffer[0] == 'i') { // flip encoder polarity. | |
if (enc_polarity > 0) { | |
enc_polarity = -1; | |
} else { | |
enc_polarity = 1; | |
} | |
} | |
computer_buffer = ""; | |
} | |
// Send encoder information to computer. | |
Serial.print(enc_count); | |
Serial.print("\t"); | |
Serial.print(ppm); | |
Serial.print("\t"); | |
Serial.print(depth); | |
Serial.print("\t"); | |
Serial.print(enc_pos); | |
Serial.print("\t"); | |
Serial.print(enc_pos_change); | |
Serial.print("\t"); | |
Serial.print(micros_current); | |
Serial.print("\t"); | |
Serial.print(micros_change); | |
Serial.print("\t"); | |
Serial.print(enc_pos_change / sec_change); | |
Serial.print("\t"); | |
Serial.print(depth_change / sec_change); | |
Serial.print("\t"); | |
// Relay the serial data to the computer, if the buffer is full. | |
if (c == 10) { | |
for (i = 0; i < j; i++) { | |
Serial.write(RS232_buffer[i]); | |
} | |
j = 0; | |
} | |
// End of transmission to computer. | |
Serial.print("\n"); | |
// Wait for a specified period (REFRESH_MS). | |
enc_pos_prev = enc_pos; | |
micros_prev = micros_current; | |
delay(REFRESH_MS); | |
} | |
// Detect pulses from depth encoder. | |
void interrupt_enc_a() | |
{ | |
if (enc_count) { | |
if (boolean) digitalRead(ENC_B) { | |
enc_pos++; | |
} else { | |
enc_pos--; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 1000.00 -0.00 -3 0 57668420 182404 0.00 0.00 00128 22222 44444 33333 | |
1 1000.00 -0.00 -3 0 57850964 182544 0.00 0.00 00116 22222 44444 33333 | |
1 1000.00 -0.00 -3 0 58033568 182604 0.00 0.00 00091 22222 44444 33333 | |
1 1000.00 -0.00 -3 0 58216168 182600 0.00 0.00 | |
1 1000.00 -0.00 -3 0 58398584 182416 0.00 0.00 00189 22222 44444 33333 | |
1 1000.00 -0.00 -3 0 58581128 182544 0.00 0.00 00159 22222 44444 33333 | |
1 1000.00 -0.00 -3 0 58763728 182600 0.00 0.00 00221 22222 44444 33333 | |
1 1000.00 -0.00 -3 0 58946332 182604 0.00 0.00 | |
1 1000.00 -0.00 -3 0 59128724 182392 0.00 0.00 00228 22222 44444 33333 | |
1 1000.00 -0.00 -3 0 59311284 182560 0.00 0.00 00293 22222 44444 33333 | |
1 1000.00 -0.00 -3 0 59493896 182612 0.00 0.00 | |
1 1000.00 -0.00 -3 0 59676336 182440 0.00 0.00 00436 22222 44444 33333 | |
1 1000.00 -0.00 -3 0 59858848 182512 0.00 0.00 00761 22222 44444 33333 | |
1 1000.00 -0.00 -3 0 60041460 182612 0.00 0.00 01080 22222 44444 33333 | |
1 1000.00 -0.00 -3 0 60224068 182608 0.00 0.00 | |
1 1000.00 -0.00 -3 0 60406508 182440 0.00 0.00 01372 22222 44444 33333 | |
1 1000.00 -0.00 -3 0 60589024 182516 0.00 0.00 01718 22222 44444 33333 | |
1 1000.00 -0.00 -3 0 60771636 182612 0.00 0.00 01832 22222 44444 33333 | |
1 1000.00 -0.00 -3 0 60954244 182608 0.00 0.00 | |
1 1000.00 -0.00 -3 0 61136712 182468 0.00 0.00 02052 22222 44444 33333 | |
1 1000.00 -0.00 -3 0 61319248 182536 0.00 0.00 01956 22222 44444 33333 | |
1 1000.00 -0.00 -3 0 61501856 182608 0.00 0.00 01948 22222 44444 33333 | |
1 1000.00 -0.00 -3 0 61684456 182600 0.00 0.00 | |
1 1000.00 -0.00 -3 0 61866924 182468 0.00 0.00 01639 22222 44444 33333 | |
1 1000.00 -0.00 -3 0 62049464 182540 0.00 0.00 01475 22222 44444 33333 | |
1 1000.00 -0.00 -3 0 62232064 182600 0.00 0.00 00908 22222 44444 33333 | |
1 1000.00 -0.00 -3 0 62414676 182612 0.00 0.00 | |
1 1000.00 -0.00 -3 0 62597136 182460 0.00 0.00 00551 22222 44444 33333 | |
1 1000.00 -0.00 -3 0 62779672 182536 0.00 0.00 00387 22222 44444 33333 | |
1 1000.00 -0.00 -3 0 62962276 182604 0.00 0.00 00267 22222 44444 33333 | |
1 1000.00 -0.00 -3 0 63144888 182612 0.00 0.00 | |
1 1000.00 -0.00 -3 0 63327340 182452 0.00 0.00 00143 22222 44444 33333 | |
1 1000.00 -0.00 -3 0 63509868 182528 0.00 0.00 00105 22222 44444 33333 | |
1 1000.00 -0.00 -3 0 63692472 182604 0.00 0.00 00078 22222 44444 33333 | |
1 1000.00 -0.00 -3 0 63875084 182612 0.00 0.00 | |
1 1000.00 -0.00 -3 0 64057524 182440 0.00 0.00 00093 22222 44444 33333 | |
1 1000.00 -0.00 -3 0 64240060 182536 0.00 0.00 00094 22222 44444 33333 | |
1 1000.00 -0.00 -3 0 64422668 182608 0.00 0.00 00064 22222 44444 33333 | |
1 1000.00 -0.00 -3 0 64605276 182608 0.00 0.00 | |
1 1000.00 -0.00 -3 0 64787700 182424 0.00 0.00 00077 22222 44444 33333 | |
1 1000.00 -0.00 -3 0 64970236 182536 0.00 0.00 00085 22222 44444 33333 | |
1 1000.00 -0.00 -3 0 65152848 182612 0.00 0.00 00116 22222 44444 33333 | |
1 1000.00 -0.00 -3 0 65335460 182612 0.00 0.00 | |
1 1000.00 -0.00 -3 0 65517872 182412 0.00 0.00 00119 22222 44444 33333 | |
1 1000.00 -0.00 -3 0 65700404 182532 0.00 0.00 00086 22222 44444 33333 | |
1 1000.00 -0.00 -3 0 65883016 182612 0.00 0.00 00100 22222 44444 33333 | |
1 1000.00 -0.00 -3 0 66065628 182612 0.00 0.00 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment