Programming a motors with I2c

Hello, I am using EMG30 motors and MD25 driver. Exactly this kit: EMG30 data
I downloaded a code from Downloads and I have got this

/***************************************************************
// CONTROLO MOTOR COM MD-25
//**************************************************************/
#include <Wire.h>
/***************************************************************
// MD25Add (default)
//**************************************************************/
#define MD25Add 0xB0 >> 1 // The Arduino IIC functions automaically 
                          // add one or zero at the end of the 
                          // address for read/write operations
/***************************************************************
// GENERAL REG.s
//**************************************************************/
#define Speed1       0x00
#define Speed2       0x01
#define Enc1a        0x02
#define Enc1b        0x03
#define Enc1c        0x04
#define Enc1d        0x05
#define Enc2a        0x06
#define Enc2b        0x07
#define Enc2c        0x08
#define Enc2d        0x09
#define Battery      0x0A
#define M1Current    0x0B
#define M2Current    0x0C
#define SWver        0x0D
#define Acceleration 0x0E
#define Mode         0X0F
#define Command      0X10
/***************************************************************
// COMMAND REG.s
//**************************************************************/
#define Reset_encoder        0x20
#define Disable_controller   0x30
#define Enable_controller    0x31
#define Disable_timeout      0x32
#define Enable_timeout       0x33
#define Change_i2c_address_1 0xA0
#define Change_i2c_address_2 0xAA
#define Change_i2c_address_3 0xA5
/***************************************************************
// Enable/Disable Timeout
//**************************************************************/
void enableTimeOut(boolean state){
  Wire.beginTransmission(MD25Add); // Transmit to device
  Wire.write(Command);             // Access to command register
  if (state){
    Wire.write(Enable_timeout);    // Enable 2 second timeout
  }
  else{
    Wire.write(Disable_timeout);   // Disable 2 second timeout
  } 
  Wire.endTransmission();          // Stop transmitting
}
/***************************************************************
// Enable/Disable on-board Controller
//**************************************************************/
void enableController(boolean state){
  Wire.beginTransmission(MD25Add); // Transmit to device
  Wire.write(Command);             // Access to command register
  if (state){
    Wire.write(Enable_controller);    // Enable controller
  }
  else{
    Wire.write(Disable_controller);   // Disable controller
  } 
  Wire.endTransmission();          // Stop transmitting
}
/***************************************************************
// Reset encoder function
//**************************************************************/
void resetEncoders(void){
  Wire.beginTransmission(MD25Add); // Transmit to device
  Wire.write(Command);             // Access to command register
  Wire.write(Reset_encoder);       // Access to reset register  
  Wire.endTransmission();          // Stop transmitting
}
/***************************************************************
// Send order to motor
//**************************************************************/
void sendData2Reg(byte reg, byte val)
{
  Wire.beginTransmission(MD25Add); // Device select
  Wire.write(reg);                 // Reg 2 write
  Wire.write(val);                 // data 
  Wire.endTransmission();          // End transmission
}
/***************************************************************
// Reset encoder function
//**************************************************************/
int receiveFromEncoder(byte reg)
{ //---------------------------------------------------
  // reg must be Enc1a or Enc1b
  //---------------------------------------------------
  int encoder = 0;         
  byte measure[4];
  byte k = 0;
  Wire.beginTransmission(MD25Add); // Transmit to device MD25
  Wire.write(reg);                 // Ask for Encx register    
  Wire.endTransmission();          // Stop transmitting 

  Wire.requestFrom(MD25Add, 4);    // Request 4 bytes of data from MD-25
                                   // Scan IIC port and retrieve data
                                   // Scan IIC port and retrieve data
  while(Wire.available())          // while available...
  { 
    measure[k] = Wire.read();      // Measure byte
    k++;
  }
  // Concatenate the data
  encoder = (measure[0]<<24) | (measure[1]<<16) | (measure[2]<<8) | measure[3];
  return(encoder);
}
/***************************************************************
// Setup function
//**************************************************************/
void setup()
{
  Serial.begin(115200);          // Initialize serial comunication with 
                                 // the computer's terminal
  Wire.begin();                  // Initialize Arduino as the IIC Master
  resetEncoders();               // Initialize to zero the encoder register
  enableTimeOut(false);          // Disable automatic 2s timeout
  enableController(false);       // Disable on-board feedback speed control
  sendData2Reg(Acceleration,2);  // Decrease acceleration (default = 5)
  sendData2Reg(Speed1, 128);     // The motor is initially stoped
  
  double data;
  for(int k=255;k>=0;--k){
    sendData2Reg(Speed1, k);
    // Filter encoder data------------------------
    data = 0;
    for (int sample=0;sample<15;sample++)
    {
      data+=(receiveFromEncoder(Enc1a)/15.0);
    }
    // -------------------------------------------
    Serial.println(data);
    delay(100);
  }
  sendData2Reg(Speed1, 128); // Stop the motor
  enableTimeOut(true);       // Enable 2 second protection timer
  enableController(true);    // Enable velocity controller
}

/***************************************************************
// Loop function
//**************************************************************/
void loop()
{
 // Nothing to do ;-)
}

I have no clue whats going on here, and I don't know how to make the encoders work.

My objective is to make the motors run for a specific counts of the encoder and then turn etc.
Can I just use a command out of this code in order to do that.

I never seen this kind of communication with a motor driver.
If this is any help here is the command list: MD25 Serial Documentation

I would be really grateful if someone could help! Thank you.

I have also found this code from the examples on the website I bought it from using serial:

/****************************************************************
*                    Arduino MD25 example code                  *
*                   MD25 running in serial mode                 *
*                                                               *
*                     by James Henderson 2012                   *
*****************************************************************/

#include <SoftwareSerial.h>

// Values of 0 being sent using serial.write() have to be cast as a byte to stop them being misinterpereted as NULL
// This is a bug with arduino 1
#define CMD                 (byte)0x00              //  MD25 command byte of 0

#define WRITESP1            0x31                    // Byte writes speed to motor 1
#define WRITEACCEL          0x33                    // Byte writes a value of acceleration
#define RESETREG            0x35                    // Byte resets encoders
#define SETMODE             0x34                    // Byte sets mode
#define READIVS             0x2C                    // Byte reads motor currents and battery voltage        
#define READENCS            0x25                    // Byte reads both encoders
#define GET_VER             0x29

#define LCD_RX              0x02                    // RX and TX pins used for LCD0303 serial port
#define LCD_TX              0x03
#define LCD03_HIDE_CUR      0x04
#define LCD03_CLEAR         0x0C
#define LCD03_SET_CUR       0x02

SoftwareSerial lcd03 = SoftwareSerial(LCD_RX, LCD_TX);          // Define the serial port for the LCD03

long encValue = 0;
byte softwareRev = 0;

void setup(){
  Serial.begin(38400);
  lcd03.begin(9600);
  
  Serial.write(CMD);                                            // Set MD25 accelleration value
  Serial.write(WRITEACCEL);
  Serial.write(10);
  delayMicroseconds(10);                                        // Wait for this to be processed
  Serial.write(CMD);                                            // Reset the encoder registers to 0
  Serial.write(RESETREG);         
  Serial.write(CMD);                                            // Set mode to 2, Both motors controlled by writing to speed 1
  Serial.write(SETMODE);
  Serial.write(2);    
  
  Serial.write(CMD);                                            // Get software version of MD25
  Serial.write(GET_VER);
  while(Serial.available() < 1){}                               // Wait for byte to become available         
  softwareRev = Serial.read();  
  
  lcd03.write(LCD03_CLEAR);                                     // Clear the LCD03 screen
  lcd03.write(LCD03_HIDE_CUR);                                  // Hide the LCD03 cursor
}

void loop(){ 
  while(encValue < 3000){               // While encoder 1 value less than 3000 move forward
    Serial.write(CMD);                  // Set motors to drive forward at full speed
    Serial.write(WRITESP1);
    Serial.write(150);
    encValue = readEncoder();
    readVolts();
  }
  delay(100);
  while(encValue > 100){
    Serial.write(CMD);                  // Drive motors reverse at full speed
    Serial.write(WRITESP1);
    Serial.write(100);
    encValue = readEncoder();
    readVolts();
  }
  delay(100);
}

long readEncoder(){                        // Function to read and display the value of both encoders, returns value of first encoder
  long result1 = 0; 
  long result2 = 0;
  Serial.write(CMD);
  Serial.write(READENCS);
  while(Serial.available() < 8){}          // Wait for 8 bytes, first 4 encoder 1 values second 4 encoder 2 values 
  result1 = Serial.read();                 // First byte for encoder 1, HH.
  result1 <<= 8;
  result1 += Serial.read();                // Second byte for encoder 1, HL
  result1 <<= 8;
  result1 += Serial.read();                // Third byte for encoder 1, LH
  result1 <<= 8;
  result1  += Serial.read();               // Fourth byte for encoder 1, LL
  result2 = Serial.read();
  result2 <<= 8;
  result2 += Serial.read();
  result2 <<= 8;
  result2 += Serial.read();
  result2 <<= 8;
  result2 += Serial.read();

  lcd03.write(LCD03_SET_CUR);              // Set the LCD03 cursor position
  lcd03.write(21);
  lcd03.print("Encoder 1:");               // Displays data to the LCD03 screen
  lcd03.print(result1,DEC);
  lcd03.print(" ");                        // Print a blank space to clear any unwanted characters that are leftover on the LCD03 display
  
  delay(5);                                // Delay for LCD03 to process data
  
  lcd03.write(LCD03_SET_CUR);
  lcd03.write(41); 
  lcd03.print("Encoder 2:");
  lcd03.print(result2,DEC);
  lcd03.print(" ");
  return result1;                                   
}
  
void readVolts(){                                                 // Function reads current for both motors and battery voltage
  byte batteryVolts, mot1_current, mot2_current = 0;
  Serial.write(CMD);
  Serial.write(READIVS);                                          // Send byte to readbattery voltage and motor currents
  while(Serial.available() < 3){}                                 // Wait for the 3 bytes to become available then get them
  batteryVolts = Serial.read();
  mot1_current = Serial.read();
  mot2_current = Serial.read();

  lcd03.write(LCD03_SET_CUR);
  lcd03.write(61);
  lcd03.print("Mot1 I:");
  lcd03.print(mot1_current,DEC);
  lcd03.print(" Mot2 I:");
  lcd03.print(mot2_current,DEC);
  lcd03.print(" "); 
  
  delay(5);
  
  lcd03.write(LCD03_SET_CUR);
  lcd03.write(1);
  lcd03.print("Rev:");
  lcd03.print(softwareRev, DEC);
  lcd03.print(" ");
  lcd03.print("Volts:");
  lcd03.print(batteryVolts/10,DEC);                               // Seperate whole number and descimal place of battery voltage and display
  lcd03.print(".");  
  lcd03.print(batteryVolts%10,DEC);
  lcd03.print(" ");   
}

Hello,

If this is any help here is the command list: MD25 Serial Documentation

I think your link is not referring to I2C but to Serial communication with the MD25.
The right link must be: MD25 Technical Documentation.

I think there must be a mistake also here:

int receiveFromEncoder(byte reg)
{ //---------------------------------------------------
  // reg must be Enc1a or Enc1b
  //---------------------------------------------------

I rather think reg must be Enc1a or Enc2a, the beginning adress of the encoders, don't you think I am right ?

I am also trying to use an MD25 to control motors and I don't manage to have them run, even with this example.
When I try reading any register, I get the disappointing 0 value, even for battery rate or #version.

Does anyone know how to do that ?

Also, I didn't know about the communication time of I2C, but when I try to use this with the Serial Monitor I can only print 3 or 4 lines per second. Is it normal ? Won't it ruin the command to the motors ?

The controller gives you a choice, talk to via serial or via I2C which one are you going to use it's upto you!.

Mark