0

We are using MyoWare EMG sensor and reading the RAW EMG pin as well as filtered SIG pin on the board. What it basically does is sends an analog value as a fraction of the input Voltage, which in this case is 3.3 Volts from the Teensy 3.2. I chose the Teensy 3.2 because it has two independent ADC's and using Pedvide's ADC library, I am able to read from one pin on each ADC in a continuous synchronized read in under 25 microseconds. Using the analogRead() function on Arduino IDE leads to square wave data, which is lossy. I chose the USB serial protocol because we have a 2-3ft cable and SPI or I2C is not recommended for distances that long. When the Teensy is plugged into the PC, and a python script reads serial data over USB exactly as it is supposed to read on the Pi, the script runs fine, reads all the serial, prints it, and saves it to a text file. But when I use a Raspberry Pi 3, the pi reads like 30 lines of serial and then just stops reading. In the simpler readserial version of the code, it was reading and then printing 20-30 lines off serial with 20 microseconds timestamp between each line, and then printing another bunch of lines off of serial but the time stamp was ~2,000,000 microseconds after the last read line, and then the same 20 microseconds difference between each line. I also tried the analogRead() function and the result was the same. So, it's not the Arduino. It's the pi. We read several posts over stackexchange here, on Arduino forums, Reddit, etc., and modified the code accordingly, but it's still giving the same issue or not printing anything at all. What I'd like to know is how to read all that data from Serial sent by Teensy without losses. Here is the current code on Arduino:

    #include <ADC.h>

    const int readPin = A1;
    const int readPin2 = A20;

    ADC *adc = new ADC(); // adc object

    void setup() {
        pinMode(readPin, INPUT);
        pinMode(readPin2, INPUT);

        Serial.begin(9600);

    ///// ADC0 ////

        adc->setAveraging(3); // set number of averages
        adc->setResolution(16); // set bits of resolution
        adc->setConversionSpeed(ADC_CONVERSION_SPEED::VERY_HIGH_SPEED); // change the conversion speed
        adc->setSamplingSpeed(ADC_SAMPLING_SPEED::VERY_HIGH_SPEED); // change the sampling speed

    ////// ADC1 /////
        adc->setAveraging(3, ADC_1); // set number of averages
        adc->setResolution(16, ADC_1); // set bits of resolution
        adc->setConversionSpeed(ADC_CONVERSION_SPEED::VERY_HIGH_SPEED, ADC_1); // change the conversion speed
        adc->setSamplingSpeed(ADC_SAMPLING_SPEED::VERY_HIGH_SPEED, ADC_1); // change the sampling speed

        adc->startSynchronizedContinuous(readPin, readPin2);
        delay(100);
        Serial.println("end setup");
    }

    int value = 0;
    int value2 = 0;
    int tic=0;
    int toc=0;

    ADC::Sync_result result;

    void loop() {
        tic=micros();
        result = adc->readSynchronizedContinuous();

        result.result_adc0 = (uint16_t)result.result_adc0;
        result.result_adc1 = (uint16_t)result.result_adc1;
        toc=micros();

  //    Serial.print(tic);
 //     Serial.print(F(" "));
        Serial.print(result.result_adc0);
        Serial.print(F(" "));
        Serial.println(result.result_adc1);

       //Serial.print(F(" "));
       // Serial.println(toc-tic);

    // Print errors, if any.
        adc->printError();

        delay(10); //changing this delay does not help
    }


Here is the Python code on the raspberry pi end. It has been edited through several sources both on this stackexchange, reddit and Arduino, so if someone recognizes their bits of code, thank you so much for helping


    import serial
    import time
    def serial_com():
        serial_port=serial.Serial(port='/dev/ttyACM0', baudarate=9600, timeout=1)
    lines = []

    def serial_com()  
        while True:
            line = serial_port.readline()
            lines.append(line.decode('utf-8').rstrip()
            print(line)
            print('In loop')
            timeout = time.time() + 0.1
            print(serial_port.inWaiting())
            while not serial_port.inWaiting() and timeout > time.time():
                pass
        serial_port.close()
        return lines

    lines = serial_com()
    print(lines)
    lines[-1]
  • Well, Rpi UART at 9600 baud should have no transmission problems. A possible problem is your "print lines" which jams the traffic. One get around is to store the readings in memory or file, and print them later. – tlfong01 Jul 20 '19 at 0:57
  • Make a teensy program which just prints lines and nothing else, and check if you can reproduce the issue. – Dmitry Grigoryev Jul 21 '19 at 8:10
  • @tlfong01 thanks for the tip. how do it do that? – Yusra Jul 22 '19 at 16:39
  • @Yursa, you mean how to write to a text file? See: (1) raspberrypi.stackexchange.com/questions/96716/… , (2) penzu.com/public/80c1c577 – tlfong01 Jul 23 '19 at 1:55
  • @Yursa, if writing to a text file still jams traffic, you can write to a python list, which should be fast enough. – tlfong01 Jul 23 '19 at 2:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.