Arduino and Freepie ?

Official forum for open source FreePIE discussion and development.
NeoTokyo_Nori
Cross Eyed!
Posts: 144
Joined: Wed Jul 16, 2014 10:29 am
Location: Tokyo, Japan

Arduino and Freepie ?

Post by NeoTokyo_Nori »

Hi, I would like to use arduino with Freepie in my project,
(to send imu data along with button IO events over bluetooth)

But to my surprise I have not seen anything that mentions arduino in the Freepie documents or forums.
Am I just missing something? or is arduino not implemented (yet)

If not, is there still a way to do it? (with UDP packets or something)

cheers!
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Arduino and Freepie ?

Post by CyberVillain »

We dont have any raw Arduino data plugin. Only specific Arduino library implementations. Namly ahrs and FreeIMu

If you dont want to roll your own plugin you can read the data off the serial port using python (The scripting language of FreePIE)

http://playground.arduino.cc/interfacing/python
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Arduino and Freepie ?

Post by CyberVillain »

btw, the easisest way is probaly to use .NET's own serial port client
From the script do

Code: Select all

from System.IO.Ports import SerialPort 
Now you can access the SerialPort class, here is the doc for it

http://msdn.microsoft.com/en-us/library ... .110).aspx
NeoTokyo_Nori
Cross Eyed!
Posts: 144
Joined: Wed Jul 16, 2014 10:29 am
Location: Tokyo, Japan

Re: Arduino and Freepie ?

Post by NeoTokyo_Nori »

Thanks CyberVillan

So, it looks like reading the data off the serial port using python, is the way to go for now..

I do not have the skills to write a plugin at this point in time,
but hope a plugin can be worked on since arduino has so many potential applications,
and I will contribute what I can in the event that it does happen!

cheers!
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Arduino and Freepie ?

Post by CyberVillain »

A general arduino plugin would just be a generic Serial plugin I guess that pretty muchs wraps the SerialPort anyway
NeoTokyo_Nori
Cross Eyed!
Posts: 144
Joined: Wed Jul 16, 2014 10:29 am
Location: Tokyo, Japan

Re: Arduino and Freepie ?

Post by NeoTokyo_Nori »

So are you saying that it would be easy for me/someone to do?
Or that it would be easy for you to do??? wink wink ; )
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Arduino and Freepie ?

Post by CyberVillain »

No, im just saying that a generic Serial port plugin built into FreePIE wont be much easier than using the SerialPort class directly from python.
Give it a try, write a string on your Arduino's serial port and try fething it in FreePIE using the code I gave you below
NeoTokyo_Nori
Cross Eyed!
Posts: 144
Joined: Wed Jul 16, 2014 10:29 am
Location: Tokyo, Japan

Re: Arduino and Freepie ?

Post by NeoTokyo_Nori »

Ok, I will look into it.
But I should give warning that I have no experience with .net.
and also kind of allergic to C variants : /

Anyway, to make sure that I understand you:
Should I put this code inside the FREEPIE script ?

.NET's own serial port client

Code: Select all

from System.IO.Ports import SerialPort
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Arduino and Freepie ?

Post by CyberVillain »

Yes, thats the syntax to import modules in Python

Once that is done you can create a serial port instance like this

Code: Select all

if starting:
   port = SerialPort("com1", 57600)
   port.Open()
NeoTokyo_Nori
Cross Eyed!
Posts: 144
Joined: Wed Jul 16, 2014 10:29 am
Location: Tokyo, Japan

Re: Arduino and Freepie ?

Post by NeoTokyo_Nori »

Hi, I put that code in the freepie script,
and it runs without error for the first time,
but the next time the same comport cannot be accessed,
so the port probably has to be closed when ending right?

I tried this but did not work.

Code: Select all

if ending:
   port.Close()
I was also surprised to find that
if starting:
is not defined in the the python language, or in the freepie reference,
so some documentation may be needed for it too?


BTW, I am wondering if this pySerial library would make things easier.
http://playground.arduino.cc/interfacing/python

cheers!
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Arduino and Freepie ?

Post by CyberVillain »

Yeah, i know neither the starting or stopping booleans are in the code completion. Its on my list

So change to if stopping
NeoTokyo_Nori
Cross Eyed!
Posts: 144
Joined: Wed Jul 16, 2014 10:29 am
Location: Tokyo, Japan

Re: Arduino and Freepie ?

Post by NeoTokyo_Nori »

Wow, it really is a festival over on reddit isn`t it :lol:
I doubt there has ever been a product in history like oculus where people are just throwing money at their screens and demanding they have it now! It`s probably more so than the iphone was.
I am not expecting my notification for a couple of days, but it`s pretty hard to NOT keep checking all the things :D

Anyway to get back on topic:
I have run into the problem that the same port that arduino is using to send the serial data
cannot be opened from freepie at the same time, so I obviously I cannot read the serial data from arduino, doh!

This is the code I am testing it with so far.

Code: Select all

def readSerialPort():
    serialInputLine = port.ReadLine()

def closeSerialPort():
	port.Close()

if starting:
   system.setThreadTiming(TimingTypes.HighresSystemTimer)
   system.threadExecutionInterval = 2
   
   global port
   port = SerialPort("com18", 115200)
   port.Open()
   port.DataReceived += readSerialPort
   port.ErrorReceived += closeSerialPort
 


Looks like that`s as far as I get for tonight!
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Arduino and Freepie ?

Post by CyberVillain »

Do you close any other program thats reading from the port like the Arduino IDE
NeoTokyo_Nori
Cross Eyed!
Posts: 144
Joined: Wed Jul 16, 2014 10:29 am
Location: Tokyo, Japan

Re: Arduino and Freepie ?

Post by NeoTokyo_Nori »

Ha! you were right, closing the Arduino IDE solved it!
I would have never thought of doing that myself, so thanks.

diagnostics.watch(port.ReadLine)
is only giving something like
ironPython.runtime..
for now..
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Arduino and Freepie ?

Post by CyberVillain »

Hmm, ReadLine will halt the script thread which will make FreePIE angry. Cant you read it async instead?
NeoTokyo_Nori
Cross Eyed!
Posts: 144
Joined: Wed Jul 16, 2014 10:29 am
Location: Tokyo, Japan

Re: Arduino and Freepie ?

Post by NeoTokyo_Nori »

Hi there,
Yeah, I wasn`t getting anything so far with that, so I was wondering what to do next...
But I don`t see anything in the .net page about async.
Can you elaborate further? :geek:

edit: may be use SerialPort::ReadTo Method?
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Arduino and Freepie ?

Post by CyberVillain »

Have you tried the datareceived event?

http://msdn.microsoft.com/en-us/library ... .110).aspx
NeoTokyo_Nori
Cross Eyed!
Posts: 144
Joined: Wed Jul 16, 2014 10:29 am
Location: Tokyo, Japan

Re: Arduino and Freepie ?

Post by NeoTokyo_Nori »

Do I need to use the
SerialDataReceivedEventHandler ?

I really don`t understand C++ code very well :roll:
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Arduino and Freepie ?

Post by CyberVillain »

Code: Select all

from System.IO.Ports import SerialPort

def update(s, e):
	global port
	indata = port.ReadExisting();
	diagnostics.watch(indata)

if starting:
	port = SerialPort("COM1")
	port.DataReceived += update
Thats a start
NeoTokyo_Nori
Cross Eyed!
Posts: 144
Joined: Wed Jul 16, 2014 10:29 am
Location: Tokyo, Japan

Re: Arduino and Freepie ?

Post by NeoTokyo_Nori »

Hi, I tried to get that working here, but I see nothing so far :cry:

Code: Select all

from System.IO.Ports import SerialPort

def update(s, e):
   global port
   indata = port.ReadExisting()
   diagnostics.watch(indata)
   diagnostics.debug(indata)
   
if starting:
   system.setThreadTiming(TimingTypes.HighresSystemTimer)
   system.threadExecutionInterval = 2
   
   port = SerialPort("com18", 115200)
   port.Open()
   port.DataReceived += update
Just curious, what arguments is update expecting?

Code: Select all

def update(s, e):
Note:
I should be getting constant Ping data from an ultrasonic sensor on that serial port.
I can see the live serial data on the serial monitor in arduino IDE. like this
Ping: 4cm
Ping: 4cm
Ping: 4cm
Ping: 4cm
Ping: 5cm
Ping: 5cm
Ping: 5cm
Ping: 5cm
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Arduino and Freepie ?

Post by CyberVillain »

I think its better to listen to the event before doing open, try that.. But probaly wont help..

Will have to dig out my ardunio and test, sorry
NeoTokyo_Nori
Cross Eyed!
Posts: 144
Joined: Wed Jul 16, 2014 10:29 am
Location: Tokyo, Japan

Re: Arduino and Freepie ?

Post by NeoTokyo_Nori »

Ok, thanks anyway! I will keep trying.

By the way, are you planing to make the DK2 position data (x,y,z) accessible in freepie??
We gotta get it right?? :woot
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Arduino and Freepie ?

Post by CyberVillain »

It works for me.. Be very carefull though, since the update function is executing outside of the FreePIE sandbox it will hard crash if you do something wrong in that function, so always save script before running so you do not lose work
NeoTokyo_Nori
Cross Eyed!
Posts: 144
Joined: Wed Jul 16, 2014 10:29 am
Location: Tokyo, Japan

Re: Arduino and Freepie ?

Post by NeoTokyo_Nori »

Hello!
I had to spend most of the week dealing with some other arduino related issues,
but I still have not been able to get serial data to show up in freepie.

I got the Razor IMU working, so am trying to get data from that!
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Arduino and Freepie ?

Post by CyberVillain »

But the Razor IMU supports AHRS so you should use the AHRS plugin instead
NeoTokyo_Nori
Cross Eyed!
Posts: 144
Joined: Wed Jul 16, 2014 10:29 am
Location: Tokyo, Japan

Re: Arduino and Freepie ?

Post by NeoTokyo_Nori »

Hi!

As I mentioned in the "Sparkfun Razor IMU - Freepie script" thread,
I have the AHRS IMU data working and showing data in freepie,
So I just need the same serial data to show, using the serial port method in this thread.
But it just will not work for me!

I am using COM19, 57600
and this is working fine

Code: Select all

def update():
   diagnostics.watch(ahrsImu.yaw)

if starting:
   ahrsImu.update += update
But this still gives me nothing, no error message, no data, nothing

Code: Select all

from System.IO.Ports import SerialPort

def update():
#def update(s, e):
   diagnostics.debug("serial data received")
#   indata = port.ReadExisting();
#   diagnostics.debug(indata)

if starting:
   global port
   port = SerialPort("COM19", 57600)
#   port.Open()
   port.DataReceived += update
if I use port.Open(), it crashes...


Would it just be simpler for you to modify AhrsImuPlugin to parse generic serial data?
:geek:
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Arduino and Freepie ?

Post by CyberVillain »

I wipped up something for you to test

Try

Code: Select all

for buffer in genericCom.ReadExisting():
    diagnostics.watch(buffer)
For byte data

And for text

Code: Select all

for text in genericCom.ReadExistingString():
    diagnostics.watch(text)
You do not have the required permissions to view the files attached to this post.
NeoTokyo_Nori
Cross Eyed!
Posts: 144
Joined: Wed Jul 16, 2014 10:29 am
Location: Tokyo, Japan

Re: Arduino and Freepie ?

Post by NeoTokyo_Nori »

Hi cybervillan, thanks!
but you may be going a bit to fast for me...
what should I do with FreePIE.Core.Plugins.dll that you attached :?:

I have tried this with your code :!:

Code: Select all

from System.IO.Ports import SerialPort

def update(s, e):
   for buffer in genericCom.ReadExisting():
      diagnostics.watch(buffer)

   for text in genericCom.ReadExistingString():
      diagnostics.watch(text)

if starting:
   global port
   port = SerialPort("COM19", 9600)
#   port = SerialPort("COM19", 57600)
   port.DataReceived += update
so far I get no errors, but still no data in the watch window... :?
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Arduino and Freepie ?

Post by CyberVillain »

Have you replaced the dll in the plugins folder of FreePIE?
The correct usage is

Code: Select all

def update():
   for text in genericCom.ReadExistingString():
      diagnostics.watch(text)

if starting:
   genericCom.update+= update
Tou cant combine ReadExstingStreing and ReadExsting
NeoTokyo_Nori
Cross Eyed!
Posts: 144
Joined: Wed Jul 16, 2014 10:29 am
Location: Tokyo, Japan

Re: Arduino and Freepie ?

Post by NeoTokyo_Nori »

WOW!! YES!!!
It is working finally :!:

You must be a genius cybervillan :woot

I am not sure if this is something to be concerned about,
but here is what the text looks like in the console window.
Just wondering if those line breaks should be there... :?:

Code: Select all

#YPR=-169.93,-2.7
9,-9.27

#YPR=-169.94,-2.80,-9.26


#YPR=-1
69.94,-2.79,-9.27

#YPR=-169.94,-2.77,-9.
28

#
YPR=-169.95,-2.74,-9.31
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Arduino and Freepie ?

Post by CyberVillain »

Since its generic it wont read until new line. It will read until the next time the script will ask for data.

diagnsotics.debug will add a line

If you want to read a line then you need to ReadExistingString until you find a new line
NeoTokyo_Nori
Cross Eyed!
Posts: 144
Joined: Wed Jul 16, 2014 10:29 am
Location: Tokyo, Japan

Re: Arduino and Freepie ?

Post by NeoTokyo_Nori »

Hi,
just to clarify, this is the code I was using to get the above output :)

Code: Select all

def update():
   for text in genericCom.ReadExistingString():
      diagnostics.watch(text)
      diagnostics.debug(text)
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Arduino and Freepie ?

Post by CyberVillain »

I updated the plugin with a ReadLine function to make it easier, see attached

Code: Select all

def update():
	line = genericCom.ReadLine()
	if line != "":
		diagnostics.debug(line)

if starting:
	genericCom.update += update
You do not have the required permissions to view the files attached to this post.
NeoTokyo_Nori
Cross Eyed!
Posts: 144
Joined: Wed Jul 16, 2014 10:29 am
Location: Tokyo, Japan

Re: Arduino and Freepie ?

Post by NeoTokyo_Nori »

Yes!, the output looks beautiful :!: :D

#YPR=-169.94,-1.54,-7.30
#YPR=-169.94,-1.55,-7.29
#YPR=-169.94,-1.56,-7.28
#YPR=-169.94,-1.55,-7.31
#YPR=-169.94,-1.54,-7.34
#YPR=-169.94,-1.57,-7.37
#YPR=-169.95,-1.56,-7.39
#YPR=-169.94,-1.55,-7.34
#YPR=-169.94,-1.57,-7.33
#YPR=-169.95,-1.60,-7.34
#YPR=-169.95,-1.55,-7.33

:woot
NeoTokyo_Nori
Cross Eyed!
Posts: 144
Joined: Wed Jul 16, 2014 10:29 am
Location: Tokyo, Japan

Re: Arduino and Freepie ?

Post by NeoTokyo_Nori »

Hello CV, The serial data is flowing smooth !
I am glad we now have a new plugin feature for the next version update 8-)

Now, I have to get busy working on the arduino side of things to make all my buttons and sensors to work.

The next step is,
I need to connect at least 2 arduino AHRS IMUs to the pc at the same time, while using the generic serial to send button IO events.
Connection is serial over bluetooth, so com40 and com41 will be used at the same time (for example).

And we would like to have
genericCom[0].update += update
genericCom[1].update += update

I guess the IMU data will have to be handled by the genericCom now
in order to handle the IMU data plus button IO data at the same time...?

(note: the IMUs have to be on separate bluetooth connections, in my case )

I can imagine that people will want to connect several IMUs simultaneously
for things like arms/upper body/full body tracking, so this can have broader applications for freepie users.

I know that the wiimote plugin can handle 2 or more wiimotes. :idea:
I am wondering, do you think it is possible to do the same with the serial com :?: :?:
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Arduino and Freepie ?

Post by CyberVillain »

Yeah, its been on my list. I guess I have to remove the settings and let you create a port from code like

Code: Select all

deviceZero =  genericCom.create(0, 57600)
NeoTokyo_Nori
Cross Eyed!
Posts: 144
Joined: Wed Jul 16, 2014 10:29 am
Location: Tokyo, Japan

Re: Arduino and Freepie ?

Post by NeoTokyo_Nori »

CyberVillain wrote:Yeah, its been on my list. I guess I have to remove the settings and let you create a port from code like

Code: Select all

deviceZero =  genericCom.create(0, 57600)
That woiuld be awesome CV :D :!:

So we can put multiple coms and DK2 position data
as upcomming features in the next update 8-)

These are Exciting times :!:
NeoTokyo_Nori
Cross Eyed!
Posts: 144
Joined: Wed Jul 16, 2014 10:29 am
Location: Tokyo, Japan

Re: Arduino and Freepie ?

Post by NeoTokyo_Nori »

Hello CV, how is it going?

I have not been able to figure out the correct usage for sending keyboard commands.
The reference page gives this:
setKeyDown void setKeyDown (Key key)

But all I get so far are errors, and crashes,

Code: Select all

def update():
   serialMsg = genericCom.ReadLine()
   if serialMsg != "":
#   if serialMsg == "WASD":
      keyboard.setKeyDown(Key w)
Do I have to import the keyboard class or something :?:
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Arduino and Freepie ?

Post by CyberVillain »

The correct usage is

Code: Select all

keyboard.setKeyDown(Key.W)
NeoTokyo_Nori
Cross Eyed!
Posts: 144
Joined: Wed Jul 16, 2014 10:29 am
Location: Tokyo, Japan

Re: Arduino and Freepie ?

Post by NeoTokyo_Nori »

Hi CV,
Something strange is happening...

The serial com part works alright, and serial messages are showing ok.

Code: Select all

def debugger():
   serialMsg = genericCom.ReadLine()
   if serialMsg != "":
      diagnostics.debug(serialMsg)
      diagnostics.watch(serialMsg)
But just adding that one line (even when commented out!) give these errors:
object reference is not set to object instance.
item with same key has already been added.
(the error message are in japanese, so the translation may be a little off)

Code: Select all

def debugger():
   serialMsg = genericCom.ReadLine()
   if serialMsg != "":
      diagnostics.debug(serialMsg)
      diagnostics.watch(serialMsg)
#      keyboard.setKeyDown(Key.W)
If I take off the comment, it gives the same errors, or crashes freepie
:(

I have been stuck in the same spot...
Post Reply

Return to “FreePIE”