mcjMPU6050


________________________________________________________________________
Introduction
________________________________________________________________________

This is a kit of software allowing you to control objects in a Daz Studio scene using an Arduino equipped with sensors

This release is designed for the MPU-6050 sensor board but the communication protocol is easy to implement

so if you are am Arduino programmer, you could adapt it to other sensors including potentiometers and switches


________________________________________________________________________
Pre-Requisites
________________________________________________________________________

you worked with Arduinos or electonic hardware before, so you can safely wire a sensor to an arduino

a PC computer since the bridge between arduino's COM port is an .exe

________________________________________________________________________
Installation
________________________________________________________________________

1 - install the MPU6050_tockn Arduino library by tockn which will let the arduino communicate with the MPU-6050 
https://github.com/tockn/MPU6050_tockn


2 - download and install mcjMPU6050.ino ( by me ) on your arduino uno -
it can be found in the attachment section at the bottom of this web page



3 - download MCJCOMMTTY08262018.zip it contains mcjComtty.exe A PC/DOS program known as a "terminal"
It is the communication bridge between the Arduino and DazStudio
We suggest you unzip it in YourDazContentLibrary/scripts/mCasual
because by default that's where mcjMPU6050.dsa will expect to find it
but you can place it where you please!

MCJCOMMTTY08262018.zip can be found in the attachment section at the bottom of this web page.
MCJCOMMTTY08262018.zip also contains mcjCommtty.cpp the source code,
which is a very lightly modified version of the very old ( 1996) program CommTTY by Robert Mashlan
http://nersp.nerdc.ufl.edu/~esi4161/files/commtty.c


4 - download mcjMPU6050.zip It contains 
 mcjMPU6050.dsa;  
a Daz Studio script which sends, receives and displays data to/from the Arduino 
through the mcjCommTty.exe bridge. Unzip it in a Daz Studio Content folder.

Typically it means:
c:\program files\daz\studio\contents
or
C:\Program Files (x86)\DAZ\Studio\content
or
C:\Users\YOURUSERNAME\Documents\DAZ 3D\Studio\My Library\

if installed properly, mcjMPU6050.dsa will be accessible in Daz Studio in the contents tab as

studio/scripts/mcasual/
or
My Library/scripts/mCasual

________________________________________________________________________
Version History
________________________________________________________________________
August 26th 2018 6:48PM - Release


________________________________________________________________________
Wiring
________________________________________________________________________

i used this wiring for my GY-521 module


source: https://42bots.com/tutorials/arduino-script-for-mpu-6050-auto-calibration/

________________________________________________________________________
Instructions
________________________________________________________________________

make sure the arduino is running mcjMPU6050.ino

In your Daz Studio Scene, select the object(s) which will be controlled by the mpu-6050

launch the mcjMPU6050.dsa Daz Script

set the com port appropriately and the baud rate to 57600 ( because the arduino sketch uses that baudrate )

if it's not already done, press the "Select Server" button and specify the location of mcjComtty.exe

press the "Start Server" button

the channel list on the left will be populated with the names of the sensor channels


The Server/Client Linking section

Now lets connect some sensor channels to the Gabrielle's head transforms

click-select the "AngX" server channel, click-select the  "Head Side-side" client channel

The mpu6050 gives us this angle in degrees and the car's XRotate channel is in degrees

since the side-side rotation of the sensor made the head swing too widely, i applied a 0.5 factor to it

the "Add" factor could let you for example add 90 degrees to the sensor's reading before applying it to the head rotation

click on the "Add Link" button

Note that the correct "Server channel" and the correct "Client Channel" and the correct "Server/Client Link" 
must be selected when you use the "Modify" or "remove" button

You can save/load your server-client links as a text file

note that the script will not accept to load links for the Head when the selected object is not a Head

As you can see below when the Arduino starts

it will transmit the names of the properties that will follow, 
this is called the "HEADERS" message

then the next transmissions will be the property values, separated by TAB characters with an "eol\n" delimiter at the end of each record
this is called the "DATA" messages

If the "Server Channels" list does not fill up, you can press the Arduino's  reset button or the script's "GetHeaders" button

By default the sampling rate is 10 per second ( 100ms intervals ) but you can adjust this with the "period (ms)" slider.

Also by default the Arduino doesn't send data samples until you click the "One Sample" button or you check-mark the "Periodic" checkbox




The Record buttons

using the "no feedback" button will record the motion data, but it wont be visible on screen during the capture

this ensures the optimal sample rate

using the "with live feedback" button  will record the motion data and you will see the animation on screen during the capture

  Source code for mpu6050.ino 
 #include <MPU6050_tockn.h> #include <Wire.h> //mCasualJacques 08/26/2018 5:18PM MPU6050 mpu6050(Wire); bool bPeriodic = false; int period = 100; long timer = 0; long timezero = millis(); void setup() { Wire.begin(); mpu6050.begin(); mpu6050.calcGyroOffsets(true); Serial.setTimeout(50); //improve responsiveness Serial.begin(57600); delay( 10 ); // note: we delayed the Serial.begin() until now // to avoid having the mpu6050 setup functions // sending debug info in a format that // mcjMPU6050.dsa cant process // the alternative would be to modify MPU6050_tockn.cpp // but we prefer not doing that sendHeaders(); } void sendHeaders() { timezero = millis(); Serial.print( "HEADERS\t" ); Serial.print(millis()-timezero); Serial.print("\t"); Serial.print( "tmpr\t" ); Serial.print( "aclX\t" ); Serial.print( "aclY\t" ); Serial.print( "aclZ\t" ); Serial.print( "gyrX\t" ); Serial.print( "gyrY\t" ); Serial.print( "gyrZ\t" ); Serial.print( "acAX\t" ); Serial.print( "acAY\t" ); Serial.print( "gyAX\t" ); Serial.print( "gyAY\t" ); Serial.print( "gyAZ\t" ); Serial.print( "angX\t" ); Serial.print( "angY\t" ); Serial.print( "angZ\t" ); Serial.print( "eol\n" ); } void sendSample() { Serial.print("DATA"); Serial.print("\t"); Serial.print(millis()-timezero); Serial.print("\t"); Serial.print(mpu6050.getTemp()); Serial.print("\t"); Serial.print(mpu6050.getAccX()); Serial.print("\t"); Serial.print(mpu6050.getAccY()); Serial.print("\t"); Serial.print(mpu6050.getAccZ()); Serial.print("\t"); Serial.print(mpu6050.getGyroX()); Serial.print("\t"); Serial.print(mpu6050.getGyroY()); Serial.print("\t"); Serial.print(mpu6050.getGyroZ()); Serial.print("\t"); Serial.print(mpu6050.getAccAngleX()); Serial.print("\t"); Serial.print(mpu6050.getAccAngleY()); Serial.print("\t"); Serial.print(mpu6050.getGyroAngleX());Serial.print("\t"); Serial.print(mpu6050.getGyroAngleY());Serial.print("\t"); Serial.print(mpu6050.getGyroAngleZ());Serial.print("\t"); Serial.print(mpu6050.getAngleX()); Serial.print("\t"); Serial.print(mpu6050.getAngleY()); Serial.print("\t"); Serial.print(mpu6050.getAngleZ()); Serial.print("\teol\n"); } void loop() { mpu6050.update(); if( (millis() - timer > period ) && bPeriodic ) { sendSample(); timer = millis(); } else { if (Serial.available()) { //example: "period 33" for 30fps String str = Serial.readString(); str.replace("\n",""); int pos = str.indexOf( "period" ); if( pos >= 0 ) { period = str.substring( pos + 6 ).toInt(); Serial.print("ACK"); Serial.print("\t"); Serial.print(millis()-timezero); Serial.print("\t"); Serial.print(str); Serial.print("\teol\n"); } else if( str.indexOf( "getHeaders" ) >= 0 ) { sendHeaders(); } else if( str.indexOf( "getSample" ) >= 0 ) { sendSample(); } else if( str.indexOf( "setPeriodic on" ) >= 0 ) { bPeriodic = true; Serial.print("ACK"); Serial.print("\t"); Serial.print(millis()-timezero); Serial.print("\t"); Serial.print(str); Serial.print("\teol\n"); } else if( str.indexOf( "setPeriodic off" ) >= 0 ) { bPeriodic = false; Serial.print("ACK"); Serial.print("\t"); Serial.print(millis()-timezero); Serial.print("\t"); Serial.print(str); Serial.print("\teol\n"); } else { Serial.print("NACK"); Serial.print("\t"); Serial.print(millis()-timezero); Serial.print("\t"); Serial.print(str); Serial.print("\teol\n"); } } } }  
   

________________________________________________________________________
Notes
________________________________________________________________________
using a sampling period lower than the desired sampling period
may allow you to get up to 120 samples per second.
but dont go too low, else the arduino will not have idle time to check 
your commands. a sampling period of 10ms was ok

The arduino transmits 14 parameters, if only the last 3 interest you
then in mcjmpu6050.ino comment out the lines  for those parameters
Be careful not to comment out the following lines. Also make sure that 
if you comment out a DATA item, you comment out the corresponding HEADERS item

Serial.print( "HEADERS\t" ); Serial.print(millis()-timezero); Serial.print("\t"); ...
Serial.print( "eol\n" );

and

Serial.print( "DATA\t" );
________________________________________________________________________
Programmer notes, protocol
________________________________________________________________________
The Arduino recognizes the following commands

 period 33 set the sampling period at 33ms
 getHeaders get the record column headers
 getSample get one record values
 setPeriodic on setAutomatic sampling + transmission on
 setPeriodic off setAutomatic sampling + transmission off
The arduino can send the following responses
 HEADERS 0 tmpr aclX aclY aclZ gyrX gyrY gyrZ acAX acAY gyAX gyAY gyAZ angX angY angZ eol  response for getHeaders
 DATA 31293 28.36 0.02 -0.05 1.03 0.19 -0.02 -0.09 -2.83 -0.87 -1.14 -0.81 0.30 -2.73 -1.41 0.30 eol  response for getSample
 ACK 8032 ~~~valid command~~~ eol acknowledging valid commands
 NACK 8032 ~~~invalid command~~~ eol acknowledging invalid commands

facer100.mp4


________________________________________________________________________
License and credits for mcjCommtty.exe
________________________________________________________________________
copyright 2018 mCasualJacques 
permission to use in other programs is granted.

this is an almost unmodified version of commtty.c
by Robert Mashlan

copyright 1996 Robert Mashlan rmashlan@r2m.com
permission to use in other programs is granted.

http://nersp.nerdc.ufl.edu/~esi4161/files/commtty.c

________________________________________________________________________

// License and credits for mcjmpu6050

________________________________________________________________________

// mcjmpu6050.dsa by mCasual/Jacques c2018

// mcjmpu6050.ino by mCasual/Jacques c2018


// You can use this script freely for personal or commercial use.

// You may not sell, resell, sub-license or rent this script in any way.

// you may credit this script to mCasual/Jacques


// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,

// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES

// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND

// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT

// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,

// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING

// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR

// OTHER DEALINGS IN THE SOFTWARE.


__________________________________________


ċ
mcMPU6050.zip
(62k)
mCasual Jacques,
Aug 26, 2018, 3:48 PM
ċ
mcjCommTty08262018.zip
(8k)
mCasual Jacques,
Aug 26, 2018, 3:41 PM
ċ
mcjMpu6050.ino
(4k)
mCasual Jacques,
Aug 26, 2018, 2:19 PM
Comments