【问题标题】:wiringPi library of Raspberry Pi 2 to rotate bidirectional DC servo motorRaspberry Pi 2的wiringPi库旋转双向直流伺服电机
【发布时间】:2017-01-18 18:50:03
【问题描述】:

我正在尝试使用 Raspberry pi 2 的 i2c 接口和以下代码旋转电机(RMCS-220X 高扭矩编码器直流伺服电机和驱动器):

#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 库,允许我只在一个(右)方向上旋转电机。现在我也想向左旋转它。

非常感谢您的建议。

【问题讨论】:

    标签: c raspberry-pi2 i2c wiringpi


    【解决方案1】:

    您必须在数据的最后 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
    
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多