这是关于通过node.js srcipt中的I2C总线发送字节。 设定: 我正在使用Grove I2C Motor Driver Board通过Raspberry Pi 3通过I2C总线进行控制,并使用node.js脚本来控制有刷直流电动机。
控制开发板的官方代码适用于Arduino:
ccp-file with acual i2c commands
现在,我想使其与Raspberry Pi和节点脚本一起运行,并且我正在使用i2c-bus npm软件包和its writeWord() method。
在arduino github代码上,它们看起来像是just setting the speed to run the motor-和IC2MotorDriver Method(第113行):
/ *****************************DC Motor Function******************************
// Set the speed of a motor, speed is equal to duty cycle here
// motor_id: MOTOR1, MOTOR2
// _speed: -100~100, when _speed>0, dc motor runs clockwise; when _speed<0,
// dc motor runs anticlockwise
void I2CMotorDriver::speed(unsigned char motor_id, int _speed)
{
if(motor_id<MOTOR1 || motor_id>MOTOR2) {
Serial.println("Motor id error! Must be MOTOR1 or MOTOR2");
return;
}
if(motor_id == MOTOR1) {
if (_speed >= 0) {
this->_M1_direction = 1;
_speed = _speed > 100 ? 100 : _speed;
this->_speed1 = map(_speed, 0, 100, 0, 255);
}
else if (_speed < 0) {
this->_M1_direction = -1;
_speed = _speed < -100 ? 100 : -(_speed);
this->_speed1 = map(_speed, 0, 100, 0, 255);
}
}
else if(motor_id == MOTOR2) {
if (_speed >= 0) {
this->_M2_direction = 1;
_speed = _speed > 100 ? 100 : _speed;
this->_speed2 = map(_speed, 0, 100, 0, 255);
}
else if (_speed < 0) {
this->_M2_direction = -1;
_speed = _speed < -100 ? 100 : -(_speed);
this->_speed2 = map(_speed, 0, 100, 0, 255);
}
}
// Set the direction
if (_M1_direction == 1 && _M2_direction == 1) direction(BothClockWise);
if (_M1_direction == 1 && _M2_direction == -1) direction(M1CWM2ACW);
if (_M1_direction == -1 && _M2_direction == 1) direction(M1ACWM2CW);
if (_M1_direction == -1 && _M2_direction == -1) direction(BothAntiClockWise);
// send command
Wire.beginTransmission(this->_i2c_add); // begin transmission
Wire.write(MotorSpeedSet); // set pwm header
Wire.write(this->_speed1); // send speed of motor1
Wire.write(this->_speed2); // send speed of motor2
Wire.endTransmission(); // stop transmitting
delay(4); // Wait
}
所以对我来说,这是the crucial part:
Wire.beginTransmission(this->_i2c_add); // begin transmission
Wire.write(MotorSpeedSet); // set pwm header
Wire.write(this->_speed1); // send speed of motor1
Wire.write(this->_speed2); // send speed of motor2
Wire.endTransmission(); // stop transmitting
但是,如何在节点脚本中完成此操作。我确定我已正确连接V3.3 SDA,SCL和GND。 i2cdetect -y 1
显示了驱动器板:
pi@raspi:~/i2c_tests $ i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- 0f
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
我尝试了许多变化。我使用.writeWord方法希望将2个字节值(100和100)作为一个单词发送。 writeWord()方法的文档:
bus.writeWord(addr, cmd, word, cb)
addr - I2C device address
cmd - command code
word - data word
cb - completion callback
Asynchronous SMBus write word. The callback gets one argument (err).
但是似乎什么也没做:
/*********I2C command definitions****************/
const MotorSpeedSet = parseInt(0x82);
const PWMFrequenceSet = parseInt(0x84);
const DirectionSet = parseInt(0xaa);
const MotorSetA = parseInt(0xa1);
const MotorSetB = parseInt(0xa5);
const Nothing = parseInt(0x01);
/*********Motor ID*****parseInt(*****************/
const MOTOR1 = parseInt(1);
const MOTOR2 = parseInt(2);
/*********Motor DirectiparseInt(on***************/
const BothClockWise = parseInt(0x0a);
const BothAntiClockWise = parseInt(0x05);
const M1CWM2ACW = parseInt(0x06);
const M1ACWM2CW = parseInt(0x09);
/*********Motor DirectiparseInt(on***************/
const ClockWise = parseInt(0x0a);
const AntiClockWise = parseInt(0x05);
/*********Prescaler FreparseInt(quence***********/
const F_31372Hz = parseInt(0x01);
const F_3921Hz = parseInt(0x02);
const F_490Hz = parseInt(0x03);
const F_122Hz = parseInt(0x04);
const F_30Hz = parseInt(0x05);
/*********I2C Address of the Grove Board*********/
const I2C_ADDRESS = parseInt(0x0f); // 0x0f = default Asdress
var i2c1;
i2c1 = i2c.open(1, ()=>{
//i2c1.writeByte(I2C_ADDRESS, MotorSpeedSet, 0x01, cb);
//short int16 = (short)(((0x64 & 0xFF) << 8) | (0x00 & 0xFF));
//trying with a buffer
var buffer = new Buffer([0x64,0x64]); // speed values 100 and 100, motor A & B
//trying with a In16Array
var int16 = new Int16Array(2);
int16[0] = 42;
i2c1.writeWord(I2C_ADDRESS, MotorSpeedSet, int16[0], ()=>{});
//trying subsequent sendings of 1 byte
//i2c1.sendByte(I2C_ADDRESS, MotorSpeedSet, ()=>{});
//i2c1.sendByte(I2C_ADDRESS, 100, ()=>{});
//i2c1.sendByte(I2C_ADDRESS, 100, ()=>{});
});
因此,我在这里寻求建议或获得有关如何解决此问题的新思路。似乎诀窍在于如何处理和发送每个电动机的那两个速度字节,就像它们对arduino代码所做的一样。但是我真的不知道如何在node.js中正确实现它。我希望我能理解地描述它。