Home>

Currently, I am conducting research to measure myoelectric potential using rasberry pi, myoware and mcp3204.
I'm thinking of creating a 1000x24 table by building a program like the one below and saving data each time I press a letter and enter. The ideal table is also shown below. With the current program, the data of syakkutu will be saved at the end and the table will be a table only for syakkutu. I want to make a 1000x24 table by connecting these horizontally in the order of weakness, glassing, opening, syoukutu, haikutu, syakutu, respectively. I would like to ask for any advice or improvements.

import wiringpi as pi
import time
import numpy as np
import openpyxl as excel
import spidev
import threading
import pandas as pd
SPI_CE = 0
SPI_SPEED = 5000000
READ_CH = 0
VREF = 5
spi = spidev.SpiDev ()
spi.open (0,0)
spi.max_speed_hz = SPI_SPEED
during_time = 0.5
sampling_rate = 2000
sampling_num = int (during_time * sampling_rate)
sampling_interval = 1/sampling_rate
x = np.arange (0, sampling_num/sampling_rate, sampling_interval)
y = np.zeros (sampling_num)
def main ():
    print ('Start measurement')
    for i in ['weakness','grasping','opening','syoukutu','haikutu','syakkutu']:
        print ("{}: measurement" .format (i))
        toriga = input ()
        if toriga:
            MEASURE = measure ()
            data = MEASURE.keisoku ()
            print (data)
            pd.DataFrame (np.array (data)). T.to_csv ('data.csv', index = False, header = False)
        print ("{}: End of measurement" .format (i))
class measure (object):
    def __init __ (self):
        self.data = []
        self.data1 = []
        self.data2 = []
        self.data3 = []
        self.Vi = []
        self.Vi1 = []
        self.Vi2 = []
        self.Vi3 = []

    def do_sampling (self, i, time_start): # Voltage acquisition
        y [i] = time.perf_counter () --time_start
    def read_ch0 (self):
        adc = spi.xfer2 ([6,0,0])
        data = ((adc [1]&15)<<8) + adc [2]
        self.data = data
        #print ('channel0')
        return
    def read_ch1 (self):
        adc = spi.xfer2 ([6,0x40,0])
        data = ((adc [1]&15)<<8) + adc [2]
        self.data1 = data
        #print ('channel1')
        return
    def read_ch2 (self):
        adc = spi.xfer2 ([6,0x80,0])
        data = ((adc [1]&15)<<8) + adc [2]
        self.data2 = data
        #print ('channel2')
        return
    def read_ch3 (self):
        adc = spi.xfer2 ([6,0xc0,0])
        data = ((adc [1]&15)<<8) + adc [2]
        self.data3 = data
        #print ('channel3')
        return
    def keisoku (self):
        count = 0
        time_start = time.perf_counter ()
        time_next = time_start
        Vi = self.Vi
        Vi1 = self.Vi1
        Vi2 = self.Vi2
        Vi3 = self.Vi3
        while count<sampling_num:
            self.do_sampling (count, time_start)
            time_next + = sampling_interval
            thread1 = threading.Thread (target = self.read_ch0)
            thread2 = threading.Thread (target = self.read_ch1)
            thread3 = threading.Thread (target = self.read_ch2)
            thread4 = threading.Thread (target = self.read_ch3)
            thread1.start ()
            thread2.start ()
            thread3.start ()
            thread4.start ()
            Vi.append (self.data)
            Vi1.append (self.data1)
            Vi2.append (self.data2)
            Vi3.append (self.data3)
            while (time.perf_counter ()<time_next): # Wait until the next time.
                pass
            count = count + 1
        return Vi, Vi1, Vi2, Vi3

if __name__=='__main__':
    main ()

]
(3d813534dc1c47f65e28c9ffb80ad9b5.png)

  • Answer # 1

    In the attached source code,
    Since data.csv is newly created and overwritten each time one measurement is performed, only the data of the last measurement remains.
    If you collect the data once and then output it to a file as shown below, it will work as intended.

    def main ():
        print ('Start measurement')
        all_data = []
        for i in ['weakness','grasping','opening','syoukutu','haikutu','syakkutu']:
            print ("{}: measurement" .format (i))
            toriga = input ()
            if toriga:
                MEASURE = measure ()
                data = MEASURE.keisoku ()
                print (data)
                all_data.extend (data)
            print ("{}: End of measurement" .format (i))
        pd.DataFrame (all_data) .T.to_csv ('data.csv', index = False, header = False)