検索
AND検索
OR検索
トップ
|
リロード
|
新規
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
ETロボコン シミュレータ 制作日記 (by T.Misawa)
トップ
シミュレータダウンロード
日記
日記
日記/20070406
日記/20070409
日記/20070412
日記/20070413
日記/20070414
日記/20070417
日記/20070420
日記/20070421
日記/20070422
日記/20070423
日記/20070425
日記/20070426
日記/20070427
日記/20070428
日記/20070429
日記/20070502
日記/20070504
日記/20070505
日記/20070506
日記/20070507
日記/20070510
日記/20070517
日記/20070519
日記/20070520
日記/20070521
日記/20070523
日記/20070526
日記/20070527
日記/20070529
日記/20070601
日記/20070610
日記/20070611
日記/20070616
日記/20070617
日記/20070618
日記/20070624
日記/20070628
日記/20070629
日記/20070630
日記/20070701
日記/20070702
日記/20070506 をテンプレートにして作成
開始行:
|[[''前の日記''>日記/20070505]]|[[''次の日記''>日記/20070507]]|
~
*20070506 シミュレータを使ったコーディング [#a31ecf62]
**VC++でシミュレータプログラムを作る [#q14f39a5]
パスファインダーに搭載するプログラムを意識して、プログラミングをしていきます。&size(20){''しばらくC++です。''};~
~
C++といっても.NET2.0のC++はちょっと違うので「H8用のコテコテのC++とVisual C++で同じコード使いたい」ときには色々と注しないといけません。~
~
まぁ、こういうところが面白いんだけど~
.NET Framework 2.0 のC++は[[''C++/CLI''>http://ja.wikipedia.org/wiki/C++/CLI]]に準拠していて、今までのC++とは文法の記法が違うところもあり注意が必要です。.NET1.1から.NET2.0(C++/CLI)でポインタが''*''から''^''になったのにはちょっと驚き。違和感が。~
ほかにも、6.0時代はnmakeでmakefileベースでビルドできたのに、VC2005みてたらどうやらMSBuildというツールに変わったみたいです。Apache ANTっぽい感じ。~
ということで、この辺を語るのは別のブログに譲るとして、シミュレータ日記に徹することにします。~
~
~
**Managed C++とOldSyntax [#jb29abd2]
.NET時代のc++はUnmanagedとManagedの2つがありますが、いままでc#でつくってきたシミュレータライブラリ(SimEtrLib.dllという名前にしました。)がManagedなライブラリなので、c++もManaged c++を使います。また、c++/cliで導入された構文でなくて、c++の構文を使わないとパスファインダーのコードと連携ができなさそうなので、構文はc++を使います。~
~
.NET Framework 2.0のManagedなコンパイラでは、デフォルトでc++/CLIとなっているので、オプションで古い構文(OldSyntax)を指定してやります。~
~
**ライブラリを用いたコーディング [#k27bb3da]
コード例は、イメージをつかむためにのせておきます。ヘッダ読めばわかるところなどは省略します。~
''走行アルゴリズム部分(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();
}
}
**いろいろなシミュレーション条件 [#we9c39bd]
***情報を表示しながら [#xd7a040c]
#ref(info.gif,nolink);
***2台同時走行 [#s06fe7d2]
方向を変えて2台で。
#ref(2pf.gif,nolink);
***4台同時走行 [#hb8fcb66]
カラフルに4台で。~
にぎやかで〜す。
#ref(4pf.gif,nolink);
~
&size(20){''これでシミュレータV1.0は完成です''};~
あとは、本番コースの画像で、ちゃんとしたシミュレーションロジックでシミュレーションをまわしてみます。GWも今日までなので、次からは平日夜にのんびり更新します。~
~
|[[''前の日記''>日記/20070505]]|[[''次の日記''>日記/20070507]]|
~
終了行:
|[[''前の日記''>日記/20070505]]|[[''次の日記''>日記/20070507]]|
~
*20070506 シミュレータを使ったコーディング [#a31ecf62]
**VC++でシミュレータプログラムを作る [#q14f39a5]
パスファインダーに搭載するプログラムを意識して、プログラミングをしていきます。&size(20){''しばらくC++です。''};~
~
C++といっても.NET2.0のC++はちょっと違うので「H8用のコテコテのC++とVisual C++で同じコード使いたい」ときには色々と注しないといけません。~
~
まぁ、こういうところが面白いんだけど~
.NET Framework 2.0 のC++は[[''C++/CLI''>http://ja.wikipedia.org/wiki/C++/CLI]]に準拠していて、今までのC++とは文法の記法が違うところもあり注意が必要です。.NET1.1から.NET2.0(C++/CLI)でポインタが''*''から''^''になったのにはちょっと驚き。違和感が。~
ほかにも、6.0時代はnmakeでmakefileベースでビルドできたのに、VC2005みてたらどうやらMSBuildというツールに変わったみたいです。Apache ANTっぽい感じ。~
ということで、この辺を語るのは別のブログに譲るとして、シミュレータ日記に徹することにします。~
~
~
**Managed C++とOldSyntax [#jb29abd2]
.NET時代のc++はUnmanagedとManagedの2つがありますが、いままでc#でつくってきたシミュレータライブラリ(SimEtrLib.dllという名前にしました。)がManagedなライブラリなので、c++もManaged c++を使います。また、c++/cliで導入された構文でなくて、c++の構文を使わないとパスファインダーのコードと連携ができなさそうなので、構文はc++を使います。~
~
.NET Framework 2.0のManagedなコンパイラでは、デフォルトでc++/CLIとなっているので、オプションで古い構文(OldSyntax)を指定してやります。~
~
**ライブラリを用いたコーディング [#k27bb3da]
コード例は、イメージをつかむためにのせておきます。ヘッダ読めばわかるところなどは省略します。~
''走行アルゴリズム部分(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();
}
}
**いろいろなシミュレーション条件 [#we9c39bd]
***情報を表示しながら [#xd7a040c]
#ref(info.gif,nolink);
***2台同時走行 [#s06fe7d2]
方向を変えて2台で。
#ref(2pf.gif,nolink);
***4台同時走行 [#hb8fcb66]
カラフルに4台で。~
にぎやかで〜す。
#ref(4pf.gif,nolink);
~
&size(20){''これでシミュレータV1.0は完成です''};~
あとは、本番コースの画像で、ちゃんとしたシミュレーションロジックでシミュレーションをまわしてみます。GWも今日までなので、次からは平日夜にのんびり更新します。~
~
|[[''前の日記''>日記/20070505]]|[[''次の日記''>日記/20070507]]|
~
ページ名: