From 80458e3493b4dbb666862160f3dca3ce61bf76c9 Mon Sep 17 00:00:00 2001 From: Saima Firdaus Date: Sun, 19 Nov 2023 20:07:04 +1100 Subject: [PATCH 1/2] adding optical rpm wheel speedometer file --- optical_rpm_log.ino | 129 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 optical_rpm_log.ino diff --git a/optical_rpm_log.ino b/optical_rpm_log.ino new file mode 100644 index 0000000..fa45bf3 --- /dev/null +++ b/optical_rpm_log.ino @@ -0,0 +1,129 @@ +float REV = 0; +int RPM_VALUE; +int PREVIOUS = 0; +int TIME; + +void INTERRUPT() +{ + REV++; +} + +void setup() +{ + Serial.begin(9600); + attachInterrupt(3, INTERRUPT, RISING); +} + +void loop() +{ + delay(1000); + detachInterrupt(0); + TIME = millis() - PREVIOUS; + RPM_VALUE = (REV/TIME) * 60000; + PREVIOUS = millis(); + REV = 0; + Serial.println(RPM_VALUE); + attachInterrupt(1, INTERRUPT, RISING); +} +/** + * Wheel speed rpm logging using the V2 DAS + changed few things in orginal wheel-rpm-logging code + */ +// +//// Commas or spaces? +//#define SEPARATOR "," +//// #define SEPARATOR " " +// +//// Pins +//#define REED_SWITCH 3 +//#define LED_EXTERNAL 13 +// +//#define MIN_TIME 20000 // Minimum rotation time (us) for debouncing purposes +//#define BAUD_RATE 115200 +//#define RPM_CONVERSION_FACTOR 60 // 60 seconds in a minute +//#define AV_CONVERSION_FACTOR TWO_PI +//#define PRINT_DECIMALS 15 +// +//volatile unsigned int rotationFlag = false; +//volatile uint32_t curTime = 0; +//volatile uint32_t prevTime = 0; +//volatile unsigned long rotations = 0; +//double prevAV = 0; +// +//#define RESET() +// +//void setup() { +// // Hardware +// pinMode(REED_SWITCH, INPUT); + +// +// // Serial +// Serial.begin(BAUD_RATE); +// while(!Serial) { +//// // Let the user know the computer is not connected correctly +//// digitalWrite(LED_EXTERNAL, LOW); +//// delay(100); +//// digitalWrite(LED_EXTERNAL, HIGH); +//// delay(100); +//// } +// // Serial.println(F("MHP Wheel speed logging. Compiled" __TIME__ "," __DATE__)); +// Serial.println("Time_us" SEPARATOR "Rotation_Number" SEPARATOR "Rotation_Time_us" SEPARATOR "Angular_V_rad_s" SEPARATOR "Angular_A_rad_s2" SEPARATOR "RPM"); +// +// // Enable the switch +// attachInterrupt(REED_SWITCH, reedInterrupt, RISING); +//} +// +//void loop() { +// // Do the time consuming stuff outside the ISR +// if(rotationFlag) { +// // Disable interrupts whilst reading to avoid race conditions. +// noInterrupts(); +// unsigned long rotationTime = curTime - prevTime; +// unsigned long rotationsCopy = rotations; // To avoid race conditions +// unsigned long curTimeCopy = curTime; +// interrupts(); +// +// double rotationTimeSeconds = (double)rotationTime / 1e6; +// double rpm = RPM_CONVERSION_FACTOR / rotationTimeSeconds; +// double angularV = AV_CONVERSION_FACTOR / rotationTimeSeconds; +// double angularA = (angularV - prevAV) / rotationTimeSeconds; +// prevAV = angularV; +// +// Serial.print(curTimeCopy); +// Serial.write(SEPARATOR); +// Serial.print(rotationsCopy); +// Serial.write(SEPARATOR); +// Serial.print(rotationTime); +// Serial.write(SEPARATOR); +// Serial.print(angularV, PRINT_DECIMALS); +// Serial.write(SEPARATOR); +// Serial.print(angularA, PRINT_DECIMALS); +// Serial.write(SEPARATOR); +// Serial.println(rpm, PRINT_DECIMALS); +// rotationFlag = false; +// } +// +// if(Serial.available() && Serial.read() == 'r') { +// // Reset if 'r' is sent +// // Serial.println(F("'r' received, so resetting...")); // Don't pollute the new file with this message +// Serial.flush(); ///??????? +// delay(200); +// RESET(); +// } +//} +// +///** +// * Interrupt for the reed switch being triggered. +// */ +//void reedInterrupt() { +// uint32_t now = micros(); +// if(now - curTime >= MIN_TIME) { +// // Switch closed and no event interrupts for the last little while +// // Assume this is a genuine wheel rotation and not switch bounce. +//// digitalWrite(LED_EXTERNAL, rotations & 0x1); // Toggle the led (lsb of rotations toggles each time). <-- ??? +// rotations++; +// prevTime = curTime; +// curTime = now; +// rotationFlag = true; +// } +//} \ No newline at end of file From dca0686ce50dba22a7b5e023326f8ca9cbcabaf6 Mon Sep 17 00:00:00 2001 From: saima-firdaus <140468997+saima-firdaus@users.noreply.github.com> Date: Tue, 16 Apr 2024 11:34:26 +0300 Subject: [PATCH 2/2] Update optical_rpm_log.ino updating the rpm tachometer arduino file --- optical_rpm_log.ino | 201 +++++++++++++++++--------------------------- 1 file changed, 78 insertions(+), 123 deletions(-) diff --git a/optical_rpm_log.ino b/optical_rpm_log.ino index fa45bf3..4b28f78 100644 --- a/optical_rpm_log.ino +++ b/optical_rpm_log.ino @@ -1,129 +1,84 @@ -float REV = 0; -int RPM_VALUE; -int PREVIOUS = 0; -int TIME; +#define SEPARATOR "," -void INTERRUPT() -{ - REV++; -} +//Pins +#define TCRT_DIG 2 +#define TCRT_ANLG A0 + +// +volatile int digital_val; +int counter = 0; +volatile unsigned prevRPM = 0; -void setup() -{ - Serial.begin(9600); - attachInterrupt(3, INTERRUPT, RISING); +#define MIN_TIME 50000 +#define BAUD_RATE 115200 +#define RPM_CONVERSION_FACTOR 60 +#define AV_CONVERSION_FACTOR TWO_PI +#define PRINT_DECIMALS 15 + +volatile unsigned int rotationFlag = false; +volatile uint32_t curTime = 0; +volatile uint32_t prevTime = 0; +volatile unsigned long rotations = 0; +double prevAV = 0; + +void irInterrupt(){ + uint32_t now = micros(); + if(now - curTime >= MIN_TIME) { + // Switch closed and no event interrupts for the last little while + // Assume this is a genuine wheel rotation and not switch bounce. + rotations++; + prevTime = curTime; + curTime = now; + rotationFlag = true; + } } -void loop() -{ - delay(1000); - detachInterrupt(0); - TIME = millis() - PREVIOUS; - RPM_VALUE = (REV/TIME) * 60000; - PREVIOUS = millis(); - REV = 0; - Serial.println(RPM_VALUE); - attachInterrupt(1, INTERRUPT, RISING); +void setup() { + Serial.begin(BAUD_RATE); + pinMode(TCRT_DIG, INPUT_PULLUP); + pinMode(TCRT_ANLG, INPUT); + // Serial.println(F("MHP Wheel speed logging. Compiled" __TIME__ "," __DATE__)); + Serial.println("Time_us" SEPARATOR "Rotation_Number" SEPARATOR "Rotation_Time_us" SEPARATOR "Angular_V_rad_s" SEPARATOR "Angular_A_rad_s2" SEPARATOR "RPM"); + + // Enable the switch + attachInterrupt(digitalPinToInterrupt(TCRT_DIG),irInterrupt, FALLING); } -/** - * Wheel speed rpm logging using the V2 DAS - changed few things in orginal wheel-rpm-logging code - */ -// -//// Commas or spaces? -//#define SEPARATOR "," -//// #define SEPARATOR " " -// -//// Pins -//#define REED_SWITCH 3 -//#define LED_EXTERNAL 13 -// -//#define MIN_TIME 20000 // Minimum rotation time (us) for debouncing purposes -//#define BAUD_RATE 115200 -//#define RPM_CONVERSION_FACTOR 60 // 60 seconds in a minute -//#define AV_CONVERSION_FACTOR TWO_PI -//#define PRINT_DECIMALS 15 -// -//volatile unsigned int rotationFlag = false; -//volatile uint32_t curTime = 0; -//volatile uint32_t prevTime = 0; -//volatile unsigned long rotations = 0; -//double prevAV = 0; -// -//#define RESET() -// -//void setup() { -// // Hardware -// pinMode(REED_SWITCH, INPUT); -// -// // Serial -// Serial.begin(BAUD_RATE); -// while(!Serial) { -//// // Let the user know the computer is not connected correctly -//// digitalWrite(LED_EXTERNAL, LOW); -//// delay(100); -//// digitalWrite(LED_EXTERNAL, HIGH); -//// delay(100); -//// } -// // Serial.println(F("MHP Wheel speed logging. Compiled" __TIME__ "," __DATE__)); -// Serial.println("Time_us" SEPARATOR "Rotation_Number" SEPARATOR "Rotation_Time_us" SEPARATOR "Angular_V_rad_s" SEPARATOR "Angular_A_rad_s2" SEPARATOR "RPM"); -// -// // Enable the switch -// attachInterrupt(REED_SWITCH, reedInterrupt, RISING); -//} -// -//void loop() { -// // Do the time consuming stuff outside the ISR -// if(rotationFlag) { -// // Disable interrupts whilst reading to avoid race conditions. -// noInterrupts(); -// unsigned long rotationTime = curTime - prevTime; -// unsigned long rotationsCopy = rotations; // To avoid race conditions -// unsigned long curTimeCopy = curTime; -// interrupts(); -// -// double rotationTimeSeconds = (double)rotationTime / 1e6; -// double rpm = RPM_CONVERSION_FACTOR / rotationTimeSeconds; -// double angularV = AV_CONVERSION_FACTOR / rotationTimeSeconds; -// double angularA = (angularV - prevAV) / rotationTimeSeconds; -// prevAV = angularV; -// -// Serial.print(curTimeCopy); -// Serial.write(SEPARATOR); -// Serial.print(rotationsCopy); -// Serial.write(SEPARATOR); -// Serial.print(rotationTime); -// Serial.write(SEPARATOR); -// Serial.print(angularV, PRINT_DECIMALS); -// Serial.write(SEPARATOR); -// Serial.print(angularA, PRINT_DECIMALS); -// Serial.write(SEPARATOR); -// Serial.println(rpm, PRINT_DECIMALS); -// rotationFlag = false; -// } -// -// if(Serial.available() && Serial.read() == 'r') { -// // Reset if 'r' is sent -// // Serial.println(F("'r' received, so resetting...")); // Don't pollute the new file with this message -// Serial.flush(); ///??????? -// delay(200); -// RESET(); -// } -//} -// -///** -// * Interrupt for the reed switch being triggered. -// */ -//void reedInterrupt() { -// uint32_t now = micros(); -// if(now - curTime >= MIN_TIME) { -// // Switch closed and no event interrupts for the last little while -// // Assume this is a genuine wheel rotation and not switch bounce. -//// digitalWrite(LED_EXTERNAL, rotations & 0x1); // Toggle the led (lsb of rotations toggles each time). <-- ??? -// rotations++; -// prevTime = curTime; -// curTime = now; -// rotationFlag = true; -// } -//} \ No newline at end of file +void loop() { + // put your main code here, to run repeatedly: +// digital_val2 = digitalRead(TCRT_DIG); + if(rotationFlag) { + // Disable interrupts whilst reading to avoid race conditions. + + + noInterrupts(); + unsigned long rotationTime = curTime - prevTime; + unsigned long rotationsCopy = rotations; // To avoid race conditions + unsigned long curTimeCopy = curTime; + interrupts(); + + double rotationTimeSeconds = (double)rotationTime / 1e6; + double rpm = RPM_CONVERSION_FACTOR / rotationTimeSeconds; // rpm if the differnce between 2 values is above 300 ignore + double angularV = AV_CONVERSION_FACTOR / rotationTimeSeconds; + double angularA = (angularV - prevAV) / rotationTimeSeconds; + prevAV = angularV; + + + Serial.print(curTimeCopy); + Serial.write(SEPARATOR); + Serial.print(rotationsCopy); + Serial.write(SEPARATOR); + Serial.print(rotationTime); + Serial.write(SEPARATOR); + Serial.print(angularV, PRINT_DECIMALS); + Serial.write(SEPARATOR); + Serial.print(angularA, PRINT_DECIMALS); + Serial.write(SEPARATOR); + Serial.println(rpm, PRINT_DECIMALS); + + rotationFlag = false; + prevRPM = rpm; //change + } +} +// when speed up okay, there isn't a lot of chnage; but when the speed decreases very fast there are random values +//schmitt trigger