Dsissitka, I need your assistance again.
It has been about a month since you got a base program working for me, and I haven't had too much time to play since then. Ironically because I have been spending all my time on the ergo. Gave myself a silly 310km in December challenge which I have thankfully finished tonight. I have however spent some time learning python via "Learn Python the hard way".
Anyway....I managed to get somewhere with my program. It looks pretty awful, and is still super simple, but at least it is doing something. It takes 1 reading a second and outputs it to a text file, hurrah. I am planning on putting more readings into the text file, and changing it from once a second to once a stroke, but I haven't had the time to experiment on the ergo to find out what readings I need to take, what with all the ergo-ing I have been doing.
My hugely untidy code so far
. The business end is at the bottom. There are a couple of sections in there not being used yet, because it is a work in progress.
Code: Select all
import os
import usb
import sys
import time
device = usb.core.find(idVendor=6052)
if not device:
print "No rowing machine found."
sys.exit(1)
try:
configuration = device[0]
interface = configuration[(0, 0)]
inEndpoint = interface[0]
outEndpoint = interface[1]
if device.is_kernel_driver_active(0):
device.detach_kernel_driver(0)
except:
print "Unknown error."
sys.exit(1)
def metres():
data = [
1, # Report ID
241, # Standard Frame Start Flag
161, # Command
161, # Checksum
242, # Frame Stop Flag
# Padding. Standard frames are 21 bytes.
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
]
device.write(outEndpoint.bEndpointAddress, data)
response = device.read(inEndpoint.bEndpointAddress, inEndpoint.wMaxPacketSize)
return response[5]+response[6]*255
def rowTime_Arr():
data = [
1, # Report ID
241, # Standard Frame Start Flag
160, # Command
160, # Checksum
242, # Frame Stop Flag
# Padding. Standard frames are 21 bytes.
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
]
device.write(outEndpoint.bEndpointAddress, data)
response = device.read(inEndpoint.bEndpointAddress, inEndpoint.wMaxPacketSize)
return response
def rowTime_Sec():
Time_Arr = rowTime_Arr()
return Time_Arr[5]*3600.0+Time_Arr[6]*60.0+Time_Arr[7]+Time_Arr[8]/255.0
def Workout_Start():
Time_Arr = rowTime_Arr()
if Time_Arr[2] == 9:
return "Start"
else:
return "wait"
def Machine_State():
data = [
1, # Report ID
241, # Standard Frame Start Flag
128, # Command
128, # Checksum
242, # Frame Stop Flag
# Padding. Standard frames are 21 bytes.
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
]
device.write(outEndpoint.bEndpointAddress, data)
response = device.read(inEndpoint.bEndpointAddress, inEndpoint.wMaxPacketSize)
return response
def workoutType():
if device.is_kernel_driver_active(0):
device.detach_kernel_driver(0)
data = [
1, # Report ID
241, # Standard Frame Start Flag
137, # Command
137, # Checksum
242, # Frame Stop Flag
# Padding. Standard frames are 21 bytes.
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
]
device.write(outEndpoint.bEndpointAddress, data)
return device.read(inEndpoint.bEndpointAddress, inEndpoint.wMaxPacketSize)
def Create_File():
Date_Time = (time.strftime("%Y%m%d_%H%M%S"))
File_Name = "RowLog_%s.txt" % Date_Time
file = open(File_Name, 'w')
file.write('Loop,Distance,Time\n')
def rowstats():
# Create_File()
Date_Time = (time.strftime("%Y%m%d_%H%M%S"))
File_Name = "RowLog_%s.txt" % Date_Time
file = open(File_Name, 'w')
file.write('Loop,Distance,Time\n')
print "File Created. Waiting to Start"
while Workout_Start() == "wait":
pass
for i in xrange(120):
Workout_info = str(i) + ',' + str(metres()) + ',' + str(rowTime_Sec())
print Workout_info
file.write(Workout_info + '\n')
time.sleep(1)
file.close()
Incidentally, have I missed something obvious. In the SDK there is a breakdown of all the commands you can ask, but there doesn't seem to be a breakdown of all the replies that can be given. I.e. In your first example to me, you showed machine state, and when playing with that I got various numbers back, but had to guess what they meant depending on what I happened to be doing on the rowing machine at the time, or in the case of horizontal distance, it says one of the numbers is the units, but what specific number represents metres, or Km, or miles or whatever? I presume even if it is trial and error to find these answers, everyone else here must have already been through it, so is there a library somewhere of responses for each command?
If not, I can do it myself manually hopefully, now I have some time to experiment.
Anyway, my master plan at the start of all this, was to write the output to a text file (check), and then display that output on a graph (still outstanding). This is where I am currently stuck, and it is more of a python/ubuntu problem than a Concept2 problem so if I need to head off to a python forum please tell me, but having googled for a bit, I am going to need some serious dumbing down of answers if I try my luck there. I found a youtube video of how to display a graph from a csv file using matplotlib. This is the code:
Code: Select all
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
def graph():
Row_Count, Distance, Time = np.loadtxt('RowLog_20141224_151056.txt', delimiter=',', unpack=True)
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1, axisbg='white')
plt.plot(x=Time, y = Distance, fmt='-')
plt.title('title')
plt.ylabel('Distance')
plt.xlabel('Time')
plt.show
graph()
Which all looks pretty simple, but when running it fails on the import of numpy.
I think the problem is due to numpy being installed on Python2.7.8, but my program using the default python which came with ubuntu (at a guess). When I try to install numpy it says it is already installed. When I ran "dpkg -L python-numpy" it printed a whole load of files stored in "/usr/lib/python2.7/xxxxxx" which looks right. But when I run "type python" the response is "/usr/local/bin/python". Do you think that the problem that python is defaulting to "/usr/local/bin" rather than "/usr/lib/" ?
And as a secondary question, do you think this is the best way to attack the problem, given I will ultimately want a csv of my previous row, and the stats from my current row in real time to be plotted on the same graph? Matplotlib looks to be the way to do it, if I could get it working.
Thank you again for getting me the leg up I needed to get this far.
Happy new year.
Phil