mvrk33 / AgroBot

5 stars 1 forks source link

Test all the components individually and together #12

Open Rishikz opened 5 months ago

krishnavamsi333 commented 4 months ago

testing

const int encoderPinC1 = 2;      // Encoder channel 1 pin
const int encoderPinC2 = 3;      // Encoder channel 2 pin
volatile long encoderCount = 0;   // Encoder pulse count
unsigned long previousMillis = 0; // Time tracking variables
unsigned long interval = 1000;    // Time interval in milliseconds

void setup() {
  Serial.begin(9600);

  // Setup encoder pins as inputs
  pinMode(encoderPinC1, INPUT);
  pinMode(encoderPinC2, INPUT);

  // Enable interrupts on both rising and falling edges
  attachInterrupt(digitalPinToInterrupt(encoderPinC1), handleEncoder, CHANGE);
  attachInterrupt(digitalPinToInterrupt(encoderPinC2), handleEncoder, CHANGE);
}

void loop() {
  // Calculate RPM every second
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    // Save the current time for the next calculation
    previousMillis = currentMillis;

    // Calculate RPM
    float timeIntervalMinutes = interval / 60000.0; // Convert milliseconds to minutes
    float pulsesPerRevolution = 44.0; // Number of pulses per revolution (CPR)
    float RPM = ((encoderCount / timeIntervalMinutes) / pulsesPerRevolution)/90;

    // Print RPM to the serial monitor
    Serial.print("RPM: ");
    Serial.println(RPM);

    // Reset encoder count
    encoderCount = 0;
  }
}

// Interrupt service routine to handle encoder pulses
void handleEncoder() {
  encoderCount++;
}

Encoder Specifications:

Encoder Type: Quad PPR: 11 CPR: 11 x 4 = 44 CPR at output shaft: 3960

(0-4094)pulse =1 rotation

11.372` pulse= 1 degree

Result

Screenshot 2024-02-18 150625

poojithanagasarapu commented 3 months ago

Rhino Motor Driver

Circuit Diagram

image

Requirements

Download RMCS - 2303 Arduino library from: RMCS2303drive_Arduino_Library.zip

Extracting zip files to Arduino

  1. Open Arduino.
  2. Go to Menu - Sketch > Include Library > Add .ZIP Library. image
  3. Choose the downloaded ZIP file.
  4. You can see confirmation if library import is successful. image
  5. From Menu - File > Examples > RMCS-2303_DC_Servo > Digital_speed_control_mode.

Code

RMCS2303 rmcs; //object for class RMCS2303

SoftwareSerial myserial(2,3); //Software Serial port For Arduino Uno. Comment out if using Mega.

//parameter Settings "Refer datasheet for details" byte slave_id=7; int INP_CONTROL_MODE=257;
int PP_gain=32; int PI_gain=16; int VF_gain=32; int LPR=334; int acceleration=5000; int speed=8000;

long int Current_position; long int Current_Speed;

void setup() { rmcs.Serial_selection(1); //Serial port selection:0-Hardware serial,1-Software serial rmcs.Serial0(9600); //Set baudrate for usb serial to monitor data on serial monitor Serial.println("RMCS-2303 Speed control mode demo\r\n\r\n");

//rmcs.begin(&Serial1,9600); //Uncomment if using hardware serial port for mega2560:Serial1,Serial2,Serial3 and set baudrate. Comment this line if Software serial port is in use rmcs.begin(&myserial,9600); //Uncomment if using software serial port. Comment this line if using hardware serial. rmcs.WRITE_PARAMETER(slave_id,INP_CONTROL_MODE,PP_gain,PI_gain,VF_gain,LPR,acceleration,speed); //Uncomment to write parameters to drive. Comment to ignore. rmcs.READ_PARAMETER(slave_id);

}

void loop(void) { Serial.println("Sending speed command - 8000 RPM"); rmcs.Speed(slave_id,8000); //Set speed within range of 0-65535 or 0-(maximum speed of base motor) rmcs.Enable_Digital_Mode(slave_id,0); //To enable motor in digital speed control mode. 0-fwd,1-reverse direction.

delay(3000); Current_Speed=rmcs.Speed_Feedback(slave_id); Serial.print("Current Speed feedback : "); Serial.println(Current_Speed);

delay(5000); Serial.println("Break Motor"); rmcs.Brake_Motor(slave_id,0); //Brake motor. 0-fwd,1-reverse direction.

delay(2000); Serial.println("Changing direction"); rmcs.Enable_Digital_Mode(slave_id,1); //To enable motor in digital speed control mode. 0-fwd,1-reverse direction.

delay(3000); Current_Speed=rmcs.Speed_Feedback(slave_id); Serial.print("Current Speed feedback : "); Serial.println(Current_Speed);

delay(3000); Serial.println("Disable Motor"); rmcs.Disable_Digital_Mode(slave_id,1); //Disable motor in digital speed control mode. 0-fwd,1-reverse direction. delay(3000); }

Code to control 2 motors at a time

include

RMCS2303 rmcs; //object for class RMCS2303

SoftwareSerial myserial(2,3); //Software Serial port For Arduino Uno. Comment out if using Mega.

//parameter Settings "Refer datasheet for details" byte slave_id_1 =7; byte slave_id_2 =6;

int INP_CONTROL_MODE=257;
int PP_gain=32; int PI_gain=16; int VF_gain=32; int LPR=334; int acceleration=5000; int speed=8000;

long int Current_position; long int Current_Speed;

void setup() { rmcs.Serial_selection(1); //Serial port selection:0-Hardware serial,1-Software serial rmcs.Serial0(9600); //Set baudrate for usb serial to monitor data on serial monitor Serial.println("RMCS-2303 Speed control mode demo\r\n\r\n");

//rmcs.begin(&Serial1,9600); //Uncomment if using hardware serial port for mega2560:Serial1,Serial2,Serial3 and set baudrate. Comment this line if Software serial port is in use rmcs.begin(&myserial,9600); //Uncomment if using software serial port. Comment this line if using hardware serial. rmcs.WRITE_PARAMETER(slave_id_1,INP_CONTROL_MODE,PP_gain,PI_gain,VF_gain,LPR,acceleration,speed); //Uncomment to write parameters to drive. Comment to ignore. rmcs.WRITE_PARAMETER(slave_id_2,INP_CONTROL_MODE,PP_gain,PI_gain,VF_gain,LPR,acceleration,speed); //Uncomment to write parameters to drive. Comment to ignore.

rmcs.READ_PARAMETER(slave_id_1); rmcs.READ_PARAMETER(slave_id_2); }

void loop(void) { Serial.println("Sending speed command - 8000 RPM"); rmcs.Speed(slave_id_1,8000); //Set speed within range of 0-65535 or 0-(maximum speed of base motor) rmcs.Enable_Digital_Mode(slave_id_1,0); //To enable motor in digital speed control mode. 0-fwd,1-reverse direction.

delay(3000);

Serial.println("Sending speed command - 8000 RPM"); rmcs.Speed(slave_id_2,8000); //Set speed within range of 0-65535 or 0-(maximum speed of base motor) rmcs.Enable_Digital_Mode(slave_id_2,0); //To enable motor in digital speed control mode. 0-fwd,1-reverse direction.

delay(3000); Current_Speed=rmcs.Speed_Feedback(slave_id_1); Serial.print("Current Speed feedback : "); Serial.println(Current_Speed);

delay(5000);

Current_Speed=rmcs.Speed_Feedback(slave_id_2); Serial.print("Current Speed feedback : "); Serial.println(Current_Speed);

delay(5000); Serial.println("Break Motor"); rmcs.Brake_Motor(slave_id_1,0); //Brake motor. 0-fwd,1-reverse direction.

delay(2000);

Serial.println("Break Motor"); rmcs.Brake_Motor(slave_id_2,0); //Brake motor. 0-fwd,1-reverse direction.

delay(2000);

Serial.println("Changing direction"); rmcs.Enable_Digital_Mode(slave_id_1,1); //To enable motor in digital speed control mode. 0-fwd,1-reverse direction.

delay(3000);

Serial.println("Changing direction"); rmcs.Enable_Digital_Mode(slave_id_2,1); //To enable motor in digital speed control mode. 0-fwd,1-reverse direction.

delay(3000);

Current_Speed=rmcs.Speed_Feedback(slave_id_1); Serial.print("Current Speed feedback : "); Serial.println(Current_Speed);

delay(3000);

Current_Speed=rmcs.Speed_Feedback(slave_id_2); Serial.print("Current Speed feedback : "); Serial.println(Current_Speed);

delay(3000);

Serial.println("Disable Motor"); rmcs.Disable_Digital_Mode(slave_id_1,1); //Disable motor in digital speed control mode. 0-fwd,1-reverse direction. delay(3000);

Serial.println("Disable Motor");

rmcs.Disable_Digital_Mode(slave_id_2,1); //Disable motor in digital speed control mode. 0-fwd,1-reverse direction. delay(3000);

}


## Output 

14:39:24.801 -> RMCS-2303 Speed control mode demo
14:39:24.848 -> 
14:39:24.848 -> 
14:39:24.848 -> INP_CONTROL :          DONE
14:39:24.898 -> INP_MODE :             DONE
14:39:24.945 -> PP_GAIN :              DONE
14:39:24.985 -> PI_GAIN :              DONE
14:39:25.087 -> VF_GAIN :              DONE
14:39:25.136 -> LPR :                  DONE
14:39:25.203 -> ACCELERATION :         DONE
14:39:25.301 -> SPEED :                DONE
14:39:25.301 -> <<DEVICE_MODBUS_ADDRESS: 7
14:39:25.357 -> <<INP_CONTROL_BYTE: 1
14:39:25.404 -> <<INP_MODE_BYTE: 1
14:39:25.453 -> <<PP_GAIN_BYTE: 32
14:39:25.487 -> <<VI_GAIN_BYTE: 16
14:39:25.525 -> <<VF_GAIN_BYTE: 32
14:39:25.569 -> <<LINES_PER_ROT: 334
14:39:25.604 -> <<TRP_ACL_WORD: 5000
14:39:25.638 -> <<TRP_SPD_WORD: 8000
14:39:25.687 -> Sending speed command - 8000 RPM
14:39:28.825 -> <<Current Speed feedback : 165
14:39:33.843 -> Break Motor
14:39:35.919 -> Changing direction
14:39:39.033 -> <<Current Speed feedback : -149
14:39:42.049 -> Disable Motor

# DEMO VIDEO 

https://github.com/mvrk33/AgroBot/assets/142658724/900aa545-c0ed-48f0-8490-c3c993dfbe7d
krishnavamsi333 commented 3 months ago

To find the correct I2C address


void setup() {
  Wire.begin();
  Serial.begin(9600);
  while (!Serial); // Wait for the serial port to connect
  Serial.println("\\nI2C Scanner");
}

void loop() {
  byte error, address;
  int nDevices = 0;
  Serial.println("Scanning...");

  for(address = 1; address < 127; address++ ) {
    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (error == 0) {
      Serial.print("I2C device found at address 0x");
      if (address < 16) Serial.print("0");
      Serial.print(address, HEX);
      Serial.println("  !");

      nDevices++;
    }
    else if (error == 4) {
      Serial.print("Unknown error at address 0x");
      if (address < 16) Serial.print("0");
      Serial.println(address, HEX);
    }    
  }
  if (nDevices == 0) Serial.println("No I2C devices found\\n");
  else Serial.println("done\\n");

  delay(5000); // Wait 5 seconds for the next scan
}