Help us understand the problem. What is going on with this article?

【半田レス版】obnized M5StickC 回転Servoで2WD Car

はじめに

前に書いたobnized M5StickC 回転Servoで2WD Carの進化系です。半田付けなしで作れるような構成に改良しました。

image.png

■動画

■接続図

下記の部品を使います
・5cm程度のGROVEケーブル。
・両端ロングのピンヘッダ、3ピン×2個。

半田付け不要で、だだ挿すだけです。

GROVE端子とピンヘッダはピッチが異なるのですが、慎重かつ強引に差し込めば何とかなります。なんならGROVEケーブルなしで、Servo端子を直接M5StickCのGROVE端子に刺せない事もないのですが、ヤバイのでお勧めはしません。でもやればできます。

image.png

■車体(参考)

LEGOブロック等で適当に組み上げます。
image.png

■サンプルコード

<html>
<head>
  <meta charset="utf-8">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
  <script src="https://obniz.io/js/jquery-3.2.1.min.js"></script>
  <script src="https://unpkg.com/obniz@3.2.0/obniz.js"  crossorigin="anonymous"></script>
  <script src="https://unpkg.com/m5stickcjs/m5stickc.js"></script>
</head>
<body>
  <div id="obniz-debug"></div>
  <font size="7">
    M5StickC 回転Servo 2WD Car<br><br>
  </font>
  <button id="FORWARD_LEFT"  class="btn btn-warning" style="width:30%;height:80px;font-size:60px;"></button>  
  <button id="FORWARD"       class="btn btn-warning" style="width:30%;height:80px;font-size:60px;"></button>
  <button id="FORWARD_RIGHT" class="btn btn-warning" style="width:30%;height:80px;font-size:60px;"></button><br><br>

  <button id="TURN_LEFT"     class="btn btn-warning" style="width:30%;height:80px;font-size:60px;"></button> 
  <button id="STOP"          class="btn btn-warning" style="width:30%;height:80px;font-size:60px;">STOP</button>
  <button id="TURN_RIGHT"    class="btn btn-warning" style="width:30%;height:80px;font-size:60px;"></button><br><br>

  <button id="BACK_LEFT"     class="btn btn-warning" style="width:30%;height:80px;font-size:60px;"></button>
  <button id="BACK"          class="btn btn-warning" style="width:30%;height:80px;font-size:60px;"></button>
  <button id="BACK_RIGHT"    class="btn btn-warning" style="width:30%;height:80px;font-size:60px;"></button><br><br>

<script>
const Lhome =  92; //右停止
const Rhome =  92; //左停止
const LF    = +85; //左前進
const LB    = -85; //左後進
const RF    = -85; //右前進
const RB    = +85; //右後進

var obniz = new M5StickC('XXXX-XXXX');
obniz.onconnect = async function () {
  var Servo_L = obniz.wired("ServoMotor" , {signal:26} );
  var Servo_R = obniz.wired("ServoMotor" , {signal:32} ); 
  obniz.led.on();
  $("#FORWARD_LEFT").on('touchstart mousedown'    ,async ()=> { await FORWARD_LEFT();  })
  $("#FORWARD_LEFT").on('touchend mouseup'        ,async ()=> { await STOP();          })
  $("#FORWARD").on('touchstart mousedown'         ,async ()=> { await FORWARD();       })
  $("#FORWARD").on('touchend mouseup'             ,async ()=> { await STOP();          })  
  $("#FORWARD_RIGHT").on('touchstart mousedown'   ,async ()=> { await FORWARD_RIGHT(); })
  $("#FORWARD_RIGHT").on('touchend mouseup'       ,async ()=> { await STOP();          })

  $("#TURN_LEFT").on('touchstart mousedown'       ,async ()=> { await TURN_LEFT();     })
  $("#TURN_LEFT").on('touchend mouseup'           ,async ()=> { await STOP();          })
  $("#STOP").on('touchend mouseup'                ,async ()=> { await STOP();          })  
  $("#TURN_RIGHT").on('touchstart mousedown'      ,async ()=> { await TURN_RIGHT();    })
  $("#TURN_RIGHT").on('touchend mouseup'          ,async ()=> { await STOP();          })  

  $("#BACK_LEFT").on('touchstart mousedown'       ,async ()=> { await BACK_LEFT();     })
  $("#BACK_LEFT").on('touchend mouseup'           ,async ()=> { await STOP();          })
  $("#BACK").on('touchstart mousedown'            ,async ()=> { await BACK();          })
  $("#BACK").on('touchend mouseup'                ,async ()=> { await STOP();          })
  $("#BACK_RIGHT").on('touchstart mousedown'      ,async ()=> { await BACK_RIGHT();    })
  $("#BACK_RIGHT").on('touchend mouseup'          ,async ()=> { await STOP();          }) 

  async function FORWARD(){
     Servo_L.angle(Lhome+LF);
     Servo_R.angle(Rhome+RF);
  }
  async function BACK(){
     Servo_L.angle(Lhome+LB);
     Servo_R.angle(Rhome+RB);
  }  
  async function FORWARD_LEFT(){
     Servo_L.angle(Lhome);
     Servo_R.angle(Rhome+RF);
  }
  async function TURN_LEFT(){
     Servo_L.angle(Lhome+LB);
     Servo_R.angle(Rhome+RF);
  }  
  async function FORWARD_RIGHT(){
     Servo_L.angle(Lhome+LF);
     Servo_R.angle(Rhome);
  }
  async function TURN_RIGHT(){
     Servo_L.angle(Lhome+LF);
     Servo_R.angle(Rhome+RB);
  }  
  async function BACK_LEFT(){
     Servo_L.angle(Lhome);
     Servo_R.angle(Rhome+RB);
  }
  async function BACK_RIGHT(){
     Servo_L.angle(Lhome+LB);
     Servo_R.angle(Rhome);
  }  
  async function STOP(){
     Servo_L.angle(Lhome);
     Servo_R.angle(Rhome);
  }
}
</script>
</body>
</html>

■最後に

とりあえず回転Servoを簡単に接続する方法はこれで完成です。次は何とかDC Motorも、より簡単に接続する方法を考えていきます。

iotlt
IoT縛りの勉強会です。 毎月イベントを実施しているので是非遊びに来てください! 登壇者を中心にQiitaでも情報発信していきます。 https://iotlt.connpass.com
https://iotlt.connpass.com/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした