ev3dev / ev3dev-lang-python

Pure python bindings for ev3dev
MIT License
425 stars 144 forks source link

csv() compatibility with ev3dev2 #685

Closed naynt closed 4 years ago

naynt commented 4 years ago

Hello,

Aim: To collect data from EV3 and NXT sensors via ev3dev2 and export to csv file

Problem: While running script via ev3.dev2 python, csv file fails to generate despite no errors being received.

Example1

#!/usr/bin/env python3 

from ev3dev2.sensor.lego import Sensor
from sys import stderr
from time import sleep
from ev3dev2.sensor import INPUT_1,INPUT_2
import csv
from datetime import datetime

temp = Sensor(INPUT_1)
temp.mode = 'TEMP'
x = temp.value(0)

pressure = Sensor(INPUT_1)
pressure.mode = 'PRESS'
y = pressure.value(0)

temp = Sensor(INPUT_2)
temp.mode = 'NXT-TEMP-C'
z = temp.value(0)
start_time = datetime.now()

count=0

with open('data.csv','a',newline='') as csvfile:
    data_file = csv.writer(csvfile,delimiter = ' ')
    data_file.writerow(['Temp1','Temp2','Pressure','time'])

    while True:
        count = count + 1
        time_elapsed = datetime.now() - start_time  

        print('Temp:',x/1000,'or',z/10,'Degrees Celcius','---','Barometric Pressure:',(y/10)*25.4,'mmHg','---', end=' ',file = stderr)
        print('Time: (hh:mm:ss) {}'.format(time_elapsed),file=stderr)

        data_file.writerow([x,z,y,time_elapsed])

        sleep(1)

        if count == 6:
            break

output:

Starting: brickrun --directory="/home/robot/Lone Script" "/home/robot/Lone Script/NXT Barometer and Temp.py"
Started.
----------
Temp: 28.366 or 24.8 Degrees Celcius --- Barometric Pressure: 660.4 mmHg --- Time: (hh:mm:ss) 0:00:00.020213
Temp: 28.366 or 24.8 Degrees Celcius --- Barometric Pressure: 660.4 mmHg --- Time: (hh:mm:ss) 0:00:01.029985
Temp: 28.366 or 24.8 Degrees Celcius --- Barometric Pressure: 660.4 mmHg --- Time: (hh:mm:ss) 0:00:02.040378
Temp: 28.366 or 24.8 Degrees Celcius --- Barometric Pressure: 660.4 mmHg --- Time: (hh:mm:ss) 0:00:03.049831
Temp: 28.366 or 24.8 Degrees Celcius --- Barometric Pressure: 660.4 mmHg --- Time: (hh:mm:ss) 0:00:04.058387
Temp: 28.366 or 24.8 Degrees Celcius --- Barometric Pressure: 660.4 mmHg --- Time: (hh:mm:ss) 0:00:05.067629
----------
Completed successfully.

However no csv file is generated

Example 2 - SIMPLIFIED

#!/usr/bin/env python3 

from ev3dev2.sensor import INPUT_1,INPUT_2
from time import sleep
import csv
from datetime import datetime
from sys import stderr

x = 35
y = 35
z = 500

count=0

with open('data.csv','a',newline='') as csvfile:
    data_file = csv.writer(csvfile,delimiter = ' ')
    data_file.writerow(['Temp1','Temp2','Pressure'])

    while True:
        count = count + 1
        print('Temp:',x,'or',z,'Degrees Celcius','---','Barometric Pressure:',y,'mmHg','---', end=' ',file = stderr)
        data_file.writerow([x,z,y])

        sleep(1)

        if count == 6:
            break

OUTPUT

The same above, but with different values and no CSV file is generated.

When I attempted to run the above script in python by itself (not via ev3dev2), a csv file was generated.

Question

Is their anyway I can successfully create a csv file within the ev3dev2 framework?

Windows 10 Visual Studio Code with ev3.dev device browser extension (August 2019 version 1.38) Image file: ev3dev-stretch-ev3-generic-2019-03-03 Kernel version: 4.14.96-ev3dev-2.3.2-ev3 Brickman v0.10.0

ddemidov commented 4 years ago

What happens if you add

import os
print('CWD:', os.getcwd())

to your script? Is the working directory the one you expected it to be? Your csv files most probably end up there. You could try to specify full path for your output files (or use __file__ to put them near your script).

naynt commented 4 years ago

I see. The csv file is being sent to /home/robot (the brick) instead of my PC.

dlech commented 4 years ago

You can use the ev3dev device browser in VS Code to upload it back to your computer.

Selection_005

naynt commented 4 years ago

Awesome, thanks for the help guys!