please dont rip this site

Servo Motor Control With Audio Waveforms or Files

Any device that can play music (PC, SmartPhone, MP3 player, etc...) can be used to reproduce arbitrary waveforms within the limitations of the electronic system that drives the analog output. Long periods of maximum or minimum signal level may not be coupled if the electronics don't reproduce low frequencies well. Other than that, there is no reason why PWM Servo control signals can't be encoded in a sound and then played back to move a motor.

Connecting a PC or SmartPhone audio headphone jack to a pair of servos requires no more than an 0.1uF decoupling capacitor in line from each stereo channel to the servo PWM input. Some have found that the cap's cause distortion and should be bypassed. ^ This may cause damage so take care.
http://www.gluemotor.com/how-to-make

Schematics of GlueMotor Cable

Note: This circuit will only work with devices that have a very loud audio output. In most cases, a simple transistor amplifier will be required.

An app or HTML5 web app could provide cross platform, programmable, motion control via cheap RC servoes and this simple hardware. For more complex transmission of data from a web page see: Unidirectional I2C (2 wire) via Images

The most basic use is to simply record and play back a series of motions via a single huge audio file. There is no programatic control, just a simple repeitition of the movement for each play. Generating the wave file has been accomplished with MatLab and should be possible from other programming environments.

Many Files

To give the playback device, such as a web browser or SmartPhone, the ability to change the waveform as directed by a program, multiple files, each with a different position encoded, could be loaded and played, as desired. Obviously, that means having a HUGE number of files, for all possible combinations of each channel. Even for a single channel, single degree control would mean having 180 seperate files. The advangate of this approach is that it doesn't require HTML5 <audio> tag support. Any Javascript enabled device should be able to programatically select different files and play them. But before making those files, the hardware needs to be improved. The "Generate" approach below can be used until then.

One File, Multiple Waveform "sprites"

With Javascript control over the position of the playback via the .currentTime property, a single sound file, incoded with a series of different PWM position signals at different times in the playback, can provide servo control from an HTML5 web (or smartphone) app. The .currentTime value is expressed in seconds, so playback position can not be selected any finer than 1 second. As a result, each PWM value must be repeated in the file for one full second. This increases the required file size, but the advantage of loading only one file may make that worth while.

// UNTESTED sample code

<audio id="PWM" ontimeupdate='updatePWMTime(this);'>
  <source src="PWM.wav">
</audio>

<script>
function setServoPosition(degrees) {
	var pwm = document.getElementById("PWM");
	pwm.desired = degrees + 90;
	updatePWMTime(pwm);
	}

function updatePWMTime(pwm) {
	//if (pwm.currentTime>pwm.desired) { //not needed
		pwm.currentTime = pwm.desired;
	//	} //ontimeupdate is only called once a second.
	}
</script>

<input type="range" name="degrees" min="-90" max="90" value="0" step="1" onchange='setServoPosition(this.value)';>

.WAV files are (ironically) not supported in Internet Explorer before version 9. They are supported in all other standard PC browsers (Chrome, Safari, Firefox). The Audio tag is supported in all modern browsers. Support on mobile devices may be buggy:
http://pupunzi.open-lab.com/2013/03/13/making-html5-audio-actually-work-on-mobile/

http://caniuse.com/audio
http://textopia.org/androidsoundformats.html

Although the .WAV file format allows almost any value for samples per second, in practice, the systems that play .WAV files back expect certain specific sample rates and depths. e.g. 96.000kHz (DVD), 48.000kHz (DAT), 44.100kHz - (CD, DAT, Mac, PC), 22.050kHz - (Mac, PC), 11.025kHz - (Mac, PC, Web).

For single degree resolution, the PWM signal needs to have 180 different duty cycles (allowing -90 to +90 degree movement in 1 degree intervals) from 1ms to 2ms. This requires a wavelength with enough samples to make 180 changes between those two values. The distance of 1ms (from 1 to 2) divided by 180 gives 5.55~degrees/ms. There must be 180 samples from 0 to 1ms, and another 180 from 1ms to 2ms, then some number of additional samples to complete the waveform in the low side. So more than 360 samples are required.

At 22050, each sample is ~0.0453ms so 1ms is abut 22 samples ~8' steps. 0.5MB file.

At 44100, each sample is ~0.0227ms so 1ms is about 44 samples ~4' steps. 2MB file

At 96000, a sample is ~0.0104ms so 1ms is about 96 samples ~2' steps. 9MB file

Obviously, there is no reasonable way to get single degree resolution. 2' isn't bad, but a 96KHz file would be very large. More than 9Mb. Perhaps it would compress well into an MP3?

Generate Waveform on Demand

In browsers that support the <audio> tag^ and .getChannelData(0)^ we can generate a high speed PWM wav on the fly^ and completely avoid the need for an audio file: Sadly, the Andriod browsers do not support this as of March 2013^ and andriod app is available, but without source code, it is useless except as a demonstration.^

html4audiopwm.html (try it hint: use Chrome)


<html>
<head><title>PWM</title></head>
<body>
<script>
var sampleRate = 96000;
var samplesPerMilliSecond = sampleRate*0.001;
var samplesPerCycle = samplesPerMilliSecond * 3;
var bufferLength = samplesPerCycle; //buffer one cycle
var context = new webkitAudioContext();
var buffer = context.createBuffer(1, bufferLength, sampleRate);
var data = buffer.getChannelData(0);
// TODO: Done: adjust samplesPerCycle to be an even multiple of the bufferLength
var degrees = 0;

function buildPWMWav(degrees, samplesPerMilliSecond, data) {
    var pwm = (degrees+90)/180*samplesPerMilliSecond+samplesPerMilliSecond;
    for (var i = 0; i < data.length; i++) {
        /* Prepare data */
        //data[i] = (Math.random() - 0.5) * 2; // Noise
        //data[i] = (i>=samplesPerMilliSecond && i<pwm)? 2 : 0;
	//data[i] = (i%samplesPerCycle<pwm)? 2 : 0
	data[i] = (i<pwm)? 2 : 0
        };
   }

onPWMChange = function (degrees) {    
    buildPWMWav(degrees, samplesPerMilliSecond, data);
    };

onPWMChange(0); //start in the center.

var source = context.createBufferSource();
source.loop = true; // Make sure that sound will repeat over and over again
source.buffer = buffer; // Assign our buffer to the source node buffer

gainNode = context.createGainNode();

//connect the source to the gainNode, and the gainNode to the destination.
source.connect(gainNode);
gainNode.connect(context.destination);

onVolumeChange = function (vol) {
    gainNode.gain.value = vol;
    };

onVolumeChange(0); //set initial volumn to zero


source.noteOn(0);
//Just start immediatly with zero volume. 
//Set volume to 0 to stop. If you use noteOff to stop, you have to recreate the source.

displayWave = function(canvas) {
var ctx = canvas.getContext('2d');
var amp = 32*gainNode.gain.value;
	canvas.width = data.length+10;
	ctx.fillStyle = 'white';
	ctx.fillRect(0,0,canvas.width,canvas.height);
	ctx.strokeStyle = 'red';
	ctx.beginPath();
	ctx.moveTo(0,canvas.height);
	for (var i = 0; i < data.length; i++) {
		ctx.lineTo(i,canvas.height-data[i]*amp);
		};
	ctx.moveTo(i-1,canvas.height);
	ctx.stroke();
	}

</script>
<H1>HTML5 PWM audio generator</H1>
Volume: 
<input type="range" min=0 max=100 onchange="
	onVolumeChange(parseInt(this.value, 10) / 100);
	displayWave(document.getElementById('scope'));
	" value=2>
<input type="button" value="On" onclick="
	onVolumeChange(0.05);
	displayWave(document.getElementById('scope'));
	">
<input type="button" value="Off" onclick="onVolumeChange(0);">
<BR>Degrees: -90
<input type="range" min=-90 max=90 onchange="
	onPWMChange(parseInt(this.value, 10));
	document.getElementById('degree').innerHTML=this.value;
	displayWave(document.getElementById('scope'));
	" value=0>+90
<div id="degree">0</div>
<canvas id="scope" width="200" height="100"></canvas>
<P>ToDo:
<UL>
<LI>Done: <strike>Adjust samplesPerCycle to a even divisor of bufferLength to remove the 1 second tick</strike>
<LI>Done: <strike>Added a visualization so you can see what the wave form is supposed to look like</strike>
<LI>Setup a pull down for common sample rates.
<LI>Allow wavforms to be saved at the current volume and degree.
</UL>
</body>

Given the lack of support for HTML5 <audio> in SmartPhone Browsers, it might be necessary to use a set of seperate .wav or .mp3 files, each one encoding a different PWM position.

See also:

The ability to control a motor could then be combined with Javascript based object recognition to turn a cell phone into an object following device. +


file: /Techref/io/servo/wav.htm, 13KB, , updated: 2017/2/3 00:24, local time: 2017/8/17 18:18,
TOP NEW HELP FIND: 
54.162.111.247:LOG IN

 ©2017 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE! / 

<A HREF="http://techref.massmind.org/Techref/io/servo/wav.htm"> Servo Motor, .Wav file, Hobby Servo, IOT</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

 

Welcome to massmind.org!

 

Welcome to techref.massmind.org!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .