You can use the == operator to see if two pin objects are the same.
p = machine.Pin(0)
p == machine.Pin(0) # True
p == machine.Pin(1) # False
MicroPython actually only creates one Pin object for each physical pin. So you could also use the is operator to compare pin objects by identity.
Pin interrupts on Rpi pico with arduno-pico code
Correct way to implement interrupts inside a class (Pi Pico W)
micropython - RPI Pico Micro python which pin triggered the interrupt - Stack Overflow
micropython - How to find which pin caused an interrupt? - Stack Overflow
Videos
Hi!
I am coding a differential robot controller on the Pi Pico W with C++ using the Arduino IDE and I'm facing some trouble implementing the interrupts for the motor encoders.
My main class is robot_control and I am using a motor_controller class to initialize each motor and encoder. This is the class where I want to set the interrupts.
After failing a lot and reviewing many posts on the subject -specially the last comment of this thread: https://forum.arduino.cc/t/interrupt-inside-a-class/180419 I finally got to this code, which compiles but doesn't trigger the interrupts.
Can you please help me find the reason and possible solutions? Thank you in advance!
Main .ino file
// main.ino
#include "robot\_control.h"
RobotControl robot;
void setup() {
Serial.begin(115200);
while (!Serial) {
; // Wait for serial port to connect
}
Serial.println("Initializing robot control system...");
robot.setup();
Serial.println("Robot control system initialized.");
}
void loop() {
robot.loop();
}robot_control.cpp
#include "robot\_control.h"
RobotControl::RobotControl() : emergencyStop(false), currentMode(ControlMode::AUTONOMOUS) {
// Adjust pin numbers as needed for your setup
leftMotor = new MotorController(11, 12, 13, 3); // en, l\_pwm, r\_pwm, encoderA
rightMotor = new MotorController(7, 8, 9, 1); // en, l\_pwm, r\_pwm, encoderB
display = new Display();
wifiManager = new WifiManager();
joystick = new JoystickController(26, 27, 16); // Adjust pins as needed
}
void RobotControl::setup() {
pinMode(EMERGENCY\_STOP\_PIN, INPUT\_PULLUP);
display->init();
wifiManager->connect("ssid", "psw");
}
void RobotControl::loop() {
// Related stuff
}robot_control.h
// robot_control.h
#pragma once
#include <Arduino.h>
#include <WiFi.h>
#include <SPI.h>
#include <Wire.h>
#include <U8g2lib.h>
#include <BTS7960.h>
class MotorController {
public:
MotorController(int en, int l_pwm, int r_pwm, int encoderPin);
void setSpeed(int speed);
int getSpeed();
void enable();
void disable();
void turnLeft(uint8_t pwm);
void turnRight(uint8_t pwm);
void update();
void encoderLoop();
void handleEncoder();
private:
BTS7960 motor;
// Encoder variables
volatile long encoderCount;
unsigned long lastTime;
float currentSpeed;
int encoderPin;
static MotorController* sEncoder;
static void handleEncoderISR();
};
class RobotControl {
public:
RobotControl();
void setup();
void loop();
private:
MotorController* leftMotor;
MotorController* rightMotor;
Display* display;
WifiManager* wifiManager;
JoystickController* joystick;
// Related functions
};motor_controller.cpp
#include "robot_control.h"
MotorController* MotorController::sEncoder = 0;
MotorController::MotorController(int en, int l_pwm, int r_pwm, int encoderPin)
: motor(en, l_pwm, r_pwm), encoderPin(encoderPin),
encoderCount(0), lastTime(0), currentSpeed(0.0), pulsesPerRevolution(42) {
pinMode(encoderPin, INPUT_PULLUP);
// Set up Encoder variables
const int pulsesPerRevolution = 42;
sEncoder = this;
// Attach interrupt for encoder
attachInterrupt(digitalPinToInterrupt(encoderPin), MotorController::handleEncoderISR, RISING);
}
void MotorController::setSpeed(int speed) {
setpoint = speed;
}
int MotorController::getSpeed() {
return currentSpeed;
}
void MotorController::enable() {
motor.Enable();
}
void MotorController::disable() {
motor.Disable();
}
void MotorController::turnLeft(uint8_t pwm) {
motor.TurnLeft(pwm);
}
void MotorController::turnRight(uint8_t pwm) {
motor.TurnRight(pwm);
}
void MotorController::update() {
// update
}
void MotorController::updatePID() {
// PID algorithm
}
void MotorController::handleEncoder() {
encoderCount++;
}
void MotorController::handleEncoderISR() {
if (sEncoder != 0)
sEncoder->handleEncoder();
}
void MotorController::encoderLoop() {
//…
}