flyingeinstein / tread-station

Wirelessly control your treadmill, replace the old lackluster dashboard with graphics and workout analytics! Replace the dashboard with a monitor and add a small computer and you have a Treadmill Workstation.
MIT License
12 stars 2 forks source link

PWM Driver on Nordictrack C2450 #3

Open zindine opened 5 years ago

zindine commented 5 years ago

Hello there,

You might have seen a lot of effort toward interfacing treadmill with Zwift happening out there, one of them is Zwifit (https://github.com/dawsontoth/zwifit). Do you think it would be doable to access the PWM driver right on the treadmill through an Android app? I would then spit the readings directly through a TCP stream or even better through the Bluetooth chip that most newer Nordictrack have. I am not familiar with how the PWM driver works on Linux, so any pointer would be appreciated. Thanks a lot.

guru-florida commented 5 years ago

Zwift seems cool and yet very expensive at $15/mo. I am not really sure what you are asking exactly. Do you mean can you control TreadStation from an Android app? Specificallythe PWM part within TreadStation? The short answer is Yes.

The PWM driver mentioned is not an OS driver, but TreadStation piece of code (I just call them drivers too). The PWM driver being used in TreadStation is hardware specific to what's in the BeagleBoardBlack (BBB) and yes there is a driver there too. The BBB sends the PWM to the motor control board as the old treadmill console used to do, that old console was ripped off and garbaged. With TreadStation installed on a BBB and wired to a NordicTrack motor board you could then send PWM commands to TreadStation from an Android app...that's easy. TreadStation has a Rest API, this is how the UI part of TreadStation controls the treadmill core service. Any app can send WebSocket API commands, including Java, Swift/ObjC, javascript, whatever.

The chain of execution for a speed update would be something like this: (Browser - any computer) TreadStation-UI => WebSockets Http API => (BBB) ... TreadStation-Service (NodeJS) => PostalJS => MotionControl driver (javascript) => PWM driver (javascript) => BBB PWM driver (OS/Linux) => Nordic Motor Control Board

Other Linux boards such as RPi could also work, but the PWM driver would need to be copied and modified for RPi hardware. TreadStation can probe and detect and will only keep drivers loaded that return success. If no PWM driver loads then TreadStation enters simulation mode by activating a sim driver. So install TreadStation on a normal Linux box first, this is a great way to develop as the sim behaves as a virtual treadmill.

You could control the PWM driver inside TreadStation but for the sake of intersecting with Zwift this is not necessary. Just send speed and incline commands, and query speed back and let TreadStation handle the details of PWM/etc. If you use your browser to inspect the network traffic you'd see how the UI does it and they are simple Json request/responses.

You can also send or subscribe to messages from the TreadStation drivers as well. In this way, you could control/query the PWM driver specifically but like I say this is probably not the way to go just for Zwift interfacing.

If the goal is Zwift it could be interfaced with tread-station, probably with minimal effort. Looking at the Zwifit source code as an example I see how it is broadcasting the Treadmill status out of bluetooth and given that Zwifit is a NodeJS project like TreadStation we could adapt that code into a new TreadStation driver. On load, TreadStation would see the driver, load it an call probe() on our driver. If probe() finds a bluetooth module then it activates and returns probe success. The driver would subscribe to speed or other updates and broadcast out just like Zwifit does. If the driver receives a speed/incline command via bluetooth it would send that command to the MotionControl driveras the UI does (which then computes the new PWM values). You can still have the TreadStation UI active too, it can all work together.

I read and watches some youtube vids, but TBH I am not sure I see the value in zwift for treadmilling, especially since at 5-8mph the virtual scenery is not exactly going to be going by fast. Maybe need a speed multiplier. yeah! I see the value in social biking. Could you explain what you get out of it? I am genuinely curious.

guru-florida commented 5 years ago

FYI I made a mistake above so post has been edited...it's not Rest API calls but Websocket. Websocket provides a better real-time always-connected connection to the TreadStation core. Doesnt change anything, you can still control from Java, Android or whatever.

zindine commented 5 years ago

I will try to answer your last question first, running on Zwift at the moment is free (only riding has a cost) and workout integration is really impressive (you get simple instructions on screen with targets and such). It's simply another motivator during the long winter months. :-)

What I was trying to get at is to simply use the Android tablet that is built into the treadmill to broadcast through TCP or BLE the current state of the treadmill (speed and incline). I assumed they were driving the motor controller the same way you were in your own application. But I might be wrong. Thanks for taking the time to answer. It's appreciated.

guru-florida commented 5 years ago

I may give integration with Zwift a try in the next few weeks then. I just got my treadmill unpacked from reno after a long hiatus. I'll be looking to spin it up again and get back on the black path to nowhere. lol.

I am not familiar with the new Nordics that I guess have Android built into them. That's neat. So are you able to access or jailbreak (I suppose) these treadmills? I would think you'd need to do some heavy reverse engineering to figure out how they are controlling the PWM on their hardware. I doubt it looks like BBB hardware though you never know. I'd first look for /sys/class/pwm directory. You might find the PWM nodes there. If so, you could read the PWM from there but controlling it might be a problem since Nordic's app will soon overwrite what you wrote there. There is probably some other interface Nordic implemented you might be able to spoof such as the Wifit, etc. Like Zwifit does, maybe you can send the BT messages out on the Nordic and it will loopback, not sure it would but maybe.

zindine commented 5 years ago

There is no need to really jailbreak. There is a startup procedure that launch the iFit app automatically. There is a rudimentary OS layer underneath. I forgot how exactly I did it back then, but you can access the launcher by pressing the screen at the right time. Then install aptoide or your own application from a USB stick. I ended installing a task switcher that leaves an icon on the screen at all time. Attached here is the listing of the PWM folder. image

guru-florida commented 5 years ago

That's good news! Looks like they are using proper linux PWM drivers so regardless of what the underlying PWM hardware is the control should be the same. Look into each of the pwm.x folders. Turn on the treadmill to slow. Then inspect the files within those dirs. You should be able to cat a duty, period, etc files in there and get values. The duty is probably represented as a number between 0 and up to the period amount. The PWM folder in Linux has undergone a lot of changes in the past 4 years...it's been very frustrating to keep up with the kernel devs on that (related to Linux Device Tree). So I cant say exactly what state your PWM dirs are in but as it is in sys/devices/platform I would say it is at least 2 years ago. Another thing, some of the directories in sys/devices are just device info and not control. So you may need to do a find /sys -name "pwm.?" to get all occurrences.

guru-florida commented 5 years ago

FYI I am attaching all my notes here. There is a lot that would be unrelated to you but there may be some nuggets of info in them related to PWM control. NordictrackNotes.pdf