Windows for IoT で距離を測る (GP2Y0A21YK)

秋月で手に入る距離センサー (SHARP GP2Y0A21YK) を使って距離を測ります。

[参考]

http://yutapon.hatenablog.com/entry/2014/02/09/192603

http://www.pwv.co.jp/~take/TakeWiki/index.php?Arduino%2FArduino%E3%81%AE%E5%8B%89%E5%BC%B7%E3%81%AF%E3%81%98%E3%82%81%E3%81%BE%E3%81%97%E3%81%9F#ab78b6e7

気を付けるのは、analogRead の値が 4096 段階なところ。

上記ページのような Arduino のサンプルで出ている式に突っ込む場合は、analogRead が 1024 段階なのを考慮して 4 で割ってから。

実際に取ってみましたが、大体あってる気がする。
たまに外れ値というか、デカすぎる値が帰ってくるので、適宜丸めたりは必要かも。

データシートとかを見ると、これの測定範囲は 10 – 80 cm だそうなので、それ以外は丸めればおkかな?

image

distance

// Main.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "arduino.h"

int _tmain(int argc, _TCHAR* argv[])
{
    return RunArduinoSketch();
}

int tmp;
int distance;

void setup()
{

}

// the loop routine runs over and over again forever:
void loop()
{
	tmp = analogRead(0)/4;

	if (tmp < 4) tmp = 4;

	// 距離を計算(cm)
	distance = (6787 / (tmp - 3)) - 4;
	Log(L"distance : %dn", distance);
	delay(100);
}
</pre>

Windows for IoT で analogRead / analogWrite

[参考] : http://kousaku-kousaku.blogspot.jp/2008/05/arduino_26.html

(Arduino 界隈では超有名な建築発明工作ゼミ様です。)

可変抵抗の入力値を基に、LED の明るさを変えてみるだけのシンプルなサンプルです。

Arduino では analogRead() は 1024 段階でしたが、Intel Galileo は 4096 段階になっていますね。
[参考] : http://arduino.cc/en/ArduinoCertified/IntelGalileo

analogWrite() は情報見つからないのですが、とりあえず 256 段階ってことで、以下の例では 16 で割って使ってます。4096 段階出せてるような気がしないでもないけれど。。。

あと、PWM 出来るピンは 3, 5, 6, 9, 10, 11 です。
ピンの番号のところに ~ がついてるのが PWM 対応ってことみたい。

回路図をサクっと書ける fritzing だと、13 も ~ ついてるんですが、ダメそう。
Visual Studio でなぜか以下のようなエラーが出ておかしいなぁと思って 30 分くらい悩んだのでした。

First-chance exception at 0x775D0F63 in analogIO.exe: Microsoft C++ exception: _arduino_fatal_error at memory location 0x00CCF69C.

analogRead

// Main.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "arduino.h"

int _tmain(int argc, _TCHAR* argv[])
{
    return RunArduinoSketch();
}

int led = 3;  // This is the pin the LED is attached to.
int val = 0;

void setup()
{

}

// the loop routine runs over and over again forever:
void loop()
{
	val = analogRead(0);
	Log(L"analogRead value = %dn", val);
    analogWrite(led, val/16);
    Log(L"analogWriten");
    delay(100);
}

サーバー障害

このブログをホストしているレンタルサーバーの H/W 障害により、2 か月近くのデータが消失した模様です。

投稿内容自体はほぼ復旧させましたが、その間の投稿は URL が変わっていたり、コメント消えていたりするのでご了承ください。

個人的には、Rep2X の記事の PV が 5 万超えていたのに 35000 ほどまで戻ったのが残念ですが、まあ全部飛ばなかっただけマシですね。皆さんもバックアップはお忘れなく。

Windows for IoT on Intel Galileo 上で使える標準コマンドとか、フォルダの階層とか。

system32 配下の exe を漁るとこんな感じ。

tasklist とか無いし、だいぶ少ない?(個人的に使いそうなのが太字

C:windowssystem32>dir *.exe

Volume in drive C has no label.

Volume Serial Number is 30F2-6459

Directory of C:windowssystem32

2014-07-31 19:15:18 102,176 appverif.exe

2014-07-31 19:15:18 18,432 attrib.exe

2014-07-31 19:15:18 22,528 ARP.EXE


2014-07-31 19:15:26 194,848 audiodg.exe

2014-07-31 19:15:18 53,248 auditpol.exe

2014-07-31 19:15:28 9,728 backgroundTaskHost.exe

2014-07-31 19:15:16 136,416 AuthHost.exe

2014-07-31 19:15:26 28,672 BackgroundTransferHost.exe

2014-07-31 19:15:16 48,640 CertEnrollCtrl.exe

2014-07-31 19:15:18 67,072 certmgr.exe

2014-07-31 19:15:16 25,600 CheckNetIsolation.exe

2014-07-31 19:15:18 22,016 chkdsk.exe


2014-07-31 19:15:18 310,272 bcdedit.exe


2014-07-31 19:15:18 312,832 cmd.exe

2014-07-31 16:54:36 16,144 csrss.exe

2014-07-31 19:15:16 793,600 autochk.exe

2014-07-31 19:15:18 73,728 devcon.exe

2014-07-31 19:15:16 17,848 dllhost.exe

2014-07-31 19:15:18 13,312 finger.exe

2014-07-31 19:15:18 27,136 filever.exe

2014-07-31 19:15:18 29,184 findstr.exe


2014-07-31 19:15:18 22,528 fltMC.exe

2014-07-31 19:15:18 17,408 ftpd.exe

2014-07-31 19:15:18 12,288 HOSTNAME.EXE

2014-07-31 19:15:18 11,264 httpsrv.exe

2014-07-31 19:15:18 30,720 ipconfig.exe

2014-07-31 19:15:18 591,872 imagex.exe

2014-07-31 19:15:18 19,968 kill.exe

2014-07-31 19:15:16 25,600 logonuitext.exe

2014-07-31 16:54:36 34,168 lsass.exe

2014-07-31 19:15:18 28,672 mindwm.exe

2014-07-31 19:15:18 6,656 minloadlib.exe

2014-07-31 19:15:18 16,384 minpart.exe

2014-07-31 19:15:20 12,288 MinShellHost.exe

2014-07-31 19:15:16 14,336 minshelltext.exe

2014-07-31 19:15:18 14,848 MRINFO.EXE

2014-07-31 19:15:18 11,776 mwstartnet.exe

2014-07-31 19:15:16 64,000 MuiUnattend.exe

2014-07-31 19:15:18 93,696 mwdbgsrv.exe

2014-07-31 19:15:18 118,784 mwumdh.exe

2014-07-31 19:15:18 46,080 net.exe

2014-07-31 19:15:18 139,776 net1.exe

2014-07-31 19:15:18 81,920 netsh.exe


2014-07-31 19:15:18 32,768 NETSTAT.EXE

2014-07-31 19:15:18 78,848 nslookup.exe

2014-07-31 19:15:18 16,896 PATHPING.EXE

2014-07-31 19:15:18 19,456 PING.EXE

2014-07-31 16:55:54 123,904 poqexec.exe

2014-07-31 16:54:34 197,632 PkgMgr.exe

2014-07-31 19:15:18 16,384 regsvr32.exe

2014-07-31 19:15:18 61,440 reg.exe

2014-07-31 19:15:18 19,968 ROUTE.EXE


2014-07-31 19:15:16 30,008 RuntimeBroker.exe

2014-07-31 19:15:18 59,904 sc.exe

2014-07-31 19:15:18 46,592 setx.exe

2014-07-31 19:15:18 9,728 shutdown.exe


2014-07-31 19:15:18 9,728 sleep.exe

2014-07-31 16:54:36 105,992 smss.exe

2014-07-31 16:54:36 328,568 services.exe

2014-07-31 19:15:16 31,640 svchost.exe

2014-07-31 16:54:34 31,232 sxstrace.exe

2014-07-31 19:15:18 10,752 TCPSVCS.EXE

2014-07-31 19:15:18 15,872 TRACERT.EXE


2014-07-31 19:15:18 31,232 telnetd.exe

2014-07-31 19:15:18 30,720 tlist.exe

2014-07-31 19:15:18 80,384 tracelog.exe

2014-07-31 19:15:16 21,504 userinit.exe

2014-07-31 19:15:26 30,720 VaultCmd.exe

2014-07-31 19:15:16 422,400 wimserv.exe

2014-07-31 19:15:16 112,640 wininit.exe

2014-07-31 19:15:16 458,752 winlogon.exe

2014-07-31 19:15:18 39,424 xcopy.exe


2014-07-31 19:15:18 279,040 xperf.exe

2014-07-31 19:15:18 522,240 WWAHost.exe

2014-07-31 16:54:34 5,746,616 ntoskrnl.exe

2014-07-31 13:50:32 9,728 Galileo_eboot.exe

2014-07-31 13:50:32 8,704 SetComputerName.exe

2014-07-31 13:50:32 12,288 Settime.exe

78 File(s) 12,792,168 bytes

0 Dir(s) 13,582,868,480 bytes free

78 個しかないっすね。試しに手元の Win 8.1 で同じことしたら、519 個とかありました。

あと、基本的なフォルダ構成もだいぶ通常の Windows から簡略化されてますね。

上記 System32 以下の .exe 以外はほぼ何もないと思っていいかも。

参考までに dir /S して全ファイルを書き出したやつ置いときます。

C:

+ efi

+ Program Files

+ <ほぼ空>

+ ProgramData

+ test <== VS で作ったプログラムが保存されるっぽい?

+ Tools

+ Users

+ Administrator

+ <ほぼ空>

+ Default

+ Public

+ Windows

+ Boot

+ CbsTemp

+ en-US

+ Fonts

+ Globalization

+ inf

+ Logs

+ rescache

+ ServiceProfiles

+ servicing

+ System32

+ SystemResources

+ Temp

+ ToastData

+ WinSxS

DISM とかでいじれるのかなぁ…と思ったけど、そもそも DISM 自体ないじゃん。

Embedded 系はいじったことないのでさっぱりだー。

Windows for IoT on Intel Galileoで L チカ

Visual Studio 2013 で [ファイル] – [新規作成] – [プロジェクト] を選択。

テンプレートの [Visual C++] 内に Windows for IoT とか言うのがあります。

image

プロジェクト名をつけて適当に作成。“arduino.h” がないよーと怒られましたimage

どうも Galileo 用の SDK を NuGet で入れるらしい。

[ツール] – [NuGet パッケージ マネージャー] – [ソリューションの NuGet パッケージの管理]あたりから、”Galileo C++ SDK” をインストール。

image

これで準備は完了。

デバッグ対象はプロジェクトのプロパティから [構成プロパティ] – [デバッグ] で “リモート サーバー名” の箇所を Galileo のホスト名に変更するだけ。

image

F5 でデバッグ開始すると、資格情報の入力画面になるので、ログオンすれば OK。

あとは Visual Studio で普段のように開発出来るので、ガリガリやりましょう。

LED

// Main.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "arduino.h"

int _tmain(int argc, _TCHAR* argv[])
{
    return RunArduinoSketch();
}

int led = 13;  // This is the pin the LED is attached to.

void setup()
{
    // TODO: Add your code here
    
    pinMode(led, OUTPUT);       // Configure the pin for OUTPUT so you can turn on the LED.
}

// the loop routine runs over and over again forever:
void loop()
{
    // TODO: Add your code here

    digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
    Log(L"LED OFFn");
    delay(1000);               // wait for a second
    digitalWrite(led, HIGH);   // turn the LED on by making the voltage HIGH
    Log(L"LED ONn");
    delay(1000);               // wait for a second
}