2017-01-18 44 views
1

我試圖旋轉使用樹莓PI 2的I2C接口用下面的代碼電動機(RMCS-220X高扭矩編碼器直流伺服電動機和驅動程序):wiringPi庫旋轉雙向DC伺服馬達

#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; 
} 

在函數wiringPiI2CWriteReg16中寫入的字節如下面的代碼在電機的數據表中給出。

/*這裏是關於RMCS-220X更新速度變量的一個例子使用轉發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 

/*這裏是關於RMCS-220X更新速度變量的一個例子使用扭轉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(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 

在我的情況與如上所述使用wiringPi庫,允許我僅在一個(右)方向上旋轉馬達的代碼。現在,我想它在左方向旋轉。

將非常感謝您的建議。

回答

0

您必須在最後2個字節的數據使用符號值:

要旋轉向後:

... 

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_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 

...