LoginSignup
2
2

More than 1 year has passed since last update.

micro:bitとProcessingで通信してドローン(Tello)を操縦する

Last updated at Posted at 2019-07-29

タイトルの通り,micro:bitをコントローラとして操作し,Processingにシリアル通信で値を送信します.Pcocessingとドローン(Tello)はUDPで通信します.コードはラピットタイプで書いたので,今後修正します!

micro:bit

スクリーンショット 2021-08-26 172314.png

コードはこちらからも↓
https://makecode.microbit.org/_KD3XDP6ebejd

Processing

import hypermedia.net.*; //UDP library
import processing.serial.*; //Serial library
Serial microbit;
UDP udp;
DroneContol drone;

String ip = "192.168.10.1"; 
int port = 8889;
int mbitX, mbitY, mbitA ,mbitB, mbitAB = 0;
String recMess;

void setup(){
  String portName[] = Serial.list();
  println(portName);
  microbit = new Serial(this, portName[0], 115200);
  drone = new DroneContol();
  udp = new UDP(this, port);
  udp.listen(true);
  udp.send("command", ip, port);
}

void draw(){
  drone.update(mbitX,mbitY,mbitA,mbitB,mbitAB);

}

void serialEvent(Serial microbit) {
  String str = microbit.readStringUntil('\n');
  if (str != null) {
    str = trim(str);
    int sensors[] = int(split(str, ','));
    mbitX = sensors[0];
    mbitY = sensors[1];
    mbitA = sensors[2];
    mbitB = sensors[3];
    mbitAB = sensors[4];
    //println("x="+ sensors[0]+ ", y="+ sensors[1]+ ", A="+ sensors[2]+ ", B="+ sensors[3]+ ", AB="+ mbitAB);
  }
}

void receive(byte[] data, String ip, int port) {
  data = subset(data, 0, data.length-2);
  recMess = new String(data);
  println(recMess);
}

class DroneContol {

  int a, b, ab, maxRange, minRange;
  String lrStr, fdStr, udStr, yawStr;
  int lrSpeed, fbSpeed, udSpeed, yawSpeed;
  boolean is_flight, is_up, is_down;

  DroneContol() {
    a = b = ab = 0;
    maxRange = 1023;
    minRange = -1023;
    is_flight = is_up = is_down = false;
    lrSpeed = fbSpeed = udSpeed = yawSpeed = 0;
  }

  void update(int x, int y, int A, int B, int AB) {
    if (ab != AB) {
      if (is_flight==false) takeOFF();
      else land();
    }

    if (is_flight) {
      if (a != A) {
        if (!is_down) is_up = !is_up;
      } else if (b != B) {
        if (!is_up) is_down = !is_down;
      } else {
        if (is_up) {
          up();
        } else if (is_down) {
          down();
        } else {
          setSpeed(x, y);
        }
        convertItoS();
        String rcStr = "rc "+ " "+lrSpeed+ " "+fbSpeed+ " "+udSpeed+" "+yawSpeed;
        udp.send(rcStr, ip, port);
      }
    }
    a = A;
    b = B;
    ab = AB;
  }

  void takeOFF() {
    udp.send("takeoff", ip, port);
    is_flight = true;
    int timer = second()+2;
    println("Take OFF Count 2");
    while (timer > second()) {
    }
    println("Take OFF");
  }

  void land() {
    udp.send("land", ip, port);
    int timer = second()+2;
    println("LAND Count 2");
    while (timer > second()) {
    }
    is_flight = false;
    println("LAND");
  }

  void up() {
    lrSpeed = 0;
    fbSpeed = 0;
    udSpeed = 30;
    yawSpeed = 0;
  }

  void down() {
    lrSpeed = 0;
    fbSpeed = 0;
    udSpeed = -30;
    yawSpeed = 0;
  }

  void setSpeed(int x, int y) {
    udSpeed = 0;
    yawSpeed = 0;
    if (x < minRange/2) {
      lrSpeed = -50;
    } else if (x > maxRange/2) {
      lrSpeed = 50;
    } else {
      lrSpeed = 0;
    }
    if (y < minRange/2) {
      fbSpeed = 50;
    } else if (y > maxRange/2) {
      fbSpeed = -50;
    } else {
      fbSpeed = 0;
    }
  }

  void convertItoS() {
    lrStr = str(lrSpeed);
    fdStr = str(fbSpeed);
    udStr = str(udSpeed);
    yawStr = str(yawSpeed);
  }
}

動作の様子

0bble-m8cnm.gif

最後に

ドローンとの接続方法などは,ここを参考にしてみてください!
https://qiita.com/tkyko13/items/a873ef8a34dc3f6dc67d

GitHubにもコードをまとめてあります.
kikpond15/tello_p5_microbit

2
2
2

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
2
2