craspberry-pi2i2cwiringpi

wiringPi library of Raspberry Pi 2 to rotate bidirectional DC servo motor


I am trying to spin the motor (RMCS-220X High-Torque Encoder DC Servo Motor and Driver) using Raspberry pi 2's i2c interface with the following code :

#include <stdio.h>
#include <wiringPi.h>
#include <wiringPiI2C.h>
#define Motoraddrs 0x08 //motor address

int main(void){
int I2C_fd;

wiringPiSetupGpio();
wiringPiSetup(); 

I2C_fd = wiringPiI2CSetup(Motoraddrs);

wiringPiI2CWriteReg16(I2C_fd, 0x01, 0xff);//spins from low speed of 0x01 to
                                            max 0xff in right side
delay(100);

return 0; 
}

The bytes written in function wiringPiI2CWriteReg16 are as of the following code below which are given in Datasheet of the motor.

/*Here is an example usage of updating the speed variable on the RMCS-220x to forward 255 */

I2C_Start(0x10 + 0); // send the slave address of the RMCS-220x and write   
                        bit 0                
I2C_Write(1); // send the command variable for speed
I2C_Write(255); // send LSB of 255
I2C_Write(0); // send MSB of 0 to and so Speed of forward 255
I2C_Stop(); // send I2C stop

/*Here is an example usage of updating the speed variable on the RMCS-220x to reverse 255 and reading it back */

I2C_Start(0x10 + 0); // send the slave address of the RMCS-220x and 
                       write bit 0
I2C_Write(1); // send the command variable for speed
I2C_Write(1); // send LSB of 1
I2C_Write(255); // send MSB of 255 to and so Speed of backward 255
I2C_Rep_Start(0x10 + 1); // send I2C address with rep start and 1 to read
speed = I2C_Read_Ack(); // read speed LSB byte and ack
speed = I2C_Read_Nak(); // read speed MSB byte and don’t ack
I2C_Stop(); // send I2C stop

In my case with the code above using wiringPi library, allowing me to rotate the motor only in one (right) direction. Now I want to rotate it in left direction as well.

Would really appreciate your suggestions.


Solution

  • You must use signed value in last 2 bytes of data:

    To spin backwards:

    ...
    
    I2C_Write(1); // send the command variable for speed
    
    I2C_Write(1); // send LSB of 1 
    
    I2C_Write(255);// send MSB of 255 to and so Speed of backward 255 
    
    ...
    

    To spin forwards:

    ...
    
    I2C_Write(1);// send the command variable for speed  
    I2C_Write(255);// send LSB of 255  
    I2C_Write(0);// send MSB of 0 to and so Speed of forward 255
    
    ...