パスファインダーに搭載するプログラムを意識して、プログラミングをしていきます。しばらくC++です。
C++といっても.NET2.0のC++はちょっと違うので「H8用のコテコテのC++とVisual C++で同じコード使いたい」ときには色々と注しないといけません。
まぁ、こういうところが面白いんだけど
.NET Framework 2.0 のC++はC++/CLIに準拠していて、今までのC++とは文法の記法が違うところもあり注意が必要です。.NET1.1から.NET2.0(C++/CLI)でポインタが*から^になったのにはちょっと驚き。違和感が。
ほかにも、6.0時代はnmakeでmakefileベースでビルドできたのに、VC2005みてたらどうやらMSBuildというツールに変わったみたいです。Apache ANTっぽい感じ。
ということで、この辺を語るのは別のブログに譲るとして、シミュレータ日記に徹することにします。
.NET時代のc++はUnmanagedとManagedの2つがありますが、いままでc#でつくってきたシミュレータライブラリ(SimEtrLib.dllという名前にしました。)がManagedなライブラリなので、c++もManaged c++を使います。また、c++/cliで導入された構文でなくて、c++の構文を使わないとパスファインダーのコードと連携ができなさそうなので、構文はc++を使います。
.NET Framework 2.0のManagedなコンパイラでは、デフォルトでc++/CLIとなっているので、オプションで古い構文(OldSyntax)を指定してやります。
コード例は、イメージをつかむためにのせておきます。ヘッダ読めばわかるところなどは省略します。
走行アルゴリズム部分(RunningLogic)のコード
これはH8用のBrickOS APIのシグネチャを真似て作ったもので、これを使ってH8側とWindows側でアルゴリズム部分のソースコードを同一のものにすることができます。
-------RunningLogic.h---------------------------------------- #pragma once using namespace Misawat::Etrobo::Simulator; public __gc class RunningLogic { public: RunningLogic(PathFinder* pathFinder); ~RunningLogic(void); void RunningLogic::Run(void); private: RunningLogic(void); PathFinder* pathFinder; LightSensor* lightSensor; TouchSensor* touchSensor; Motor* tractionMotor; Motor* steeringMotor; }; -------RunningLogic.cpp---------------------------------------- #include "StdAfx.h" #include "RunningLogic.h" RunningLogic::RunningLogic(PathFinder* pathFinder) { lightSensor = pathFinder->GetLightSensor(); touchSensor = pathFinder->GetTouchSensor(); tractionMotor = pathFinder->GetTractionMotor(); steeringMotor = pathFinder->GetSteeringMotor(); } //↓ここからが走行アルゴリズムそのもの void RunningLogic::Run(void) { tractionMotor->Foward(255); if (lightSensor->Get() < 50) { steeringMotor->Foward(255); } else { steeringMotor->Reverse(255); } }
※クラスをnewするときの_gcキーワードはガベージコレクションの対象とするオブジェクトですよということを宣言しています。ですので、オブジェクトを破棄するときに明示的にdeleteする必要は無くなります。
シミュレータ部分のコード
PathFinderオブジェクトをCreateして、SimulatorPlatformにAddすると、コース上にパスファインダーが出現します。何台でもCreateしてAddできます。
RunningLogicクラスでは、LightSensorの値(コースの白黒値)を読んで、TractionMotorやSteeringMotorを制御します。
-------Simulation.h---------------------------------------- #pragma once using namespace System; using namespace System::Text; using namespace Misawat::Etrobo::Simulator; public __gc class Simulation { public: Simulation(System::Drawing::Graphics* myGraphics); virtual ~Simulation(void); void Simulation::Start(); void Simulation::Stop(void); private: Simulation(void); bool running; SimulatorPlatform* _simulatorPlatform; System::Drawing::Graphics* _myGraphics; }; -------Simulation.cpp---------------------------------------- #include "StdAfx.h" #include "Simulation.h" #include "RunningLogic.h" Simulation::Simulation(System::Drawing::Graphics* myGraphics) { _myGraphics=myGraphics; } void Simulation::Start() { //SimulatorPlatform(コースの画像データ,グラフィックコンテクスト,描画時の倍率) _simulatorPlatform = __gc new SimulatorPlatform(".\\test_course01.png", _myGraphics,0.6); PathFinder* pathFinder = _simulatorPlatform->CreatePathFinder(2400, 500, 90); pathFinder->SetBodyColor(System::Drawing::Color::FromArgb(255,255,0,0)); RunningLogic* pathFinderLogic=__gc new RunningLogic(pathFinder); running=true; while(running) { System::Windows::Forms::Application::DoEvents(); _simulatorPlatform->Update(0.01); pathFinderLogic->Run(); } }
方向を変えて2台で。
カラフルに4台で。
にぎやかで〜す。
これでシミュレータV1.0は完成です
あとは、本番コースの画像で、ちゃんとしたシミュレーションロジックでシミュレーションをまわしてみます。GWも今日までなので、次からは平日夜にのんびり更新します。