akimach / LSM9DS1_RaspberryPi_Library

RaspberryPi library for the LSM9DS1 9DOF IMU
19 stars 19 forks source link

Not working on I2C-0 of compute module 3 Lite #4

Open sohilmehta92 opened 6 years ago

sohilmehta92 commented 6 years ago

I am running the code on Compute Module 3 Lite. OS is Raspbian Stretch Lite (the latest release). It works perfectly well on I2C-1 but not on I2C-0. Any particular reason why?

sohilmehta92 commented 6 years ago

Could someone point out what changes will have to be made to make it work on I2C-0 of compute module 3 Lite? Here's my test code based on the library:

#!/usr/bin/env python3
# coding: utf-8

import ctypes

import threading
import sys
import time
import sched
import datetime
from datetime import datetime

from ctypes import *
{ "__CURSOR" : "s=f80b63610a5f46a5a775ef80a3624a09;i=7956;b=1800441909b74ff7a2edd5e36aefb907;m=cf2de5bc;t=55bd13336f4b1;x=3c80b7f7f096674c", "__REALTIME_TIMESTAMP" : "1508328949085361", "__MONOTONIC_TIMESTAMP" : "3475891644", "_BOOT_ID" : "1800441909b74ff7a2edd5e36aefb907", "_SYSTEMD_CGROUP" : "/user.slice/user-1000.slice/session-c1.scope", "_SYSTEMD_SESSION" : "c1", "_SYSTEMD_OWNER_UID" : "1000", "_SYSTEMD_UNIT" :
        "session-c1.scope", "_SYSTEMD_SLICE" : "user-1000.slice", "_SYSTEMD_USER_SLICE" : "-.slice", "_SYSTEMD_INVOCATION_ID" : "3e7e567985cc4ca1899bbe3617a8fcab", "_MACHINE_ID" : "2da8dbfa7f954d6a9797343a72038f2e", "_HOSTNAME" : "raspberrypi", "PRIORITY" : "6", "_UID" : "0", "_GID" : "0", "_CAP_EFFECTIVE" : "3fffffffff", "_TRANSPORT" : "syslog", "SYSLOG_FACILITY" : "10", "SYSLOG_IDENTIFIER" : "sudo", "_COMM" : "sudo",
        "_EXE" : "/usr/bin/sudo", "MESSAGE" : "pam_unix(sudo:session): session opened for user root by (uid=0)", "_PID" : "5032", "_CMDLINE" : "sudo journalctl -f -o json", "_SOURCE_REALTIME_TIMESTAMP" : "1508328949085270" }

LSMSENSOR_COUNT = 0
path = "/home/pi/python_scripts/LSM9DS1_RaspberryPi_Library-master/lib/liblsm9ds1cwrapper.so"
lib = cdll.LoadLibrary(path)

lib.lsm9ds1_create.argtypes = []
lib.lsm9ds1_create.restype = c_void_p

lib.lsm9ds1_begin.argtypes = [c_void_p]
lib.lsm9ds1_begin.restype = None

lib.lsm9ds1_calibrate.argtypes = [c_void_p]
lib.lsm9ds1_calibrate.restype = None

lib.lsm9ds1_gyroAvailable.argtypes = [c_void_p]
lib.lsm9ds1_gyroAvailable.restype = c_int
lib.lsm9ds1_accelAvailable.argtypes = [c_void_p]
lib.lsm9ds1_accelAvailable.restype = c_int
lib.lsm9ds1_magAvailable.argtypes = [c_void_p]
lib.lsm9ds1_magAvailable.restype = c_int

lib.lsm9ds1_readGyro.argtypes = [c_void_p]
lib.lsm9ds1_readGyro.restype = c_int
lib.lsm9ds1_readAccel.argtypes = [c_void_p]
lib.lsm9ds1_readAccel.restype = c_int
lib.lsm9ds1_readMag.argtypes = [c_void_p]
lib.lsm9ds1_readMag.restype = c_int

lib.lsm9ds1_getGyroX.argtypes = [c_void_p]
lib.lsm9ds1_getGyroX.restype = c_float
lib.lsm9ds1_getGyroY.argtypes = [c_void_p]
lib.lsm9ds1_getGyroY.restype = c_float
lib.lsm9ds1_getGyroZ.argtypes = [c_void_p]
lib.lsm9ds1_getGyroZ.restype = c_float

lib.lsm9ds1_getAccelX.argtypes = [c_void_p]
lib.lsm9ds1_getAccelX.restype = c_float
lib.lsm9ds1_getAccelY.argtypes = [c_void_p]
lib.lsm9ds1_getAccelY.restype = c_float
lib.lsm9ds1_getAccelZ.argtypes = [c_void_p]
lib.lsm9ds1_getAccelZ.restype = c_float

lib.lsm9ds1_calcGyro.argtypes = [c_void_p, c_float]
lib.lsm9ds1_calcGyro.restype = c_float
lib.lsm9ds1_calcAccel.argtypes = [c_void_p, c_float]
lib.lsm9ds1_calcAccel.restype = c_float

if __name__ == "__main__":
    imu = lib.lsm9ds1_create()
    print("IMU instance created")
    lib.lsm9ds1_begin(imu)
    print("Comm initiated")
    if lib.lsm9ds1_begin(imu) == 0:
        print("Failed to communicate with LSM9DS1.")
        quit()
    print("Calibrating IMU")
    lib.lsm9ds1_calibrate(imu)
    print("Calibration Complete")

    while True:
        while lib.lsm9ds1_gyroAvailable(imu) == 0:
            pass
        lib.lsm9ds1_readGyro(imu)
        while lib.lsm9ds1_accelAvailable(imu) == 0:
            pass
        lib.lsm9ds1_readAccel(imu)
        while lib.lsm9ds1_magAvailable(imu) == 0:
            pass
        lib.lsm9ds1_readMag(imu)

        print("Get Gyro")
        gx = lib.lsm9ds1_getGyroX(imu)
        gy = lib.lsm9ds1_getGyroY(imu)
        gz = lib.lsm9ds1_getGyroZ(imu)

        print("Get Accel")
        ax = lib.lsm9ds1_getAccelX(imu)
        ay = lib.lsm9ds1_getAccelY(imu)
        az = lib.lsm9ds1_getAccelZ(imu)

        print("Calculate Gyro")
        cgx = lib.lsm9ds1_calcGyro(imu, gx)
        cgy = lib.lsm9ds1_calcGyro(imu, gy)
        cgz = lib.lsm9ds1_calcGyro(imu, gz)

        print("Calculate Accel")
        cax = lib.lsm9ds1_calcAccel(imu, ax)*9.81
        cay = lib.lsm9ds1_calcAccel(imu, ay)*9.81
        caz = lib.lsm9ds1_calcAccel(imu, az)*9.81

        print("Accel: %f, %f, %f " % (cax, cay, caz))
        print("Gyro: %f, %f, %f " % (cgx, cgy, cgz))

        time.sleep(1)

And's here's the error that I get:


IMU instance created
Comm initiated
Calibrating IMU
Calibration Complete
terminate called after throwing an instance of 'int'
Aborted
jwpleow commented 3 years ago

It's hardcoded in the function wiringPiI2CSetup unfortunately https://github.com/WiringPi/WiringPi/blob/master/wiringPi/wiringPiI2C.c

berndporr commented 3 years ago

The wrapper around i2c is so thin in the wiring pi library that one could directly do i2c access via ioctrl. It's virtually not more code. I might have a go at it over Easter for my fork and then of @akimach likes it can include it in his version.