0

这是英特尔运行加速度计(Grove 数字)的示例代码:

// Load accelerometer
var adxl345 = require('jsupm_adxl345');

// Instantiate on I2C bus
var adxl = new adxl345.Adxl345(0);

setInterval(function()
{
    adxl.update(); // Update the data
    var raw = adxl.getRawValues(); // Read raw sensor data
    var force = adxl.getAcceleration(); // Read acceleration force (g)
    var rawvalues = raw.getitem(0) + " " + raw.getitem(1) + " " + raw.getitem(2);
    console.log("Raw Values: " + rawvalues);
    console.log("ForceX: " + force.getitem(0).toFixed(2) + " g");
    console.log("ForceY: " + force.getitem(1).toFixed(2) + " g");
    console.log("ForceZ: " + force.getitem(2).toFixed(2) + " g");
}, 1000);

它构建并上传到 Edison Arduino 板,但在运行时会引发此错误:

ERROR: /home/root/.node_app_slot/main.js:10                                                                                                                                             
ERROR: var adxl = new adxl345.Adxl345(0);                                                                                                                                               
   ERROR:         ^                                                                                                                                                                     
ERROR: Error: UPM Runtime Error: Adxl345: i2c.write() control register failed                                                                                                           
    at Object.<anonymous> (/home/root/.node_app_slot/main.js:10:12)                                                                                                                     
    at Module._compile (module.js:456:26)                                                                                                                                               
    at Object.Module._extensions..js (module.js:474:10)                                                                                                                                 
    at Module.load (module.js:356:32)                                                                                                                                                   
    at Function.Module._load (module.js:312:12)                                                                                                                                         
    at Function.Module.runMain (module.js:497:10)                                                                                                                                       
    at startup (node.js:119:16)                                                                                                                                                         
    at node.js:935:3                                                                                                                                                                    

有什么想法吗?加速度计连接到 I2C 端口。另一个传感器(温度)工作正常。

4

1 回答 1

0

grove 加速器不是 ADXL345 而是 MMA7660,如此所述。

以下是 MMA7660 的 upm 代码

 var digitalAccelerometer = require('jsupm_mma7660');
    // Instantiate an MMA7660 on I2C bus 0
    var myDigitalAccelerometer = new digitalAccelerometer.MMA7660(
                    digitalAccelerometer.MMA7660_I2C_BUS, 
                    digitalAccelerometer.MMA7660_DEFAULT_I2C_ADDR);

    // place device in standby mode so we can write registers
    myDigitalAccelerometer.setModeStandby();

    // enable 64 samples per second
    myDigitalAccelerometer.setSampleRate(digitalAccelerometer.MMA7660.AUTOSLEEP_64);

// place device into active mode
myDigitalAccelerometer.setModeActive();

var x, y, z;
x = digitalAccelerometer.new_intp();
y = digitalAccelerometer.new_intp();
z = digitalAccelerometer.new_intp();

var ax, ay, az;
ax = digitalAccelerometer.new_floatp();
ay = digitalAccelerometer.new_floatp();
az = digitalAccelerometer.new_floatp();

var outputStr;

var myInterval = setInterval(function()
{
    myDigitalAccelerometer.getRawValues(x, y, z);
    outputStr = "Raw values: x = " + digitalAccelerometer.intp_value(x) +
    " y = " + digitalAccelerometer.intp_value(y) +
    " z = " + digitalAccelerometer.intp_value(z);
    console.log(outputStr);

    myDigitalAccelerometer.getAcceleration(ax, ay, az);
    outputStr = "Acceleration: x = " 
        + roundNum(digitalAccelerometer.floatp_value(ax), 6)
        + "g y = " + roundNum(digitalAccelerometer.floatp_value(ay), 6) 
        + "g z = " + roundNum(digitalAccelerometer.floatp_value(az), 6) + "g";
    console.log(outputStr);
}, 500);

// round off output to match C example, which has 6 decimal places
function roundNum(num, decimalPlaces)
{
    var extraNum = (1 / (Math.pow(10, decimalPlaces) * 1000));
    return (Math.round((num + extraNum) 
        * (Math.pow(10, decimalPlaces))) / Math.pow(10, decimalPlaces));
}

// When exiting: clear interval and print message
process.on('SIGINT', function()
{
    clearInterval(myInterval);

    // clean up memory
    digitalAccelerometer.delete_intp(x);
    digitalAccelerometer.delete_intp(y);
    digitalAccelerometer.delete_intp(z);

    digitalAccelerometer.delete_floatp(ax);
    digitalAccelerometer.delete_floatp(ay);
    digitalAccelerometer.delete_floatp(az);

    myDigitalAccelerometer.setModeStandby();

    console.log("Exiting...");
    process.exit(0);
});
于 2016-07-18T08:14:28.770 回答