tag:blogger.com,1999:blog-76969974192002513762024-03-04T22:55:23.748-08:00GorgiLabsNorvanGorgihttp://www.blogger.com/profile/00438427424218748711noreply@blogger.comBlogger4125tag:blogger.com,1999:blog-7696997419200251376.post-40130376095175204432013-11-26T08:48:00.000-08:002013-11-26T08:57:02.533-08:00Sensor design using MATLAB<b id="docs-internal-guid-531a6324-9340-50ee-ce40-4bcb170499dc" style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><span class="Apple-tab-span" style="white-space: pre;"> </span>Love it or hate it, the Arduino was one of the greatest things to happen in the field of electronics in the past 20 years. It made microcontrollers accessible to everyone, which in turn set off a demand for all sorts of sensors to allow these microcontrollers to take input from the world around them. Nowadays there’s a sensor for almost everything, and you can get them on a module all the required circuitry included - all you have to do is connect the power and data lines! However, just because the connection is easy doesn’t mean the implementation is as well. To design your algorithm, you will need to perform data analysis and simulations, and MATLAB is a tool that can make that much easier for you. In this guide I’ll show some ways to use MATLAB for this through an example with a magnetic vehicle detection sensor.</span></div>
<b id="docs-internal-guid-531a6324-9342-89e3-928f-5d37bd227c79" style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;">part 1: sending your data</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-indent: 36pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">99% of the time, if you’re talking to a microcontroller it’s going to be over UART. It may be in the form of an FTDI board, USB CDC mode, XBee, RS232, or Bluetooth, but it’s always good old UART. Since it’s primarily used for ASCII data (text, readable stuff), most people just go ahead and send their data over ASCII. It’s easy, but it comes with 2 big drawbacks. Lets say for my sensor, I want to transmit a value for each axis (XYZ) and the number of microseconds since the previous transmission routine. Each one of the axis values is a number between -2048 and 2048, and the microseconds value should ideally be 10000. At the worst case scenario, each line of the data would be:</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: center; text-indent: 36pt;">
<span style="background-color: transparent; color: black; font-size: 15px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><span style="font-family: Courier New, Courier, monospace;">-2048,-2048,-2048,10000\r\n</span></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: center;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><span class="Apple-tab-span" style="white-space: pre;"> </span>\r and \n are the carriage return and newline characters. the \ isn’t a separate character, it only specifies that the r and n are not regular printable characters.</span></div>
<br />
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"></span>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><span class="Apple-tab-span" style="white-space: pre;"> </span>That’s 25 characters, which means that you need to transmit 25 bytes of data for each sample. So what if instead of sending a textual representation of the data we send it in raw binary form? Each one of the axis values would be 2 bytes, the timestamp 3, and the \r\n characters 2. That’s only 11 bytes per sample - less than half of the original amount! Plus, this saves the overhead of converting the data to and back from text. There’s also yet another reason why you want to do this when working with Simulink - it doesn’t support strings. You either do it like this or you can’t do it at all.</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><span class="Apple-tab-span" style="white-space: pre;"> </span>Speaking of strange restrictions from Simulink, you’re going to have to frame your data rather oddly. A 16 bit integer is 2 bytes and UART sends one byte at a time, so you’d normally send the most significant byte followed by the least significant one, right? Not with Simulink! Lets pack up just the axis values into a frame (along with the newline characters) as an example.</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">X = -678<span class="Apple-tab-span" style="white-space: pre;"> </span>→<span class="Apple-tab-span" style="white-space: pre;"> </span>0xFD5A<span class="Apple-tab-span" style="white-space: pre;"> </span>→<span class="Apple-tab-span" style="white-space: pre;"> </span>0xFD<span class="Apple-tab-span" style="white-space: pre;"> </span>0x5A</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Y = 1220<span class="Apple-tab-span" style="white-space: pre;"> </span>→<span class="Apple-tab-span" style="white-space: pre;"> </span>0x04C4<span class="Apple-tab-span" style="white-space: pre;"> </span>→<span class="Apple-tab-span" style="white-space: pre;"> </span>0x04 <span class="Apple-tab-span" style="white-space: pre;"> </span>0xC4</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Z = -922<span class="Apple-tab-span" style="white-space: pre;"> </span>→<span class="Apple-tab-span" style="white-space: pre;"> </span>0xFC66<span class="Apple-tab-span" style="white-space: pre;"> </span>→<span class="Apple-tab-span" style="white-space: pre;"> </span>0xFC<span class="Apple-tab-span" style="white-space: pre;"> </span>0x66</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><b id="docs-internal-guid-531a6324-9343-e95a-8d76-2323b77a440b" style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Framing that along with the newline characters results in the following 8 byte array</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">0xFD 0x5A 0x04 0xC4 0xFC 0x66 0x0D 0x0A</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">That won’t work with Simulink. Instead, you need to frame it like so:</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">0x5A 0xFD 0xC4 0x04 0x66 0xFC 0x0D 0x0A</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">If this is confusing, then here’s my sample Arduino code to transmit a single sample over UART.</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">
<code style="color: black; font-family: Arial; font-size: 15px; word-wrap: normal;">
byte outBuff[] = { 0, 0, 0, 0, 0, 0, '\r', '\n' };
void rawPrintMode() {
outBuff[0] = (((sensor.rawVals)[0] & 0x00ff));
outBuff[1] = (((sensor.rawVals)[0] & 0xff00) >> 8);
outBuff[2] = (((sensor.rawVals)[1] & 0x00ff));
outBuff[3] = (((sensor.rawVals)[1] & 0xff00) >> 8);
outBuff[4] = (((sensor.rawVals)[2] & 0x00ff));
outBuff[5] = (((sensor.rawVals)[2] & 0xff00) >> 8);
Serial.write(outBuff, 8);
}
</code>
</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><br /><span style="vertical-align: baseline;"></span></span></div>
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<div style="line-height: 1.15;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">But enough about how you should frame your data, let’s go on to Simulink.</span></div>
<div style="line-height: 1.15;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><br /></span></div>
<div style="line-height: 1.15;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><b id="docs-internal-guid-531a6324-9361-e720-f3cf-23180f2c6777" style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b></span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;">part 2: receiving the data</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;"></span></div>
<div style="line-height: 1.15;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><span class="Apple-tab-span" style="white-space: pre;"> </span>Okay, so we’re transmitting data from our sensor, now we need to receive it. </span></div>
<div class="separator" style="clear: both; line-height: 1.15; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh4KOdknbE4vQctPzcAwbXMwQUfWPMCu6sfL0O6tMa8NmG6VXr4lKN2cqabJgKSi-6iODZIvEFlD5PISr20Si1Z2tKN0ftI8DiX0t_2jfxCWNiC3__VZV0la29lWTA2M2ytRigyOcLpMOuE/s1600/Capture3.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="269" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh4KOdknbE4vQctPzcAwbXMwQUfWPMCu6sfL0O6tMa8NmG6VXr4lKN2cqabJgKSi-6iODZIvEFlD5PISr20Si1Z2tKN0ftI8DiX0t_2jfxCWNiC3__VZV0la29lWTA2M2ytRigyOcLpMOuE/s320/Capture3.PNG" width="320" /></a></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><span style="white-space: pre;"> </span>The model above is our “Hello World” proof of concept. It merely reads in data and displays it through a display and a scope. The configuration block is self explanatory, but the receive block is a little confusing. </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Terminator</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">: As you can see in my example Arduino code, all of my frames end with \r\n, so that’s what I selected for the terminator. </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Data Type</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">: Is it an int? A byte? A long? Is it signed or unsigned? If you’re sending your data in raw format, then you should already know what type it is.</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Data Length</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">: This isn’t the number of bytes in the payload part of the frame, it’s the number of values of the given data type. For example, even though I’m sending 6 bytes, I set the size to 3 because each number consists of 2 bytes.</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Enable Blocking Mode</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">: Check this box if you don't like real-time performance</span></div>
<div class="separator" style="clear: both; line-height: 1.15; text-align: left;">
<span id="docs-internal-guid-531a6324-9362-25a9-0dd8-f3e0e19635b5"><span style="font-family: Arial; font-size: 15px; font-weight: bold; vertical-align: baseline; white-space: pre-wrap;">Sample Time: </span><span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;">MATLAB doesn't count time in seconds, so I don't have anything solid for what you should put here. Go with a very small number, one that's much smaller than your sensor's sampling rate. Also, make sure your simulation is running for inf time with a fixed step size of auto.</span></span></div>
<div class="separator" style="clear: both; line-height: 1.15; text-align: left;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"><br /></span></div>
<div class="separator" style="clear: both; line-height: 1.15; text-align: left;">
<span style="font-family: Arial; font-size: 15px; line-height: 17px; white-space: pre;"> </span><span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;">As you can see, our signal is getting routed into 4 places. First, there's a scope and a display so you can observe the data in real time. It also gets sent to the current workspace and gets logged to a file so you can then analyze and test against it. Now that you've captured your data, it's time to disconnect the sensor and start the analysis.</span></div>
<div class="separator" style="clear: both; line-height: 1.15; text-align: left;">
<span style="font-family: Arial; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;"><br /></span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: Arial;"><span style="font-size: 15px; line-height: 17px; white-space: pre-wrap;"><b><u>part 3: building the algorithm</u></b></span></span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: Arial;"><span style="font-size: 15px; line-height: 17px; white-space: pre-wrap;">We have 3 analog inputs and need to generate a single boolean output. The following scripts and graph demonstrate this process step by step.</span></span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: Arial;"><span style="font-size: 15px; line-height: 17px; white-space: pre-wrap;"><br /></span></span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: Arial;"><span style="font-size: 15px; line-height: 17px; white-space: pre-wrap;"><br /></span></span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: Arial; font-size: 15px; line-height: 17px; white-space: pre;"><code style="color: black; font-family: Arial; font-size: 15px; word-wrap: normal;">
vals = [x y z];
t = millis;
% Find a good section and zoom in.
subplot(321)
plot(vals)
title('1')
% 8375 to 9075 seems good.
t = t(8375:9075);
vals = [x(8375:9075) y(8375:9075) z(8375:9075)];
subplot(322)
plot(vals)
title('2')
% Now we need to remove each channel's offset.
% We do this by taking the average of a silent segment.
avgs = [mean(x(950:1150)) mean(y(950:1150)) mean(z(950:1150))]
for i = 1:3;
vals(:,i) = vals(:,i) - avgs(i);
end
subplot 323
plot(vals)
title('3')
% To normalize the three axes, we calculate the magnitude.
mag = []
for i = 1:length(vals);
mag(i,:) = sqrt(sum(vals(i,:).^2));
end
subplot 324
plot(mag)
title('4')
subplot 325
% Finally, we run it through our parser function
detected = []
lastState = 0
for i = 1:length(mag);
lastState = detectObject(mag(i), lastState);
detected(i) = lastState;
end
plot([mag,detected' .* 120])
title('5')
subplot 326
% And now just the triggers
detected = []
lastState = 0
for i = 1:length(mag);
lastState = detectObject(mag(i), lastState);
detected(i) = lastState;
end
plot(detected' .* 120)
ylim([0,280])
title('6')</code></span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: Arial; font-size: 15px; line-height: 17px; white-space: pre;"><code style="color: black; font-family: Arial; font-size: 15px; word-wrap: normal;"><br /></code></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi2EgVUzX6pC3U_xNWZroIg52vjICFmnHvqjoyx2ggQU-OAfjxfDOAK1zgAliQzFv-Xl1cu-OiIB_VEobCFqx7jBF4koNhyWB6v59EDLficDjHj1BZT-qDa-ss2OqV2PNXLWfW8DO3RYkja/s1600/untit6led.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="237" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi2EgVUzX6pC3U_xNWZroIg52vjICFmnHvqjoyx2ggQU-OAfjxfDOAK1zgAliQzFv-Xl1cu-OiIB_VEobCFqx7jBF4koNhyWB6v59EDLficDjHj1BZT-qDa-ss2OqV2PNXLWfW8DO3RYkja/s320/untit6led.png" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: Arial;"><span style="font-size: 15px; line-height: 17px; white-space: pre;">Here's the main detection function, which is a simple threshold trigger with hysteresis.</span></span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: Arial;"><span style="font-size: 15px; line-height: 17px; white-space: pre;"><br /></span></span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: Arial;"><span style="font-size: 15px; line-height: 17px; white-space: pre;"><code style="color: black; font-family: Arial; font-size: 15px; word-wrap: normal;">
function [ output] = detectObject (magnitude, isOn)
OFFpoint = 80
ONpoint = 120
if isOn ~= 0 & magnitude < OFFpoint;
output = false
elseif isOn == 0 & magnitude > ONpoint
output = true
else
output = isOn
end
end
</code> </span></span></div>
<br />
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: Arial;"><span style="font-size: 15px; line-height: 17px; white-space: pre;"><br /></span></span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: Arial;"><span style="font-size: 15px; line-height: 17px; white-space: pre;"><b><u>part 4: implementing the algorithm</u></b></span></span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: Arial;"><span style="font-size: 15px; line-height: 17px; white-space: pre;">From here on out, it's just a matter of porting your MATLAB functions to your platform. For this </span></span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: Arial;"><span style="font-size: 15px; line-height: 17px; white-space: pre;">example, the detectObject function would be translated into C and be called every time a new data </span></span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: Arial;"><span style="font-size: 15px; line-height: 17px; white-space: pre;">sample is received.</span></span></div>
</div>
NorvanGorgihttp://www.blogger.com/profile/00438427424218748711noreply@blogger.com0tag:blogger.com,1999:blog-7696997419200251376.post-57581925490222236122012-11-01T16:48:00.000-07:002012-11-01T16:48:47.018-07:00Window Tagging with OpenboxThere's few things I like in this world more than CrunchBang Linux. I'd used Ubuntu, Xubuntu, Lubuntu before, but all of them felt like bloated alternatives to bloated Windows and there was little reason to make the switch. Then, I discovered CrunchBang and within a month had deleted my Windows partition.<br />
<div>
<br /></div>
<div>
My favorite part about CrunchBang is Openbox - a damn fine window manager with a lot of room for customization. One (of the very, very few) drawbacks about Openbox is the lack of window tagging - and I'm not about to switch to a whole new WM like i3 or awesome just for this feature. Thankfully though, it's not impossible to implement it!</div>
<div>
<br /></div>
<div>
Here's how my solution works - press Alt-Super-(1-9) to tag a window to that number, and then press Super-(1-9) to switch to that window. This is all done using xdotool and some text files to store window id's, but since Openbox can't do anything too fancy in its execute command we have to do the work in an external shell script. Anyways, here's the goods.</div>
<div>
<br /></div>
<div>
In your rc.xml file (add as many copies as you want, just make sure to change the numbers):</div>
<div>
<pre style="background-image: URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgmVqUyakkDaPBWF_NpNmO2dU1g6OkudMGpNgxTWTlUFAqtIq5fGzQB9hSb7VbMvCtJPcgmFrJXJ4K6K2JR_jTCmt-cSGa5Tri-KYKcm8AwcjUyam0QvrJDLfnP0Tmu7VcJ-8_K0kdHBM1_/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> <keybind key="A-W-1">
<action name="Execute">
<command>sh ~/.config/winTag/saveTag 1</command>
</action>
</keybind>
<keybind key="W-1">
<action name="Execute">
<command>sh ~/.config/winTag/recallTag 1</command>
</action>
</keybind>
</code></pre>
</div>
<br />
Then, place the following 2 files in ~/.config/winTag/<br />
<br />
saveTag<br />
<pre style="background-image: URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgmVqUyakkDaPBWF_NpNmO2dU1g6OkudMGpNgxTWTlUFAqtIq5fGzQB9hSb7VbMvCtJPcgmFrJXJ4K6K2JR_jTCmt-cSGa5Tri-KYKcm8AwcjUyam0QvrJDLfnP0Tmu7VcJ-8_K0kdHBM1_/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> #!/bin/sh
xdotool getwindowfocus > ~/.config/wintag/save$1
</code></pre>
<br />
<br />
<br />
recallTag
<br />
<pre style="background-image: URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgmVqUyakkDaPBWF_NpNmO2dU1g6OkudMGpNgxTWTlUFAqtIq5fGzQB9hSb7VbMvCtJPcgmFrJXJ4K6K2JR_jTCmt-cSGa5Tri-KYKcm8AwcjUyam0QvrJDLfnP0Tmu7VcJ-8_K0kdHBM1_/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> #!/bin/sh
xdotool windowactivate $(cat ~/.config/wintag/save$1)
</code></pre>
<br/>
<br/>
<br/>
Enjoy your new found freedom from Alt-Tab hell!
NorvanGorgihttp://www.blogger.com/profile/00438427424218748711noreply@blogger.com0tag:blogger.com,1999:blog-7696997419200251376.post-60137827050864980452011-10-17T17:49:00.000-07:002013-11-25T23:09:01.966-08:00Using a TomTom Bluetooth Remote with Android<div>
<b><u><strike>EDIT: The TomTom Bluetooth Remote only works with Android 2.3 and below. The issue is that on Android 2.3 and below, the user first enters the pairing key for keyboard devices on the phone and then on the keyboard. From Honeycomb and above, Android generates its own random pairing key and since the TomTom remote doesn't have any real keys, it's impossible to type that code in. If anyone finds a workaround, please let me know so I can update this.</strike>
<br />
EDIT: A workaround has been found! Read Becherraecher's comment below for more details.</u></b><br />
<br />
I was digging through my box of assorted crap and stumbled upon an old TomTom Bluetooth Remote that I got from my old job (because after all, why does a store display TomTom need a remote?). When I first got it I couldn't find a use for it, but 2 years later with a good deal of Android experience under my belt, I decided to see what I could do with it.</div>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhe9TR0CazflBP24dIbmmDJCg3Ac2MPwKe0cqTqJrEJX5UaGrzH0J85bZ_69Lf1MsXk3OKg1IrMM5tyAqQi4-EgAnWXcbXHMVHmb_YlSiKk9vPy20mIKXpFE5dQNjuYz-em_pb6zjkv5Ad-/s1600/Tm2.jpg" onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}"><img alt="" border="0" id="BLOGGER_PHOTO_ID_5664629354485496130" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhe9TR0CazflBP24dIbmmDJCg3Ac2MPwKe0cqTqJrEJX5UaGrzH0J85bZ_69Lf1MsXk3OKg1IrMM5tyAqQi4-EgAnWXcbXHMVHmb_YlSiKk9vPy20mIKXpFE5dQNjuYz-em_pb6zjkv5Ad-/s320/Tm2.jpg" style="cursor: hand; cursor: pointer; display: block; height: 256px; margin: 0px auto 10px; text-align: center; width: 320px;" /></a><br />
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
Now the first hurdle was pairing the device with my phone. Although it's using Bluetooth, the pairing isn't done the standard way. However, I found the following method to work most of the time. Note: Your device must support the Bluetooth HID profile for this to work.</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
1. Remove one of the batteries from the remote for 30 seconds. This clears any previous pairing data and gives you a fresh slate to start from.</div>
<div style="text-align: left;">
2. Tap "Scan for devices" and quickly insert the batteries. It probably won't be found on the first try, so keep on hitting "Scan for devices" until TomTom Remote shows up. At this point, if you try to pair, it will probably fail. </div>
<div style="text-align: left;">
3. When the blue light on the remote stops blinking, select the remote on your Android device and pair with the passcode "0000". </div>
<div style="text-align: left;">
4. Now your Android device should show that it is trying to pair with the remote. Press the middle button on the remote and your Android device should prompt your for the passcode again. Enter "0000" again. </div>
<div style="text-align: left;">
5. The Android device should now show that the remote is paired but not connected. Long press the remote, check the box for Connect, and go back. Now press the center button on the remote again and it should be connected to your phone!</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
We just got over the first major hurdle! Select a text input box and press some buttons on the remote and you'll see text being entered! However, a keyboard with a few random buttons isn't really useful, so let's remap the keys!</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
To do this, we'll have to create a custom keyboard profile for the remote which will map the scancodes sent by the device to keys recognized by Android. I'm going to save you the effort of finding out the scancodes since I already found them myself.</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<div style="text-align: left;">
Direcrional Keys</div>
<div style="text-align: left;">
Up <span class="Apple-tab-span" style="white-space: pre;"> </span>103</div>
<div style="text-align: left;">
Down <span class="Apple-tab-span" style="white-space: pre;"> </span>108</div>
<div style="text-align: left;">
Left <span class="Apple-tab-span" style="white-space: pre;"> </span>105</div>
<div style="text-align: left;">
Right <span class="Apple-tab-span" style="white-space: pre;"> </span>106</div>
<div style="text-align: left;">
Center <span class="Apple-tab-span" style="white-space: pre;"> </span>28</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
3 Middle Keys</div>
<div style="text-align: left;">
A <span class="Apple-tab-span" style="white-space: pre;"> </span>1</div>
<div style="text-align: left;">
B <span class="Apple-tab-span" style="white-space: pre;"> </span>13</div>
<div style="text-align: left;">
C <span class="Apple-tab-span" style="white-space: pre;"> </span>14</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
Vol+ <span class="Apple-tab-span" style="white-space: pre;"> </span>67</div>
<div style="text-align: left;">
Vol- <span class="Apple-tab-span" style="white-space: pre;"> </span>68</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
Next, you'll need to pull 2 files off of your phone to modify. Fire up command prompt, navigate to the platform-tools directory of the Android SDK, and enter the following commands:</div>
<div style="text-align: left;">
<br /></div>
<code></code><br />
<div style="text-align: left;">
<code>adb pull /system/usr/keychars/qwerty.kcm.bin TomTom_Remote.kcm.bin</code></div>
<div style="text-align: left;">
<code><br /></code></div>
<div style="text-align: left;">
<code>adb pull /system/usr/keylayout/qwerty.kl TomTom_Remote.kl</code></div>
<br />
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
Open up TomTom_Remote.kl in your favorite text editor, see how the syntax works, erase everything, and create an entry for each of the 10 buttons. For example, mine looks like this:</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<div style="text-align: left;">
</div>
<code></code><br />
<div style="text-align: left;">
<code>key 106<span class="Apple-tab-span" style="white-space: pre;"> </span>MEDIA_NEXT</code></div>
<div style="text-align: left;">
<code>key 105<span class="Apple-tab-span" style="white-space: pre;"> </span>MEDIA_PREVIOUS</code></div>
<div style="text-align: left;">
<code>key 28<span class="Apple-tab-span" style="white-space: pre;"> </span>MEDIA_PLAY_PAUSE</code></div>
<div style="text-align: left;">
<code>key 108 <span class="Apple-tab-span" style="white-space: pre;"> </span>1</code></div>
<div style="text-align: left;">
<code>key 1 <span class="Apple-tab-span" style="white-space: pre;"> <span class="Apple-tab-span" style="white-space: pre;"> </span></span>3</code></div>
<div style="text-align: left;">
<code>key 13 <span class="Apple-tab-span" style="white-space: pre;"> </span>4</code></div>
<div style="text-align: left;">
<code>key 14 <span class="Apple-tab-span" style="white-space: pre;"> </span>5</code></div>
<div style="text-align: left;">
<code>key 68 <span class="Apple-tab-span" style="white-space: pre;"> </span>VOLUME_UP</code></div>
<div style="text-align: left;">
<code>key 67 <span class="Apple-tab-span" style="white-space: pre;"> </span>VOLUME_DOWN </code></div>
<div style="text-align: left;">
<code>key 103 <span class="Apple-tab-span" style="white-space: pre;"> </span>POWER <span class="Apple-tab-span" style="white-space: pre;"> </span>WAKE</code></div>
<br />
<div style="text-align: left;">
</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
You may chose to map the remote's D-Pad to Android's directional keys if you like, but since I'm using it as a music remote in my car, I used those keys. <a href="http://developer.android.com/reference/android/view/KeyEvent.html">You can find a list of all the possible keys here.</a> (If you're wondering why I mapped some key's to 1,3,4, and 5, it's because I also programmed my personal media player to preform special functions with those keys.)</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
Save your changes to the .kl file and enter the following commands into your command prompt:</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
</div>
<code></code><br />
<div style="text-align: left;">
<code>adb push TomTom_Remote.kl /system/usr/keylayout/TomTom_Remote.kl</code></div>
<div style="text-align: left;">
<code><br /></code></div>
<div style="text-align: left;">
<code>adb push TomTom_Remote.kcm.bin /system/usr/keychars/TomTom_Remote.kcm.kl</code></div>
<div style="text-align: left;">
<code><br /></code></div>
<div style="text-align: left;">
<div style="text-align: left;">
<code>adb shell "chmod 644 /system/usr/keychars/TomTom_Remote.kcm.kl"</code></div>
<div style="text-align: left;">
<code><br /></code></div>
<div style="text-align: left;">
<code>adb shell "chmod 644 /system/usr/keylayout/TomTom_Remote.kl" </code></div>
</div>
<br />
<div style="text-align: left;">
<div style="text-align: left;">
</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
Restart your phone, connect to the remote, and it should now preform the new functions!</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
Many thanks to <a href="http://blog.mymediasystem.net/uncategorized/igo_stowaway_for_android/">this blog post</a> for teaching me how to remap buttons.</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<br /></div>
</div>
</div>
</div>NorvanGorgihttp://www.blogger.com/profile/00438427424218748711noreply@blogger.com15tag:blogger.com,1999:blog-7696997419200251376.post-58053802271400435492010-06-14T19:08:00.001-07:002013-11-25T23:05:46.033-08:00GorgiLabsThis is the site/blog for the Android developer Norvan Gorgi.NorvanGorgihttp://www.blogger.com/profile/00438427424218748711noreply@blogger.com1