esp32とps3とスパイカメラを使ってラジコン戦車をつくる。
材料
タミヤの模型
esp32(スイッチサイエンス製)
モータードライバー
ps3コントローラ(中古)
サーボモーター(サンワ sx101z)
マブチモーター
スパイカメラ(バッテリー内蔵)
その他 配線、カプラー、電池
esp32用プログラム
//*****************************************************
#include <Ps3Controller.h>
int RY,LY,BR1,BL1,BBL1;
int MIN=10;
int MAX=128;
int MAX1=200;//400
int C_MAX=410; //=1/((1/50*1000)/2^12)
int C_NE=307; //=1.5/((1/50*1000)/2^12)
int C_MIN=200; //=2/((1/50*1000)/2^12)
uint8_t RY1=14;
uint8_t RY2=27;
uint8_t LY1=26;
uint8_t LY2=25;
uint8_t C_PIN=12;
long R_SPEED,L_SPEED;
int CAMERA,CAMERA1;
uint32_t freq=12800;
uint8_t res=8;
void setup()
{
pinMode(LY1,OUTPUT);
pinMode(LY2,OUTPUT);
pinMode(RY1,OUTPUT);
pinMode(RY2,OUTPUT);
pinMode(C_PIN,OUTPUT);
Serial.begin(115200);
Ps3.begin("7C:9E:BD:E9:A2:72"); //1.でメモしたmac adresに修正
Serial.println("Ready");
ledcAttach(LY1, freq, res);//pwm信号 ledcSetup(ピン番号, 周波数, 分解能)
ledcAttach(LY2, freq, res);//pwm信号
ledcAttach(RY1, freq, res);//pwm信号
ledcAttach(RY2, freq, res);//pwm信号
ledcAttach(C_PIN, 50, 12);//pwm信号 =1/((1/50*1000)/2^12)
ledcWrite(LY2,0);
ledcWrite(LY1,0);
ledcWrite(RY1,0);
ledcWrite(RY2,0);
ledcWrite(C_PIN,C_NE);
}
void loop(){
if (Ps3.isConnected()){
LY= Ps3.data.analog.stick.ly;
RY= Ps3.data.analog.stick.ry;
L_SPEED=map(LY,-MAX,MAX,-MAX1,MAX1);
R_SPEED=map(RY,-MAX,MAX,-MAX1,MAX1);
BL1=Ps3.data.button.l1;
BR1=Ps3.data.button.r1;
BBL1=Ps3.data.button.l2;
CAMERA=-BL1+BR1+CAMERA;
if (BBL1==1) {CAMERA=0;}
//left speed
if (abs(L_SPEED)<=MIN){//50以下なら 速度は0
ledcWrite(LY2,0);
ledcWrite(LY1,0);
}
else if (L_SPEED>MIN){
ledcWrite(LY2,0);
ledcWrite(LY1,abs(L_SPEED));
}
else {
ledcWrite(LY1,0);
ledcWrite(LY2,abs(L_SPEED));
}
//right speed
if (abs(R_SPEED)<=MIN){
ledcWrite(RY1,0);
ledcWrite(RY2,0);
}
else if (R_SPEED>MIN){
ledcWrite(RY2,0);
ledcWrite(RY1,abs(R_SPEED));
}
else {
ledcWrite(RY1,0);
ledcWrite(RY2,abs(R_SPEED));
}
//CAMERA
CAMERA1=CAMERA/400+C_NE;
if (CAMERA1<C_MIN){CAMERA1=C_MIN;}
if (CAMERA1>C_MAX){CAMERA1=C_MAX;}
ledcWrite(C_PIN,CAMERA1);
}
}
#include <Ps3Controller.h>
int RY,LY,BR1,BL1,BBL1;
int MIN=10;
int MAX=128;
int MAX1=200;//400
int C_MAX=410; //=1/((1/50*1000)/2^12)
int C_NE=307; //=1.5/((1/50*1000)/2^12)
int C_MIN=200; //=2/((1/50*1000)/2^12)
uint8_t RY1=14;
uint8_t RY2=27;
uint8_t LY1=26;
uint8_t LY2=25;
uint8_t C_PIN=12;
long R_SPEED,L_SPEED;
int CAMERA,CAMERA1;
uint32_t freq=12800;
uint8_t res=8;
void setup()
{
pinMode(LY1,OUTPUT);
pinMode(LY2,OUTPUT);
pinMode(RY1,OUTPUT);
pinMode(RY2,OUTPUT);
pinMode(C_PIN,OUTPUT);
Serial.begin(115200);
Ps3.begin("7C:9E:BD:E9:A2:72"); //1.でメモしたmac adresに修正
Serial.println("Ready");
ledcAttach(LY1, freq, res);//pwm信号 ledcSetup(ピン番号, 周波数, 分解能)
ledcAttach(LY2, freq, res);//pwm信号
ledcAttach(RY1, freq, res);//pwm信号
ledcAttach(RY2, freq, res);//pwm信号
ledcAttach(C_PIN, 50, 12);//pwm信号 =1/((1/50*1000)/2^12)
ledcWrite(LY2,0);
ledcWrite(LY1,0);
ledcWrite(RY1,0);
ledcWrite(RY2,0);
ledcWrite(C_PIN,C_NE);
}
void loop(){
if (Ps3.isConnected()){
LY= Ps3.data.analog.stick.ly;
RY= Ps3.data.analog.stick.ry;
L_SPEED=map(LY,-MAX,MAX,-MAX1,MAX1);
R_SPEED=map(RY,-MAX,MAX,-MAX1,MAX1);
BL1=Ps3.data.button.l1;
BR1=Ps3.data.button.r1;
BBL1=Ps3.data.button.l2;
CAMERA=-BL1+BR1+CAMERA;
if (BBL1==1) {CAMERA=0;}
//left speed
if (abs(L_SPEED)<=MIN){//50以下なら 速度は0
ledcWrite(LY2,0);
ledcWrite(LY1,0);
}
else if (L_SPEED>MIN){
ledcWrite(LY2,0);
ledcWrite(LY1,abs(L_SPEED));
}
else {
ledcWrite(LY1,0);
ledcWrite(LY2,abs(L_SPEED));
}
//right speed
if (abs(R_SPEED)<=MIN){
ledcWrite(RY1,0);
ledcWrite(RY2,0);
}
else if (R_SPEED>MIN){
ledcWrite(RY2,0);
ledcWrite(RY1,abs(R_SPEED));
}
else {
ledcWrite(RY1,0);
ledcWrite(RY2,abs(R_SPEED));
}
//CAMERA
CAMERA1=CAMERA/400+C_NE;
if (CAMERA1<C_MIN){CAMERA1=C_MIN;}
if (CAMERA1>C_MAX){CAMERA1=C_MAX;}
ledcWrite(C_PIN,CAMERA1);
}
}
これで動いた。