This repository has been archived by the owner on Jun 11, 2023. It is now read-only.
forked from ryo-0723/Siv3D_WheelSimulator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
402 additions
and
37 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,81 @@ | ||
#include <Siv3D.hpp> // OpenSiv3D v0.6.9 | ||
#include"Object.h" | ||
|
||
#include"Steer.h" | ||
#include"PS5Controller.h" | ||
void Main() | ||
{ | ||
Window::SetStyle(WindowStyle::Sizable); | ||
Window::SetTitle(U"Wheel_Simulation"); | ||
Color color = Palette::White; | ||
Object robot; | ||
|
||
Steer steer; | ||
PS5Controller ps; | ||
Vec2 LeftAxis{ 0,0 }; | ||
double InputTurn = 0; | ||
|
||
double robotSpeed = 100; | ||
size_t controllerIndex = 0; | ||
bool stopped = true; | ||
for (const auto& info : System::EnumerateGamepads()) | ||
{ | ||
Print << U"[{}] {} ({:#x} {:#x})"_fmt(info.playerIndex, info.name, info.vendorID, info.productID); | ||
} | ||
size_t robotIndex = 0; | ||
while (System::Update()) | ||
{ | ||
SimpleGUI::RadioButtons(controllerIndex, { U"keybord",U"PS4 orPS5" },{10,100}); | ||
ClearPrint(); | ||
if (const auto gamepad = Gamepad(0)) { | ||
/*ps5だったらGamepad(0)*/ | ||
/*ps4だったらGamepad(1)*/ | ||
//const auto& info = gamepad.getInfo(); | ||
if (controllerIndex) { | ||
LeftAxis.x = (int)(gamepad.axes.at(0) * 255); | ||
LeftAxis.y = -(int)(gamepad.axes.at(1) * 255); | ||
InputTurn = (int)(gamepad.axes.at(2) * 255); | ||
/*ps5だったらat(2)*/ | ||
/*ps4だったらat(3)*/ | ||
} | ||
|
||
// "Licenses" ボタンが押されたら | ||
if (SimpleGUI::Button(U"Licenses", Vec2{ 10, 250 })) | ||
{ | ||
// ライセンス情報を表示 | ||
LicenseManager::ShowInBrowser(); | ||
} | ||
if(controllerIndex == 0){ | ||
LeftAxis.y = KeyW.pressed() * 40 - KeyS.pressed() * 40; | ||
LeftAxis.x = KeyD.pressed() * 40 - KeyA.pressed() * 40; | ||
InputTurn = KeyRight.pressed() * 40 - KeyLeft.pressed() * 40; | ||
switch (controllerIndex) { | ||
case 0: | ||
LeftAxis.y = KeyW.pressed() * 255 - KeyS.pressed() * 255; | ||
LeftAxis.x = KeyD.pressed() * 255 - KeyA.pressed() * 255; | ||
InputTurn = KeyRight.pressed() * 255 - KeyLeft.pressed() * 255; | ||
break; | ||
case 1: | ||
ps.Update(0); | ||
LeftAxis = ps.GetLeftAxis(); | ||
InputTurn = ps.GetRightAxis().x; | ||
break; | ||
} | ||
|
||
Print << Scene::DeltaTime() * 1000; | ||
|
||
//Print << Scene::DeltaTime() * 1000; | ||
|
||
robot.SetPower({ 0,0,0,0 }, {0,0,0,0}).draw(color); | ||
|
||
//std::array<double, 4>Wheel; | ||
//Wheel[0] = LeftAxis.x + LeftAxis.y + InputTurn; | ||
//Wheel[1] = LeftAxis.x - LeftAxis.y + InputTurn; | ||
//Wheel[2] = -LeftAxis.x - LeftAxis.y + InputTurn; | ||
//Wheel[3] = -LeftAxis.x + LeftAxis.y + InputTurn; | ||
///*オムニ用角度*/ | ||
// robot.SetPower(Wheel, { 45,135,225,315 }).draw(color); | ||
if (robotIndex) { | ||
steer.Update(LeftAxis, InputTurn, 0); | ||
steer.Allow(); | ||
std::array<double, 4>angle = { steer.GetPower(place::LF).x | ||
,steer.GetPower(place::RF).x | ||
,steer.GetPower(place::RB).x | ||
,steer.GetPower(place::LB).x }; | ||
std::array<double, 4>length = { steer.GetPower(place::LF).y | ||
,steer.GetPower(place::RF).y | ||
,steer.GetPower(place::RB).y | ||
,steer.GetPower(place::LB).y | ||
}; | ||
robot.SetPower(length, angle).draw(color); | ||
} | ||
else { | ||
std::array<double, 4>Wheel; | ||
Wheel[0] = LeftAxis.x + LeftAxis.y + InputTurn; | ||
Wheel[1] = LeftAxis.x - LeftAxis.y + InputTurn; | ||
Wheel[2] = -LeftAxis.x - LeftAxis.y + InputTurn; | ||
Wheel[3] = -LeftAxis.x + LeftAxis.y + InputTurn; | ||
const auto max = std::max(abs(*std::max_element(Wheel.begin(), Wheel.end())) | ||
, abs(*std::min_element(Wheel.begin(), Wheel.end()))); | ||
double MAX = robotSpeed; | ||
if (max > MAX) { | ||
double maximum = MAX / max; | ||
for (int i = 0; i < 4; i++) { | ||
Wheel[i] *= maximum; | ||
} | ||
} | ||
/*オムニ用角度*/ | ||
robot.SetPower(Wheel, { 45,135,225,315 }).draw(color); | ||
} | ||
SimpleGUI::RadioButtons(controllerIndex, { U"keybord",U"PS5" }, { 10,90 }); | ||
SimpleGUI::RadioButtons(robotIndex, { U"Omni",U"Steering" }, { 10,170 }); | ||
SimpleGUI::Slider(U"Speed{:.2f}"_fmt(robotSpeed), robotSpeed, 0, 255, Vec2{10, 50},130.0,150.0); | ||
steer.SetPower(robotSpeed); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#include "stdafx.h" | ||
#include "PS5Controller.h" | ||
|
||
void PS5Controller::Update(size_t playerIndex) { | ||
if (playerIndex == 0) { | ||
if (const auto gamepad = Gamepad(playerIndex)) { | ||
/*ps5だったらGamepad(0)*/ | ||
/*ps4だったらGamepad(1)*/ | ||
//const auto& info = gamepad.getInfo(); | ||
leftAxis.x = gamepad.axes.at(0) * 255.00; | ||
leftAxis.y = -gamepad.axes.at(1) * 255.00; | ||
rightAxis.x = gamepad.axes.at(2) * 255.00; | ||
/*ps5だったらat(2)*/ | ||
/*ps4だったらat(3)*/ | ||
} | ||
} | ||
else if (playerIndex == 1) { | ||
if (const auto gamepad = Gamepad(playerIndex)) { | ||
leftAxis.x = gamepad.axes.at(0) * 255.00; | ||
leftAxis.y = -gamepad.axes.at(1) * 255.00; | ||
rightAxis.x = gamepad.axes.at(3) * 255.00; | ||
} | ||
} | ||
|
||
if (leftAxis.x > cutValue) { | ||
leftAxis.x = map(leftAxis.x, cutValue, 255, 0, 255); | ||
}else if (leftAxis.x < -cutValue) { | ||
leftAxis.x = map(leftAxis.x, -cutValue, -255, 0, -255); | ||
} | ||
else { | ||
leftAxis.x = 0; | ||
} | ||
|
||
if (leftAxis.y > cutValue) { | ||
leftAxis.y = map(leftAxis.y, cutValue, 255, 0, 255); | ||
} | ||
else if (leftAxis.y < -cutValue) { | ||
leftAxis.y = map(leftAxis.y, -cutValue, -255, 0, -255); | ||
} | ||
else { | ||
leftAxis.y = 0; | ||
} | ||
|
||
|
||
if (rightAxis.x > cutValue) { | ||
rightAxis.x = map(rightAxis.x, cutValue, 255, 0, 255); | ||
} | ||
else if (rightAxis.x < -cutValue) { | ||
rightAxis.x = map(rightAxis.x, -cutValue, -255, 0, -255); | ||
} | ||
else { | ||
rightAxis.x = 0; | ||
} | ||
|
||
if (rightAxis.y > cutValue) { | ||
rightAxis.y = map(rightAxis.y, cutValue, 255, 0, 255); | ||
} | ||
else if (rightAxis.y < -cutValue) { | ||
rightAxis.y = map(rightAxis.y, -cutValue, -255, 0, -255); | ||
} | ||
else { | ||
rightAxis.y = 0; | ||
} | ||
} | ||
|
||
|
||
|
||
Vec2 PS5Controller::GetLeftAxis() { | ||
return leftAxis; | ||
} | ||
|
||
Vec2 PS5Controller::GetRightAxis() { | ||
return rightAxis; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#pragma once | ||
class PS5Controller | ||
{ | ||
private: | ||
int cutValue=20; | ||
Vec2 leftAxis = {}; | ||
Vec2 rightAxis = {}; | ||
|
||
public: | ||
PS5Controller() {} | ||
|
||
|
||
void Update(size_t playerIndex); | ||
|
||
Vec2 GetLeftAxis(); | ||
|
||
Vec2 GetRightAxis(); | ||
|
||
double map(double x, double in_min, double in_max, double out_min, double out_max) { | ||
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; | ||
} | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#include "stdafx.h" | ||
#include "Steer.h" | ||
|
||
void Steer::Update(Vec2 Axis, double z, double gyro) { | ||
/*とりあえずジャイロのことは加味せずに計算していく*/ | ||
/*z(旋回のベクトル)を(0,z)とすることでy軸と同じ方向になるベクトルが作れる*/ | ||
//LFから時計回りにタイヤの計算 | ||
/* | ||
[0]LF ↑ [1]RF | ||
| ̄ ̄ ̄ ̄ ̄ ̄| | ||
| | | ||
| | | ||
| | | ||
| | | ||
|____________| | ||
[3]LB [2]RB | ||
*/ | ||
std::array<Vec2, 4> data{};//計算結果一時保存場所 | ||
|
||
for (int i = 0; i < 4; i++) {//タイヤの出力計算 | ||
data[i] = Axis + Vec2{ 0,z }.rotate((i * -0.5 - 0.25) * Math::Pi);/*Vec2*/ | ||
//Tire[i].Update(ToDegrees(data[i].yx().rotated(-0.5 * Math::Pi).getAngle()), | ||
//(double)data[i].length()); | ||
|
||
Tire[i].UpdateRev(ToDegrees(data[i].yx().rotated(-0.5 * Math::Pi).getAngle()), | ||
(double)data[i].length()); | ||
} | ||
|
||
/*PWMの最大値をとる*/ | ||
const auto max = (*std::max_element(Tire.begin(), Tire.end(), [](const Wheel& lhs, const Wheel& rhs) { | ||
return abs(lhs.length) < abs(rhs.length);})).length; | ||
if (max > MAX) { | ||
double maximum = MAX / max; | ||
for (int i = 0; i < 4; i++) { | ||
Tire[i].length *= maximum; | ||
} | ||
} | ||
} | ||
|
||
void Steer::Show() { | ||
//for(int i=0;i<4;++i) | ||
//Print << U"angle :"<< Tire[i].GetAngle(); | ||
//for(int i=0;i<4;++i) | ||
//Print << U"length :"<< Tire[i].GetLength(); | ||
} | ||
|
||
void Steer::Stop() { | ||
for (int i = 0; i < 4;++i) { | ||
Tire[i].Stop(); | ||
} | ||
} | ||
void Steer::Allow() { | ||
for (int i = 0; i < 4; ++i) { | ||
Tire[i].Allow(); | ||
} | ||
} | ||
|
||
Vec2 Steer::GetPower(place unit) { | ||
return { Tire[static_cast<int>(unit)].GetAngle(),Tire[static_cast<int>(unit)].GetLength() }; | ||
} | ||
|
||
void Steer::Culibration() { | ||
|
||
} | ||
|
||
void Steer::SetPower(double power) { | ||
MAX = power; | ||
} |
Oops, something went wrong.