From 0ac44d6dd47b7f396719ceb58e99aabefd4656e1 Mon Sep 17 00:00:00 2001 From: Jonas Fleischer Date: Fri, 29 Mar 2024 18:19:27 +0100 Subject: [PATCH 1/2] added support for optical encoders --- ROSArduinoBridge/ROSArduinoBridge.ino | 22 +++++++++++++++++- ROSArduinoBridge/encoder_driver.h | 7 ++++++ ROSArduinoBridge/encoder_driver.ino | 33 +++++++++++++++++++++++++++ ROSArduinoBridge/globals.h | 16 +++++++++++++ ROSArduinoBridge/motor_driver.ino | 12 ++++++---- 5 files changed, 84 insertions(+), 6 deletions(-) create mode 100644 ROSArduinoBridge/globals.h diff --git a/ROSArduinoBridge/ROSArduinoBridge.ino b/ROSArduinoBridge/ROSArduinoBridge.ino index d851d6f9..7e32e179 100644 --- a/ROSArduinoBridge/ROSArduinoBridge.ino +++ b/ROSArduinoBridge/ROSArduinoBridge.ino @@ -60,7 +60,10 @@ //#define ROBOGAIA /* Encoders directly attached to Arduino board */ - #define ARDUINO_ENC_COUNTER + //#define ARDUINO_ENC_COUNTER + + /* Transmissive Optical Sensor with Phototransistor Output with external pull down resistor*/ + #define TOSPO_ENCODER /* L298 Motor driver*/ #define L298_MOTOR_DRIVER @@ -119,6 +122,12 @@ #endif /* Variable initialization */ +#ifdef TOSPO_ENCODER + #include "Globals.h" + // Initialize global variables with a default direction + MotorDirection leftMotorDirection = STOPPED; + MotorDirection rightMotorDirection = STOPPED; +#endif // A pair of varibles to help parse serial commands (thanks Fergs) int arg = 0; @@ -269,6 +278,17 @@ void setup() { // tell pin change mask to listen to right encoder pins PCMSK1 |= (1 << RIGHT_ENC_PIN_A)|(1 << RIGHT_ENC_PIN_B); + // enable PCINT1 and PCINT2 interrupt in the general interrupt mask + PCICR |= (1 << PCIE1) | (1 << PCIE2); + #elif defined(TOSPO_ENCODER) + //set as inputs + DDRD &= ~(1< 255) spd = 255; - if (i == LEFT) { - if (reverse == 0) { analogWrite(LEFT_MOTOR_FORWARD, spd); analogWrite(LEFT_MOTOR_BACKWARD, 0); } - else if (reverse == 1) { analogWrite(LEFT_MOTOR_BACKWARD, spd); analogWrite(LEFT_MOTOR_FORWARD, 0); } + if (i == LEFT) { + if (reverse == 0 && spd > 0) { analogWrite(LEFT_MOTOR_FORWARD, spd); analogWrite(LEFT_MOTOR_BACKWARD, 0); leftMotorDirection = FORWARD;} + else if (reverse == 1) { analogWrite(LEFT_MOTOR_BACKWARD, spd); analogWrite(LEFT_MOTOR_FORWARD, 0); leftMotorDirection = BACKWARD;} + else { analogWrite(LEFT_MOTOR_FORWARD, 0); analogWrite(LEFT_MOTOR_BACKWARD, 0); leftMotorDirection = STOPPED; } } else /*if (i == RIGHT) //no need for condition*/ { - if (reverse == 0) { analogWrite(RIGHT_MOTOR_FORWARD, spd); analogWrite(RIGHT_MOTOR_BACKWARD, 0); } - else if (reverse == 1) { analogWrite(RIGHT_MOTOR_BACKWARD, spd); analogWrite(RIGHT_MOTOR_FORWARD, 0); } + if (reverse == 0 && spd > 0) { analogWrite(RIGHT_MOTOR_FORWARD, spd); analogWrite(RIGHT_MOTOR_BACKWARD, 0); rightMotorDirection = FORWARD;} + else if (reverse == 1) { analogWrite(RIGHT_MOTOR_BACKWARD, spd); analogWrite(RIGHT_MOTOR_FORWARD, 0); rightMotorDirection = BACKWARD;} + else { analogWrite(RIGHT_MOTOR_BACKWARD, 0); analogWrite(RIGHT_MOTOR_FORWARD, 0); rightMotorDirection = STOPPED; } } } From bb0c907c680252a1f8bcc7f5195a7e0c7e0a5108 Mon Sep 17 00:00:00 2001 From: Jonas Flesicher Date: Mon, 3 Mar 2025 18:56:36 +0100 Subject: [PATCH 2/2] add support for nano esp32 --- ROSArduinoBridge/ROSArduinoBridge.ino | 73 +++++++++++++----------- ROSArduinoBridge/encoder_driver.h | 42 ++++++++------ ROSArduinoBridge/encoder_driver.ino | 82 ++++++++++++++++++++++++++- 3 files changed, 146 insertions(+), 51 deletions(-) diff --git a/ROSArduinoBridge/ROSArduinoBridge.ino b/ROSArduinoBridge/ROSArduinoBridge.ino index 7e32e179..c101f676 100644 --- a/ROSArduinoBridge/ROSArduinoBridge.ino +++ b/ROSArduinoBridge/ROSArduinoBridge.ino @@ -123,7 +123,7 @@ /* Variable initialization */ #ifdef TOSPO_ENCODER - #include "Globals.h" + #include "globals.h" // Fix capitalization to match actual filename // Initialize global variables with a default direction MotorDirection leftMotorDirection = STOPPED; MotorDirection rightMotorDirection = STOPPED; @@ -260,38 +260,47 @@ void setup() { // Initialize the motor controller if used */ #ifdef USE_BASE - #ifdef ARDUINO_ENC_COUNTER - //set as inputs - DDRD &= ~(1<