3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

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

Posted at

#はじめに
##前に書いた**「obnized M5StickC 回転Servoで2WD Car」**の進化系です。半田付けなしで作れるような構成に改良しました。
image.png
#■動画

#■接続図 下記の部品を使います ・5cm程度のGROVEケーブル。 ・両端ロングのピンヘッダ、3ピン×2個。 ###半田付け不要で、だだ挿すだけです。 ###GROVE端子とピンヘッダはピッチが異なるのですが、慎重かつ強引に差し込めば何とかなります。なんならGROVEケーブルなしで、Servo端子を直接M5StickCのGROVE端子に刺せない事もないのですが、ヤバイのでお勧めはしません。でもやればできます。 ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/234533/9be44c55-814e-76e0-73ad-57ed8e1df8e4.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も、より簡単に接続する方法を考えていきます。

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?