#include <iostream>
#include "His/HISpidar4.h"
#ifdef _WIN32
#include <windows.h>
#include <conio.h>
#include <Device/DRUsb20Simple.h>
#include <Device/DRUsb20Sh4.h>
#endif
#ifdef __sh__
#include <cyg/kernel/kapi.h>
#include <Device/DRIHCSh4.h>
int kbhit(){return false;}
int getch(){ return 0;}
void Sleep(int msec){
cyg_thread_delay(msec*10);
}
#define cout DSTR
#endif
using namespace Spr;
using namespace std;
int main(){
DVDeviceManager devMan;
#ifdef _WIN32
devMan.RPool().Register(new DRUsb20Simple(10));
devMan.RPool().Register(new DRUsb20Sh4(0));
devMan.RPool().Register(new DRUsb20Sh4(1));
devMan.RPool().Register(new DRUsb20Sh4(2));
devMan.RPool().Register(new DRUsb20Sh4(3));
bool bMinForce = true;
const float K = 2000.0f;
#endif
#ifdef __sh__
devMan.RPool().Register(new DRIHCSh4);
bool bMinForce = false;
const float K = 20000.0f;
#endif
devMan.Init();
cout << devMan;
HISpidar4 spidar;
HISpidar4 spidar2;
#define PX 0.265f // x方向の辺の長さ/2
#define PY 0.265f // y方向の辺の長さ/2
#define PZ 0.265f // z方向の辺の長さ/2
Vec3f motorPos[][4] = {
{Vec3f(-PX, PY, PZ), Vec3f( PX, PY,-PZ), Vec3f(-PX,-PY,-PZ), Vec3f( PX,-PY, PZ)},
{Vec3f( PX, PY, PZ), Vec3f(-PX, PY,-PZ), Vec3f( PX,-PY,-PZ), Vec3f(-PX,-PY, PZ)}
};
cout.flush();
spidar.Init(devMan, motorPos[0], 0.3776f, 2.924062107079e-5f, 0.5f, 20.0f);
spidar2.Init(devMan, motorPos[1], 0.3776f, 2.924062107079e-5f, 0.5f, 20.0f);
cout.flush();
printf("Waiting...");
Sleep(1000);
printf("Calibration\n");
spidar.Calib();
spidar2.Calib();
while(1){
if (kbhit()){
int ch = getch();
switch(ch){
case ' ':
bMinForce = !bMinForce;
break;
case 0x1b:
goto end;
break;
}
}
spidar.Update(0.001f);
Vec3f force;
if (spidar.GetPos().Y() < 0){
force.Y() = - K * spidar.GetPos().Y();
}
if (force.norm() > 10){
force = force.unit() * 10;
}
if (bMinForce) spidar.SetMinForce();
else spidar.SetForce(force);
Vec3f force2;
spidar2.Update(0.001f);
if (spidar2.GetPos().Y() < 0){
force2.Y() = - K * spidar2.GetPos().Y();
}
if (bMinForce) spidar2.SetMinForce();
else spidar2.SetForce(force2);
#if 0
cout << "P:" << spidar.GetPos() << "\tF:" << spidar.GetForce() << std::endl;
#endif
#if 0
for(int i=0; i<4; i++){
cout << spidar.Motor()[i].GetLength() << " ";
}
for(int i=0; i<4; i++){
cout << spidar2.Motor()[i].GetLength() << " ";
}
cout << std::endl;
cout.flush();
#endif
#if 1
for(int i=0; i<4; i++){
if (spidar.Motor()[i].counter){
cout << spidar.Motor()[i].counter->Count() << " ";
}
}
for(int i=0; i<4; i++){
if (spidar2.Motor()[i].counter){
cout << spidar2.Motor()[i].counter->Count() << " ";
}
}
cout << std::endl;
cout.flush();
#endif
}
end:;
return 0;
}