Noob here. I am trying to get Blender 2.6 to communicate to an Arduino Mega board via USB serial comms. I cannot find a nice summary of how to write Python script to get Blender to send serial data to the Arduino. I found this guys project: http://justindailey.blogspot.com/2011/03/real-time-controlled-robotic-arm.html -> which is similar to what I want to do, but there are no details on how to establish serial communications to a specified USB port.Plus I am using Blender 2.6. Thanks!
4 Answers
Try looking at this video. The gentlemen explains, step by step how to control an sevo with python and arduino I think this is what you want.
This isn't actually a blender specific question, if blender uses fully featured python! The difference between this and "regular" python, (if I'm not mistaken) is that python "here" knows how to talk to blender.
You might want to move this to Stack Overflow, there are a likely a lot more users with these kind of skills there - it's part of the same network of question and answer sites as this.
Remember, once you get this working, you will have to be clever enough to make the python algorithm:
A.) Get the information from blender
B.) Figure out what it means for the servo
C.) Optionally but preferably, do this at an appropriate time! (ie, whenever a relevant part is moved)
Always Remember: In Engineering, it is important to break problems into their component parts.
If it were me, I would follow the video by itself first - that way, you can actually move the servo, blender or not. Then make sure a python script that can talk to blender will do this.... it doesn't matter if you have to run the script by hand, at least it will work!
Then once you can make blender move the arm, - even if it doesn't care what is on your screen in blender! - do step "A". Now you're cooking! Figure out what the screen means for the server, and make it happen!
Finally, once you can make everything, see if you can work on "C"! This is the cool part. Remember everything that look "cool" has had a lot of backround effort put into it.
Inspiring Question! Good luck!
- Download blender 2.69 win32 bit version : http://www.blender.org/download/
- Download python 3.3: http://www.python.org/getit/releases/3.3.0/
- Download pyserial for 3.3 : http://www.lfd.uci.edu/~gohlke/pythonlibs/ -> pyserial-2.7.win32-py3.3.exe
- Copy all the newly downloaded files for pyserial3.3 into the blender/python files, trying to map them to the same places. Use the "pyserial-wininst.txt" file for help
- Download 'MPide' and load code onto the UNO32 board that will accept incoming serial data. Here is an example (sorry for the lack of new lines):
#include <Servo.h>
// Servos to be used
Servo servo1;
int pos = 0; // variable to store the servo position
void setup()
{
// This is using pin 9 (change accordingly)
servo1.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(13, OUTPUT); //LED
// initialize the serial port
Serial.begin(9600);
servo1.write(45); //initial servo position
}
int angle, light=0;
void loop()
{
// wait for the servo angle
if(Serial.available() > 2){
angle = Serial.read();
servo1.write(angle);
if (light==0) {
digitalWrite(13, HIGH);
light=1; // set the LED on
}
else {
digitalWrite(13, LOW);
light =0; // set the LED off
}
}
}
open blender, open the console, type "import serial" - if no probs the import worked. Next change servo angle as follows:
>>> import serial >>> ser = serial.Serial('COM7') #if using com7 port >>> # uses a byte array to send data to Arduino board >>> ser.write(bytearray([45]))
Here is some extra info I got from various places:
>>> #In Python 3.x the strings are unicode by default.
>>> ser.write(b '255')
When sending data to Arduino, they have to be converted to bytes. This can be done by prefixing the string with b
Since serial communication is for bytes, you would need to convert the int to a byte string. So the moral of the story is..
- the port is opened when initialized.
ser.open()
fails because the serial port is already opened by theser = serial.Serial(.....)
.- One cannot send strings directly with
serial.write()
- but,
ser.write(bytearray(TheString,'ascii'))
does the job - which I have adapted toser.write(bytearray([45]))
. like:data = bytearray(data,'ascii') in ser.write()
-
-
$\begingroup$ great instructions! Curious, why do you use MPide? The OP, and most other people have a standard Arduino, so they can use the standard Arduino IDE, no? Edit: LOL. you are the OP! Thanks for following up. Still curious about the MPide. $\endgroup$ Aug 1, 2020 at 7:11
This project does exactly what you need: https://hackaday.io/project/9851-blender-controller
It takes the angles from an IK solver and sends them over serial to an arduino. Built on blender v2.76, so it's compatible with the new API :)
To send data from Blender to the Arduino over serial you first need to get the data. There are two ways of doing so, from the Blender Render or the Blender Game Engine. I've gone with the former because you can then use the Pose mode and move the arm with the mouse.
In order to do so:
Open a serial connection and select a baud rate matching that of your computer Read the angles from the Armature Write them over serial Then repeat steps 2 and 3 once per frame to get it working smoothly.
The script that does all of this is very simple and availble here: https://github.com/bqlabs/BlenderController/blob/master/Blender/BlenderController.py
Im re-answering since my previous answer got deleted for being too short and I wasn't able to improve it because it got deleted.
-
$\begingroup$ All of the previous assumes you have a rigged armature with an Ik solver. If you don't, here are two excellent tutorials by revolt_randy explaining how to do so: vimeo.com/13386007 vimeo.com/13978208 Commenting because apparently I'm only allowed to add two links to the answer :) $\endgroup$ Feb 29, 2016 at 8:54
This sample maybe userful
https://github.com/OpenDevice/opendevice-examples/tree/master/opendevice-3d-blender
It makes the integration with OpenDevice(Java) and Blender (Python)
Allowing you to make any kind of communication with the Arduino (Bluetooth, USB, ethernet, wifi), the OpenDevice does the dirty work
-
3$\begingroup$ Is any of the relevant code able to be shown in the body of the answer? It's just with link-only answers, if the link goes down or changes then so does this answer. $\endgroup$ Sep 13, 2015 at 17:03