Friday 11 July 2014

FreeCAD + Arduino

Modifying shapes by reading sensors can create a lot of possibilities, for example, this video:



An Arduino DUE with a varible resistor commands a servo inside FreeCAD.


This post is about making what is shown in the video.

What you need:


-Linux

-Any Arduino board with serial connection

-Arduino ide

-FreeCAD

- My servo model

-Pyserial library ( sudo apt-get install python-serial )

-A look at the Oficial arduino and python guide ( not really needed, but my main source )

The idea:

-Print the value of the sensor by serial
-Create a function that reads the Arduino serial
-Create another function that, with the value of the serial, updates the object in FreeCAD
-Call them repetitively by a timer

The Arduino part:

At the video I'm using an Arduino DUE because it was handy, but an UNO board is valid too.

The electrical thing consists of connecting a variable resistor to the A0 pin, like the scheme:


Any variable resistor above 1kΩ will do the job, in the video I use a 4.7kΩ one. 

The code is quite simple:
void setup()
{
  Serial.begin(9600);                  /// Start serial at 9600 baud
  pinMode( A0, INPUT );                /// Set pin A0 as input
}

void loop()
{
  Serial.println( analogRead( A0 ) );  /// Print to serial A0 value
}

Initialize serial and then constantly print the sensor value.

The Python code:

This are the needed libraries:

import serial
from PySide import QtCore

Serial for reading the serial (obvious one) and PySide for the timer object.

To initialize the serial at 9600 baud pointed to the Arduino:

ser = serial.Serial('/dev/ttyACM0', 9600)

The Arduino serial should be in '/dev/ttyACM0' but sometimes it switches to '/dev/ttyACM1',
if you fire up the Arduino Ide and open the serial window, the path is at the window title.

The name "ser" now contains the serial, which we can read this way:

ser.readline()

That prints whatever the serial is saying.

The FreeCAD part:

We need a function that changes the position of the servo arm to the input value. From the arduino, we receive a number from 0 to 1024, a servo rotates ~180 degrees, so a conversion is needed.

def SERVO(valor):
  angle = valor*-180.0/1024.0
  Position = FreeCAD.Placement(App.Vector(12.5,12,53),App.Vector(0,0,1),angle) 
  FreeCAD.ActiveDocument.Fillet004.Placement = Position

The first line of the function SERVO is the 1024 to 180º conversion, the second and third ones do the position change of the FreeCAD object. Note that the servo arm is called "Fillet004".

You can test this function by giving values to it, like SERVO(200) or SERVO(1000), it should move.

The link:

The function SERIAL links  everything together:

servalue0 = 0 # avoid problems with serial initialization

def SERIAL():
  global servalue0
  hysteresis = 6.0 # to smooth the movement
  try:
    servalue = int(ser.readline()) # to int the serial value
  except:
    servalue = servalue0
  if servalue > servalue0 + hysteresis or servalue < servalue0 - hysteresis:
    SERVO(servalue) # update the servo position
    servalue0 = servalue # keep last value to check hysteresis

The timer:

To give it life we need to call SERIAL function repetitively with a timer:

timer = QtCore.QTimer()
timer.timeout.connect(SERIAL)
timer.start(1)


First line creates timer object, second one connects its signals to the SERIAL function and the third one makes it emit a signal every 1 ms.


Complete Python script:

If you just want to test it, create the Arduino circuit, download the servo model and copy paste this at FreeCAD console:

"""
Javier Martinez Garcia, 2014 
"""
import serial
from PySide import QtCore

try:
  ser = serial.Serial('/dev/ttyACM0', 9600)
  
except:
  ser = serial.Serial('/dev/ttyACM1', 9600)

def SERVO(valor):
  angle = valor*-180.0/1024.0
  Position = FreeCAD.Placement(App.Vector(12.5,12,53),App.Vector(0,0,1),angle) 
  FreeCAD.ActiveDocument.Fillet004.Placement = Position 

servalue0 = 0 # avoid problems with serial initialization

def SERIAL():
  global servalue0
  hysteresis = 6.0 # to smooth the movement
  try:
    servalue = int(ser.readline()) # to int the serial value
  except:
    servalue = servalue0
  if servalue > servalue0 + hysteresis or servalue < servalue0 - hysteresis:
    SERVO(servalue) # update the servo position
    servalue0 = servalue # keep last value to check hysteresis

timer = QtCore.QTimer()
timer.timeout.connect(SERIAL)
timer.start(1)



And this is all you need to repeat my video.
There are things that can be improved, for example the timer. Maybe using the threading library can give the same result.

About the possibilities, the communication can be bi-directional too, here I show realworld->FreeCAD, but the opposite is perfectly possible. And the FreeCAD objects attributes that can be changed do not need to be exclusively placement, but color or even parametric models.

Just imagine.

Bye!

1 comment:

  1. Hi, your experiment is excellent, I worked in 3d delta printer, and I need a help to simulation, thanks, greetings from Colombia

    ReplyDelete