2014年2月23日日曜日

xbee wifiに関する走り書き

xbee wifiをソケット通信で普通に使うのに必要そうな情報

・受信と送信のソケットを同時に開くことは(多分)できない。送信(または受信)が終わったらそのたびにソケットを閉じること

・基本だがsocket.accept()の直後に(ウェイトを入れずに) socket.recv() などでデータを取り出そうとしてもエラーになる。バッファがちゃんと溜まるまで(人間にとって)ちょっとでも待つべき。

・連続してデータの送受信を繰り返したいとき、デフォルトの設定だと受信(PC←xbee)で10秒、送信で多分60秒くらい待つようになっている。「Network」のTMとTSが受信と送信のタイムアウトになっているのでこれらを限界まで小さくすると待ち時間が減らせる(とりあえず0.5秒に1回程度の送受信ができる)

・「serial Interfacing」のROもタイムアウトになっている。小さくすると通信が多分速くなる。

・送受信のソケットを再生成するのにも時間が必要なので繰り返す場合は生成前にウェイトを入れること。

とりあえず上記で(電源系の問題を除けば)普通のxbeeと同じように使えるはず。詳細は来週

2013年12月23日月曜日

opencvで処理した画像(動画)をandroid端末に送信して表示するあたりのメモ

以前、SimpleMjpegViewというライブラリを利用してwebcamの映像をandroid上に表示したが、今回は別の手法で(もっと原始的な形で)端末上に動画を表示する。

前回:

 webcam → (mjpegストリーム) → android端末

今回:
 webcam → PC(c++(Opencv)) → (jpeg) → android端末

 今回の方でやれば重たい画像処理を母艦PCやraspiで行ってその処理結果をandroidに送信できる。

とりあえず結果↓

 いいwebカメラ(ロジクールの1万のやつ)なのに若干カクつく。サイズを小さくしたり、jpg品質落とすことでましにできると思われる



opencvを使ったりしているのでMITライセンスになります。
強制終了前提のコードなのであくまでご参考、自己責任でお願いします。

↓MITLicenceに基づく告知

Some codes in OpenCV are utilized.
Therefore, before downloading, you have to agree to the following license.
//////////////////////////////////////////////////////////////////////////////////
IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
By downloading, copying, installing or using the software you agree to this license.
If you do not agree to this license, do not download, install,
copy or use the software.

                    Intel License Agreement

            For Open Source Computer Vision Library
Copyright (C) 2000, Intel Corporation, all rights reserved.
Third party copyrights are property of their respective owners.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
  • Redistribution's of source code must retain the above copyright notice,
    this list of conditions and the following disclaimer.
  • Redistribution's in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.
  • The name of Intel Corporation may not be used to endorse or promote products
    derived from this software without specific prior written permission.
This software is provided by the copyright holders and contributors "as is" and
any express or implied warranties, including, but not limited to, the implied
warranties of merchantability and fitness for a particular purpose are disclaimed.
In no event shall the Intel Corporation or contributors be liable for any direct,
indirect, incidental, special, exemplary, or consequential damages
(including, but not limited to, procurement of substitute goods or services;
loss of use, data, or profits; or business interruption) however caused
and on any theory of liability, whether in contract, strict liability,
or tort (including negligence or otherwise) arising in any way out of
the use of this software, even if advised of the possibility of such damage.

 ライセンス告知ここまで


まずC++側、環境はwin7 64bit Gcc(MINGW) opencvが必要(今回は2.4.7使用)


概要 : 
0、ソケット構築、android側からのアクセスを待ち受け

1、opencvで動画や画像フレームを取ってきて好きなように処理して最終的にjpgに圧縮 jpgのデータはvector<uchar>で保存される

2、jpgのデータサイズ(何バイトか)をバイナリ(intだと4byte)にする、地味なビット演算処理が必要(もっとスマートにできそうだが)。要は送信先でintとしてデータサイズを取れるようにする。これがわからないとあちらで正確なバッファが作れない

3、 jpgデータサイズとjpgデータをandroidに送る。
   vector<uchar>をソケットで送信できるconst char*に変換する部分の調査に時間がかかった(初見だとここで詰まる可能性あり)

4、おわり。上記処理をループさせている


(参考ソース)

#include <iostream>
#include <windows.h>
#include <winsock2.h>

#include "opencv2\highgui\highgui.hpp"
#include <vector>
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <sstream>

using namespace cv;
using std::stringstream;
int main()
{
~~~~~~~~~~~~~



ここにソケットの初期化(bindまで)の処理が入るが諸事情(参考文献の丸写しのため)によりカット
参考文献 : http://blog.pusheax.com/2013/07/windows-api-winsock-create-your-first.html

windowsソケットプログラミング(C++)の基本説明



~~~~~~~~~~~~~



    //////////////// start(original from here)

    int lis=listen(u_sock,3);
    /////////////// waiting client data   
   
    //opencvによる動画の取得
    VideoCapture cap(0);
   
    //メインループ
    while(1){
        stringstream ss;
        //クライアントからの受信用バッファ、何らかのコマンド受け取りを想定
        int receive_buf_size = 20;
        char vect[receive_buf_size];

        std::cout <<"start waiting client command" <<std::endl;
        //client adress define
        struct sockaddr_in client;
        int len = sizeof(client);

        //client connect waiting・・・クライアント定義、待ちうけはループ外でもいいかもしれない
        SOCKET sockCli = accept(u_sock, (struct sockaddr *)&client, &len);   
        //クライアントから20byte何かしら受け取る、必要に応じてパース
        int get=recv(sockCli,vect,receive_buf_size,0);
        if(get==SOCKET_ERROR){
            std::cout<<"Error in Receiving: "<<WSAGetLastError()<<std::endl;
            continue;
        }

        // receivedta(command) process 受け取ったデータの処理、Stringstreamを使って文字列にして表示
        Sleep(10);//data receive waiting クライアントからのデータを待つ
        std::cout <<"end receive"<<std::endl;
        ss.clear();
        String res_string;
        ss << vect;
        ss >> res_string;

        if (get <0){
            std::cout <<"no receive data"<<std::endl;
            //    break;
        }else{
            std::cout <<"receive data : ";
            std::cout <<res_string<<std::endl;
        }

        ss.clear();

        /////////// image data send process 画像の取得と送信   
        Mat frame;

        cap >> frame;

        int framesize = 1000000; ///Max size

        //送信データ用のバッファ
        vector<uchar> bufv;
        //ちゃんと変換されたかの確認用
        bool imok;
        //jpg圧縮用のパラメータ設定
        vector<int> jpgparam;
        jpgparam.push_back(CV_IMWRITE_JPEG_QUALITY);
        jpgparam.push_back(20); /* jpeg quality */
        //ここまでパラメータ設定

        //圧縮した画像をバッファに入れる
        imok=imencode(".jpg",frame,bufv,jpgparam);
       
        //Mat sframe;

        std::cout<<bufv.size()<<std::endl;

        //Send some message to remote host

        ///////////// jpg data sending
        std::cout <<"start sending"<<std::endl;

        std::cout <<"send size"<<bufv.size() <<std::endl;
        std::cout <<"senddata "<<bufv.data()<<std::endl;
        std::cout <<"start sending"<<std::endl;

        // sizeof jpg send to java  画像サイズ(何バイトか)をクライアントに送信する
        // intの数値をバイナリ(4バイト)に変換する。泥臭い処理
        vector<uchar> jpgsize_send(4);
        int jpgsize=bufv.size();
       
        //int(4byte)のバイナリを1バイトずつ送信用の配列に入れる。エンディアン注意   
        for (int i=0;i<4;i++){
        jpgsize_send[3-i]=(jpgsize >>(i * 8) );
        }
        ////////// bynary genelator

        std::cout <<"data size : "<< jpgsize <<"type size"<<sizeof(jpgsize) <<std::endl;
        //std::string siz=std::to_string(jpgsize);
        std::cout <<"type size"<<sizeof(jpgsize_send) <<std::endl;
       
        /////////////////////// send data to android
        //vectorをcharに変換してクライアントに送信、
        //ロベール本によるとreinteroret_castの使用には注意が必要らしい

        //ヘッダ部(今回は画像データバイト数のみ)送信
        int smsg_header=send(sockCli,reinterpret_cast<const char*>(jpgsize_send
                    .data()),jpgsize_send.size(),0);
        //画像データ送信
        int smsg=send(sockCli,reinterpret_cast<const char*>(bufv.data()),bufv.size(),0);

        ///////////////////////


        if(smsg==SOCKET_ERROR){
            std::cout<<"Error: "<<WSAGetLastError()<<std::endl;
            WSACleanup();
        }

        /*
        //get=recv(u_sock,vect,512,0);
        if(get==SOCKET_ERROR){
        std::cout<<"Error in Receiving: "<<WSAGetLastError()<<std::endl;
        }
        //std::cout<<vect<<std::endl;
        */
    }
    closesocket(u_sock);

~~~~~ ここから終了処理を適当に

}

------------------------------------------  C++ここまで



Java(Android側)、eclipse4.3(Kepler Service Release 1) androidosは4.4が入ったnexsus7(2012)


概要:androidのプロジェクトで新しいクラスを作って以下のソースをコピペして、パラメータ(アドレス、ポートなど)設定してからonCreateに他のTextViewみたいに設定するだけで動作する(コピーした時点でレイアウトのxmlのカスタム&~のところに出てくるので普通にレイアウトできる)。はず。
インターネットの許可が必要(忘れてちょっと詰まった)


ゴミコードが混ざっているのはご愛嬌。あくまで個人的なメモなので。
自動生成とかどうでもいいところでないところは太字にしてある



基本的なsurfaceviewのクラスにネットから画像を取ってくるクラスを接続している。サーフェスの更新をマルチスレッドでずっと回してその中でさらにネットから画像を取ってくるスレッドを走らせる。
はっきり言ってバグの温床なのでいろいろ注意、ちなみに強制終了前提




コード

(Class Mysurfaceview.java)


package com.example.jpeg_binaly_client;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.AsyncTask;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.ImageView;
import android.widget.TextView;

public class Mysurfaceview extends SurfaceView implements SurfaceHolder.Callback, Runnable {

    //ビューの常時更新には普通のマルチスレッドを使う
    Thread thread;


    SurfaceHolder holder;

    //更新される画像
    public Bitmap tcpbmp;
    //ネットワークスレッドの更新可能シグナル
    public int imggetflag;

    public Mysurfaceview(Context context, AttributeSet attrs ) {//太字の部分がないとxml関連のエラーになる
        super(context,attrs);
        // TODO 自動生成されたコンストラクター・スタブ
        getHolder().addCallback(this);
        this.holder = getHolder();

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        // TODO 自動生成されたメソッド・スタブ

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO 自動生成されたメソッド・スタブ
        thread = new Thread(this);
        thread.start();


    }

    @Override
    public void run() {
        while(true){//無限ループ注意

            doDraw(getHolder());

            try {
                Thread.sleep(100);
//通信も絡むので若干待つ、が、ここで待たなくてもいいかも
             } catch (InterruptedException e) {
                // TODO 自動生成された catch ブロック
                e.printStackTrace();
            }
        }

    }


    //描画用関数
    public void doDraw(SurfaceHolder holder) {
       
        tcpbmp =null;
        //画像取得スレッドを走らせる
        tcp_getimage3 jpgg= new tcp_getimage3();
        jpgg.execute("nl");
//特にコマンドなんかが無ければ何でもいい

        int cnt=0;
        int timeout =10;
        int waiting=10;
   //画像取得が終わるまで待つ
        while (imggetflag ==0){
            System.out.println("waiting get...");

            //一定時間待つ
            try {
                Thread.sleep(waiting);
            } catch (InterruptedException e1) {
                // TODO 自動生成された catch ブロック
                e1.printStackTrace();
            }
            cnt += waiting;
            //一定時間待っても更新シグナルがこない場合はタイムアウトとする
            if(cnt>timeout){
                System.out.println("waiting get...timeout");
                break;}
        }

        //更新可能(フラグが出ており、bmpがカラでないとき)なら画像を更新
        if(imggetflag==1 && tcpbmp !=null){
             Canvas canvas = holder.lockCanvas();

             canvas.drawBitmap(tcpbmp, 0, 0, null);
          
            holder.unlockCanvasAndPost(canvas);

        }
        //フラグを0にする
        imggetflag=0;

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO 自動生成されたメソッド・スタブ

    }

    //ネットワークからの画像取得クラス
    public class tcp_getimage3 extends AsyncTask<String,Integer,byte[]>{


        public String ipaddr="(IPアドレス)"; 

        int port_bg = (ポート番号);
       
        Socket socket_bg = null;

        DataOutputStream out=null;
        BufferedReader inp=null;

        DataOutputStream out_bg=null;
        DataInputStream inp_bg=null;
       

        //エラー出力用のカラのバイト列
        public byte[] reterr =null;


        public int cnt =0;
        public int serverflag_bg=0;  //1だとサーバ無しとみなしている。

        public tcp_getimage3(){
            super();
        }



        @Override
        protected byte[] doInBackground(String... val) {
            // TODO 自動生成されたメソッド・スタブ
           
            if (serverflag_bg==0){

                try{

                    try{
                        InetSocketAddress addr = new InetSocketAddress(ipaddr, port_bg);
                        socket_bg =new Socket();
                        socket_bg.connect(addr,50);//タイムアウトの時間は重要、繋がらなかったらさっさと切り上げて再接続したほうが早いので、とりあえず短いほうがいい。でも短すぎると電波環境によっては通信できない可能性もある。

                    }catch(SocketException e){
                        e.printStackTrace();
                        Log.i("network", "socket not connect");
                        return reterr;  //タイムアウトしたらさっさとスレッドを閉じにいく

                    }catch(Exception e){  ///調べたがこの例外じゃないと死ぬ。重要
                        e.printStackTrace();
                        Log.i("network", "その他のネットワーク系エラー");

                        return reterr;

                    }


                    Log.i("network", "sendstart ");
         ///////////////////////////////////////ここからが実質的な処理
                    DataOutputStream out_bg = new DataOutputStream(socket_bg.getOutputStream());

                    ////// データの送信、コマンドなどが無いなら何でもいい
                    out_bg.writeBytes("jpgg");

                    byte[] buf =new byte[1000000]; //最大データサイズ(byte)c++と合わせる

                    int readflag;

                    //データ読み込み↓
                    InputStream is =socket_bg.getInputStream();
                    DataInputStream inp_bg = new DataInputStream(is);

                    Thread.sleep(50);//4バイト受信できるくらい待つ必要がある


                    int receive_size=inp_bg.readInt(); //C++からヘッダ(画像のバイト数)情報を受け取る

                    //画像容量分のバッファを定義する、ここら辺が正確じゃないとBMPがちゃんと生成されない

                    byte[] buffull =new byte[receive_size];

                    //画像データをすべて受信するまで待つ
                    if (inp_bg.available() != receive_size){

                        Thread.sleep(10);
                    }


                    //受信データをバッファに入れる、ループしなくていいかも
                    while(inp_bg.available()>=0){
                        System.out.print("datasize is ");
                        System.out.println(receive_size);

                        //受信データをバッファに入れる。何らかの方法で全部のバッファを受け取ったか確認する必要がある
                        inp_bg.read(buffull,0,receive_size);
                        if(inp_bg.available()==0){break;}

                    };
                   
                    out_bg.close();
                    inp_bg.close();
                    socket_bg.close();


                    //バイナリをビットマップに変換する。jpgも勝手に解析してBMPにしてくれる
                    Bitmap bmplocal=null;
                    bmplocal=BitmapFactory.decodeByteArray(buffull,0,buffull.length);
                    System.out.println(buffull.length);←データサイズと同じか一応比較

                    //グローバルのbmpに変換したBMPを入れる
                    tcpbmp = bmplocal;


                    ///////////////////////////////////////ここまでが実質的な処理
                    //バイナリを出力する。onPostExecuteでの処理はこっちに持ってきても問題ない
                    return buffull;

                } catch (IOException e) {
                    e.printStackTrace();
                    System.out.println("io err"+e);

                    serverflag_bg=1;
                    return reterr;
                }catch (Exception e) {
                    e.printStackTrace();
                    Log.i("network", "Iilligal ");
                    System.out.println("io err"+e);

                    serverflag_bg=1;
                    return reterr;

                }

            }else{
                Log.v("tcp","noserver");
                return reterr;
            }
        }

        protected void onPostExecute(byte[] result) {

            //呼び出し元のスレッドに描画更新OKのシグナルを出す
            if (result !=null){
                imggetflag=1;}
            else{
                //何らかのエラーでバイナリが無い場合はシグナルを出さない
                imggetflag=0;
            }

            System.out.println("end tcp thread");


        }

    }
}











2013年12月7日土曜日

自分用Vim設定メモ

Vimの設定に関するメモ。主にc++(時々pythonも)でプログラム書くのをイメージ
GVimを(http://www.kaoriya.net/)入手

■ウィンドウズのキーバインドを変えてしまう
無変換キーとか変換キーなんて使わないのでそれぞれEscキーと半角/全角キーに割り振ってしまう。vimではEscキーを使いまくるのでこの変更は重要
↓これで簡単に変更できる(レジストリ変えるので再起動が必要)
http://www.forest.impress.co.jp/library/software/changekey/

■まず以下を vimrc(編集→起動時の設定 で開くファイル)にコピー

nnoremap - $
nnoremap <F11> :make run <Enter>
nnoremap <F5> :!python % <Enter>
set number
set incsearch
set autochdir
set wildmenu wildmode=list:full
copen

ここからメモ
・ノーマルモードで「-」を押すとカーソルが行末に行くこれで0が行頭 -が行末移動になる(隣り合っててわかりやすい)

・F11でメイク→実行 ちゃんとrunができるようにmakefileを書くこと
・ F5でpython実行

・左に行数を表示

・インクリメンタルサーチをオン(いい感じに検索できる)

・set autochdir : 開いているファイルのある場所にカレントディレクトリを移動する。基本的に必須

・ set wildmenu wildmode=list:full : コマンドモード(「:」入力以降)で補間する

・ copen デバッグウィンドウ的なものを開く。便利

■ctags(Exuberant ctags)を使う
主要参考ページ
http://nanasi.jp/articles/others/ctags.html

・ここ(http://hp.vector.co.jp/authors/VA025040/ctags/)でctagsをダウンロード
・exeを環境変数Pathにあるフォルダに入れる
・autochdir がオンになっていれば編集したいファイル開いてGvimのタグ作成ボタンなどでタグを作れる
・宣言と同時に値を入力している変数はタグをつけてもらえない模様

Taglistを入手(参考:http://nanasi.jp/articles/vim/taglist_vim.html)

(GVimフォルダ)\runtime\plugin の中に.vimを \runtime\doc の中に.docをコピーするだけ

・「:Taglist」で画面左もプロジェクトエクスプローラー的なものが出る(eclipseのほうが高性能だが)

2013年11月22日金曜日

windows-mingwでopencv環境構築 走り書き


基本部分参考
http://nenadbulatovic.blogspot.jp/2013/07/configuring-opencv-245-eclipse-cdt-juno.html

これの「mingw32-make」 を「mingw32-make install」にする。

「bin」フォルダにサンプル「c-example-contours.exe」などができていて動作することを確認すること。動作すればここまではOK


サンプル参考 : これが動作すれば環境構築完了
 http://stackoverflow.com/questions/10860352/getting-started-with-opencv-2-4-and-mingw-on-windows-7

上記binフォルダを環境変数 PATHにしておくこと

 -I オプションは "C:\(opencv)\build\include" opencv解凍したらbuildフォルダがある(mingwの場合使用しなさそうだから見逃した)。その中のincludeを-Iとすること他のincludeとかでは適切な階層にヘッダが無いことがある。

-Lは cmakeで生成されたopencvの「lib」フォルダ、-lオプションのバージョンもそのまま写さないように注意

これで最新バージョン(2.4.7)動作確認。細かい変更やらなにやら(解凍後の「build\x86」にmingw向けのbinが無いなど)で以前と勝手が異なっており1日つぶしたのでメモ 
 

2013年10月27日日曜日

ラジコン(の様な何か)できたよ~ ヽ(゜ω゜)ノ Ⅱ

ここ数ヶ月の休日をドブに捨てて何とかラジコンらしきものができてきたのでうp

↓概観。臓物を乗せただけ。これからボーナスをはたいた3Dプリンタが役に立つはず・・・

















↓コントローラ。androidにカメラ映像を出すのが鬼門だった。神が作りたもうたライブラリを導入することでなんとか解決。

















↓操作風景(コントローラ)、無駄にマルチタッチなどを使用。
ちなみにソナー(ではないが距離がわかるセンサ)とかコンパス、ジャイロのボタンなんかは未実装・・・(´・ω・) 一応単体動作は確認済みだけどまたそのうち実装すればいいよね・・・どうせコマンド送信するだけだし。。

 

 ↓操作風景(ラジコン側)。↑の動画を見てもわかるように操縦系がやや糞仕様。これでもだいぶましになったほうだが。

 

※諸事情(部屋の散らかり具合)により狭いエリアでの撮影となっております。







2013年10月20日日曜日

android(SimpleMjpegView)でwebcamからのストリーミングを見たときのメモ

休日2日(18時間程度)つぶしてどうにかウェブカメラ映像をタブレット上で見れるようになってのでその際のメモ、公開されているライブラリを取り込んだだけだが・・・

一応ライブラリ(SimpleMjpegView)のライセンスに基づく告知

This software is based in part on the work of the Independent JPEG Group.
Moreover, for decoding MJPEG, some codes in OpenCV are utilized.
Therefore, before downloading, you have to agree to the following license.
//////////////////////////////////////////////////////////////////////////////////
IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
By downloading, copying, installing or using the software you agree to this license.
If you do not agree to this license, do not download, install,
copy or use the software.
                    Intel License Agreement

            For Open Source Computer Vision Library
Copyright (C) 2000, Intel Corporation, all rights reserved.
Third party copyrights are property of their respective owners.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
  • Redistribution's of source code must retain the above copyright notice,
    this list of conditions and the following disclaimer.
  • Redistribution's in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.
  • The name of Intel Corporation may not be used to endorse or promote products
    derived from this software without specific prior written permission.
This software is provided by the copyright holders and contributors "as is" and
any express or implied warranties, including, but not limited to, the implied
warranties of merchantability and fitness for a particular purpose are disclaimed.
In no event shall the Intel Corporation or contributors be liable for any direct,
indirect, incidental, special, exemplary, or consequential damages
(including, but not limited to, procurement of substitute goods or services;
loss of use, data, or profits; or business interruption) however caused
and on any theory of liability, whether in contract, strict liability,
or tort (including negligence or otherwise) arising in any way out of
the use of this software, even if advised of the possibility of such damage.

 ライセンス告知ここまで


参考文献:
1、 neuralassemblyのメモ 様:
http://neuralassembly.blogspot.jp/2012/12/androidwifimjpeg.html
今回はTakashi Kanamaru 氏が公開されているありがたいライブラリ「SimpleMjpegView」を自分のプロジェクトに使う。
 
2、yahoo知恵袋 Android NDKを使った開発環境の構築
http://note.chiebukuro.yahoo.co.jp/detail/n136598
今回の話は実質的にNDKに関するものなので重要。


・今回の目的 : 自作androidアプリに(ゲームボーイ的な感じで)ウェブカメラからのリアルタイム映像を映す

・手法 : 数日粘ったが自分の手には負えそうも無かったので参考文献1のライブラリを利用させて頂くことに

・利用させて頂き方 : 
※事前にNDKが使える状態になっていること

1、 https://bitbucket.org/neuralassembly/simplemjpegview からライブラリをダウンロード、展開→eclipseへプロジェクトをインポート

2、インポートしたプロジェクトが正常に動く(ウェブカメラのストリーミングが見られること)ことを確認する


3、問題なければMjpegView.java と MjpegInputStream.javaを自分のプロジェクトのsrcにコピー。jniフォルダもコピー(自分は他にndkを使っていなかったから問題なかったが自作ndkがある場合はmakeファイル内容などを上手くマージしたりする必要があるかもしれない)

4、 コマンドプロンプトで自分のプロジェクトのフォルダに移動(cd (プロジェクトフォルダパス))して「C:\\android_ndk\\android-ndk\\ndk-build.cmd(筆者の場合)」を実行。要はjniフォルダ内のcファイルをビルドする。旧バージョンだとcygwinからやらないといけないようだが参考文献1のリンクなどを参照のこと

※重要 : ここで半日詰まった。参考文献2にもあるがjniフォルダ内のcファイルとヘッダファイル内の一部を自分のプロジェクト名に(規則にのっとって)改名する必要がある。↓
「JavaとC/C++(或いはその他の言語)によるプログラムの間で相互に連携をするためには、JNI(Java Native Interface)という仕様に沿う必要があります。この仕様では、C/C++側の関数名は頭にJava_を付け、その後ろにパッケージ名、クラス名、 メソッド名を並べ、ドットをアンダーバーに置き換えた名前にします。」(参考文献2より引用)

コピーしてきた時点でcファイルとヘッダファイル内の関数名はインポートしたSimpleMjpegViewプロジェクトのパッケージになっているので自分のパッケージ名にすべて変更する。これをしないとリンクエラーが発生する(そんなnativeメソッド無いよ。というエラーが発生する)。まとめて変更するための情報が参考文献2にあるが今回は手動で変換した

5、インポートしたプロジェクト内のMjpegActivity.javaのソースを参考に必要な部分を自分のライブラリに書き足す。最低限以下の部分をコピペすれば動作はする(ポーズなどで落ちるが)
5-1 onCreate内
     mv =(MjpegView) findViewById(R.id.mjpegView1);
         if(mv != null){
                mv.setResolution(640,480);
            }
            new DoRead().execute(URL);

 5-2 activityクラス内、onCreateなどと並列に
    public class DoRead extends AsyncTask<String, Void, MjpegInputStream> {
        protected MjpegInputStream doInBackground(String... url) {
            //TODO: if camera has authentication deal with it and don't just not work
            HttpResponse res = null;
            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpParams httpParams = httpclient.getParams();
            HttpConnectionParams.setConnectionTimeout(httpParams, 5*1000);
            Log.d(TAG, "1. Sending http request");
            try {
                res = httpclient.execute(new HttpGet(URI.create(url[0])));
                Log.d(TAG, "2. Request finished, status = " + res.getStatusLine().getStatusCode());
                if(res.getStatusLine().getStatusCode()==401){
                    //You must turn off camera User Access Control before this will work
                    return null;
                }
                return new MjpegInputStream(res.getEntity().getContent());
            } catch (ClientProtocolException e) {
                e.printStackTrace();
                Log.d(TAG, "Request failed-ClientProtocolException", e);
                //Error connecting to camera
            } catch (IOException e) {
                e.printStackTrace();
                Log.d(TAG, "Request failed-IOException", e);
                //Error connecting to camera
            }
            return null;
        }

        protected void onPostExecute(MjpegInputStream result) {
            mv.setSource(result);
            if(result!=null) result.setSkip(1);
            mv.setDisplayMode(MjpegView.SIZE_BEST_FIT);
            mv.showFps(false);
        }
    }


結果:
そりゃあ所詮無線のカメラなので若干のラグはあるがかなりスムーズ。Takashi Kanamaru 氏は間違いなく神



以上。結局NDKの基本的なところが全くわかっていなかったのが時間をとった原因だったり・・・



2013年10月13日日曜日

秋月サーボの大事な覚書き(arduinoなんかで使う際の設定値)

みんな大好き秋月サーボ(GWSサーボ)の使い方についてのメモ

参考文献:
1、GWS(サーボの会社)にあるサーボのアプリケーションノート(初見だと場所がわかりにくい)
http://www.gws.com.tw/english/product/servo/sat%20form.htm

2、Arduino 日本語リファレンスのservoのページ

要点 : 秋月で売ってるGWSのサーボを使うときはServoライブラリのデフォルトのパルス幅(min:544、max:2400)を使ってはいけない。特にmin側はぶっ壊れるかもしれないので注意

参考文献1によるパルス幅:

0.8~2.2 マイクロ秒 (800 ~ 2200ms)

※以下、個人的なメモ
 
筆者用設定値(スペック超えているため多分サーボが壊れる。自己責任で・・というよりこの設定を使用してはいけない):

GWSMICRO/2BBMG/F (小さめの1500円のやつ) → 800~2500
・筆者環境でこの設定じゃないと180度回らなかったので破損覚悟で設定。

GWSPIC+F/BB/F(最小サイズの900の方) → 800~2200
・そもそも180度回らない。目視で120度くらいか。2500で異音がしたのでスペック値に

以上