Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8691

ラジコンをリッチメニューから遠隔操作する。自動ブレーキ付き

$
0
0

やったこと

前回作成した【obniz×LINE Messaging API】音と光を使い侵入防止システムを製作するの時に使用したタッパを乗せて走らせてみました。

できたもの

LINEのリッチメニューから操作でき、10㎝以内に障害物があると自動で停止します。

構成

全体像

今回製作したシステムの全体像です。
Image from Gyazo

配線図

obnizの配線図です。
Untitled Sketch2_ブレッドボード.png

使用部品

環境

  • node.js v14.5.0
  • @line/bot-sdk 7.0.0
  • express 4.17.1
  • obniz 3.7.0

コード

index.js
'use strict';// obniz呼び出しconstObniz=require('obniz');constobniz=newObniz('××××-××××');// Obniz_IDに自分のIDを入れますconstline=require("@line/bot-sdk");constexpress=require("express");constPORT=process.env.PORT||3030;constconfig={channelSecret:'{チャネルシークレット}',channelAccessToken:'{アクセストークン}'};constuserId='{ユーザーid}';constapp=express();obniz.onconnect=asyncfunction(){obniz.display.clear();obniz.display.print('obniz meets LINE Bot!');// モーターを呼び出すconstmotorRight=obniz.wired("DCMotor",{forward:1,back:0});constmotorLeft=obniz.wired("DCMotor",{forward:8,back:11});// 距離センサーを呼び出すconstGP2Y0=obniz.wired("GP2Y0A21YK0F",{vcc:4,gnd:5,signal:6});setInterval(asyncfunction(){app.post('/webhook',line.middleware(config),(req,res)=>{console.log(req.body.events);Promise.all(req.body.events.map(handleEvent)).then((result)=>res.json(result));});constclient=newline.Client(config);functionforward_command(){motorLeft.forward();motorRight.forward();}functionstop_command(){motorLeft.stop();motorRight.stop();}functionreverse_command(){motorLeft.reverse();motorRight.reverse();}functionturning(){motorLeft.forward();motorRight.reverse();}constdistance=awaitGP2Y0.getWait();console.log(distance+' mm');functionhandleEvent(event){if(event.type!=='message'||event.message.type!=='text'){returnPromise.resolve(null);}switch(event.message.text){case'前進':client.replyMessage(event.replyToken,{type:'text',text:'前進します'});forward_command();break;case'停止':client.replyMessage(event.replyToken,{type:'text',text:'停止しました'});stop_command();break;case'後退':client.replyMessage(event.replyToken,{type:'text',text:'後退します'});reverse_command();break;case'ダンス':client.replyMessage(event.replyToken,{type:'text',text:'踊ります'});turning();break;}}if(distance<100){// setTimeout(function () {//   client.pushMessage(userId, {//   type: 'text',//   text: '障害物を検知しました',//   });// },1000);stop_command();}},1000);}app.listen(PORT);console.log(`Server running at ${PORT}`);

参考にした記事

LINEmessagingAPIの基礎部分で参考にしました。
1時間でLINE BOTを作るハンズオン (資料+レポート) in Node学園祭2017 #nodefest

終わりに

公式サイトのラジコン類はスマートに動いていて次回の目標となります、できるかどうかはわかりませんが、次に作るときはカメラで画像認識を使用して走行させてみたいと思います。今回購入したキットは、格安品でまっすぐ進むよう調整するのが大変でした。


Viewing all articles
Browse latest Browse all 8691

Trending Articles