Report
3 years ago.

Python <-> RPC <-> MBED <-> I2C project issue (also, has anyone used RPCI2C classes successfully?)

I've been working hard to get the ADS1115 library to call I2C over RPC and to control the RPC via python with the KLM25Z. The implementation 'works', but it's kind of cludgy. One thing I've noticed is that every other RPC call returns a null value. I'm not sure what's causing this.

In this case, I had to modify the ADS1115 Hello World headers to instantiate at the different addresses for four adc breakouts and cloned the ADS1115 class to represent the four different addresses.

MBED_RPC_SERIAL and ADS1115 HELLO WORLD

#include "mbed.h"
#include "mbed.h"
#include "mbed_rpc.h"
#include "Adafruit_ADS1015.h"

RpcDigitalOut Blue(LED4,"Blue");
RpcDigitalOut Red(LED1, "Red");
RpcDigitalOut Green(LED2,"Green");
I2C i2c(PTE0, PTE1);
Serial pc(USBTX, USBRX);
int reading;
RPCVariable<int> rpcreading(&reading, "reading");
Adafruit_ADS1115GND adsgnd(&i2c);
Adafruit_ADS1115SCL adsscl(&i2c);
Adafruit_ADS1115VDD adsvdd(&i2c);
Adafruit_ADS1115SDA adssda(&i2c);

//Read Ground address
void ADSREAD23GND(Arguments *input, Reply *output);
RPCFunction rpcReadGND23(&ADSREAD23GND, "ADSREAD23GND");
void ADSREAD23GND(Arguments *input, Reply *output)   {
    
    reading = adsgnd.readADC_Differential_2_3();
    pc.printf("diff a2, a3: %d\r\n", reading);
}
void ADSREAD01GND(Arguments *input, Reply *output);
RPCFunction rpcReadGND01(&ADSREAD01GND, "ADSREAD01GND");
void ADSREAD01GND(Arguments *input, Reply *output)   {
    
    reading = adsgnd.readADC_Differential_0_1();
}

//Read SCL address
void ADSREAD23SCL(Arguments *input, Reply *output);
RPCFunction rpcReadSCL23(&ADSREAD23SCL, "ADSREAD23SCL");
void ADSREAD23SCL(Arguments *input, Reply *output)   {
    
   reading = adsscl.readADC_Differential_2_3();
}
void ADSREAD01SCL(Arguments *input, Reply *output);
RPCFunction rpcReadSCL01(&ADSREAD01SCL, "ADSREAD01SCL");
void ADSREAD01SCL(Arguments *input, Reply *output)   {
    
   reading = adsscl.readADC_Differential_0_1();
}

//Read VDD address
void ADSREAD23VDD(Arguments *input, Reply *output);
RPCFunction rpcReadVDD23(&ADSREAD23VDD, "ADSREAD23VDD");
void ADSREAD23VDD(Arguments *input, Reply *output)   {
    
   reading = adsvdd.readADC_Differential_2_3();
}
void ADSREAD01VDD(Arguments *input, Reply *output);
RPCFunction rpcReadVDD01(&ADSREAD01VDD, "ADSREAD01VDD");
void ADSREAD01VDD(Arguments *input, Reply *output)   {
    
   reading = adsvdd.readADC_Differential_0_1();
}
//Read SDA address
void ADSREAD23SDA(Arguments *input, Reply *output);
RPCFunction rpcReadSDA23(&ADSREAD23SDA, "ADSREAD23SDA");
void ADSREAD23SDA(Arguments *input, Reply *output)   {
    
    adssda.readADC_Differential_2_3();
}
void ADSREAD01SDA(Arguments *input, Reply *output);
RPCFunction rpcReadSDA01(&ADSREAD01SDA, "ADSREAD01SDA");
void ADSREAD01SDA(Arguments *input, Reply *output)   {
    
    reading = adssda.readADC_Differential_0_1();
}

      
int main() {
    //The mbed RPC classes are now wrapped to create an RPC enabled version - see RpcClasses.h so don't add to base class
    
    // receive commands, and send back the responses
    char buf[256], outbuf[256];
    while(1) {
        pc.gets(buf, 256);
        //Call the static call method on the RPC class
        RPC::call(buf, outbuf); 
        pc.printf("%s\n", outbuf);
    }
}

I'm using the mbedRPY.py script provided on this website. Make sure to chmod the /dev/ttyACM* to user before playing

RPC via interactive Python console

from mbedRPC import *
from time import sleep
import numpy as np

serdev = '/dev/ttyACM0'
mbed = SerialRPC(serdev, 9600)
x = DigitalOut(mbed, "Green")
y = DigitalOut(mbed, "Red")
z = DigitalOut(mbed, "Blue")
b = RPCFunction(mbed, "ADSREAD23GND")
c = RPCVariable(mbed,"reading")
d = []
# one may now run the desired adc function via b.run('').  One may save that value to a list with d.append(c.read())

#a quick test is in order - blink the lights in random colors and collect thermocouple data:
while True:
     x.write(np.random.randint(0,2))
     y.write(np.random.randint(0,2))
     z.write(np.random.randint(0,2))
     b.run('')
     d.append(c.read())
     print(d)
     sleep(np.random.randint(1,10))


that said, I end up with every other read/write cycle being null. Here's the output of 'd':

truncated output of above python while

'diff a2, a3: -4', '', 'diff a2, a3: -4', '', 'diff a2, a3: 0', '', 'diff a2, a3: 2', '', 'diff a2, a3: 0', '', 'diff a2, a3: -2', '', 'diff a2, a3: -2', ''']

So I'm not sure what's going on here. Is the RPC channel that slow or are my pull-up resistors too weak (2.5k)? If you have any advice, I sincerely appreciate it.

Photos of the setup follow: /media/uploads/klobas/2016-03-29_18.58.13.jpg /media/uploads/klobas/2016-03-29_18.54.49.jpg /media/uploads/klobas/2016-03-29_20.20.14.jpg

Comment on this question
Be the first to answer this question.

To post an answer, please log in.