ここからは応用編です。
Raspberry Pi対応版 ScratchのTELLO拡張機能に、ストリーミングのオンオフのコードを入れたブロックを追加します。
これにより、ScratchでTelloのカメラ画像のストリーミングのオンオフをブロックで制御できるようになり、ストリーミングがオンの間Telloのカメラのリアルタイム動画をRaspberry Piのモニタに表示したり、ファイルに録画することができるようになります。
それでは、これまでのレシピで使っていた、「Downloads」ディレクトリ直下にある「tellocont.js」と「tellocontJp.js」を以下の通りターミナルから編集して、「tellocont2.js」と「tellocontJp2.js」として保存し、ストリーミングのオンオフを制御できるブロックを追加します。
まず、tellocont.jsをtellocont2.jsにコピーします。
pi@raspberrypi:~/Downloads/Scratch $ cd ~/Downloads[ENTER]
pi@raspberrypi:~/Downloads $ cp -ip tellocont.js tellocont2.js[ENTER]
|
続いてtellocont2.jsを以下の通り編集し、ストリーミングのオンオフのコードを入れた英語表示のブロックを追加します。
pi@raspberrypi:~/Downloads $ vi tellocont2.js[ENTER]
|
赤文字の部分を追加してください。
// Scratch JS extension for Ryze Tello
// This requires Tello.js (node.js HTTP server app.)
(function(ext) {
baseURL = "http://localhost:8001";
// Cleanup function when the extension is unloaded
ext._shutdown = function() {};
// Status reporting code
ext._getStatus = function() {
return {status: 2, msg: 'Ready'};
};
//Takeoff command
ext.takeoff_block = function() {
cmdURL = baseURL + "/takeoff";
$.ajax({
type: "GET",
url: cmdURL,
});
};
//Land command
ext.land_block = function() {
cmdURL = baseURL + "/land";
$.ajax({
type: "GET",
url: cmdURL,
});
};
//Fly up command
ext.up_block = function(dist) {
cmdURL = baseURL + "/up/" + dist;
$.ajax({
type: "GET",
url: cmdURL,
});
};
//Fly down command
ext.down_block = function(dist) {
cmdURL = baseURL + "/down/" + dist;
$.ajax({
type: "GET",
url: cmdURL,
});
};
//Fly left command
ext.left_block = function(dist) {
cmdURL = baseURL + "/left/" + dist;
$.ajax({
type: "GET",
url: cmdURL,
});
};
//Fly right command
ext.right_block = function(dist) {
cmdURL = baseURL + "/right/" + dist;
$.ajax({
type: "GET",
url: cmdURL,
});
};
//Fly forward command
ext.forward_block = function(dist) {
cmdURL = baseURL + "/forward/" + dist;
$.ajax({
type: "GET",
url: cmdURL,
});
};
//Fly back command
ext.back_block = function(dist) {
cmdURL = baseURL + "/back/" + dist;
$.ajax({
type: "GET",
url: cmdURL,
});
};
//Fly CW command
ext.cw_block = function(and) {
cmdURL = baseURL + "/cw/" + and;
$.ajax({
type: "GET",
url: cmdURL,
});
};
//Fly CCW command
ext.ccw_block = function(ang) {
cmdURL = baseURL + "/ccw/" + ang;
$.ajax({
type: "GET",
url: cmdURL,
});
};
//Flip command
ext.flip_block = function(mode) {
cmdURL = baseURL + "/flip/" + mode;
$.ajax({
type: "GET",
url: cmdURL,
});
};
//Set speed command
ext.setspeed_block = function(speed) {
cmdURL = baseURL + "/setspeed/" + speed;
$.ajax({
type: "GET",
url: cmdURL,
});
};
//Stream ON command
ext.setstreamon_block = function() {
cmdURL = baseURL + "/streamon";
$.ajax({
type: "GET",
url: cmdURL,
});
};
//Stream OFF command
ext.land_block = function() {
cmdURL = baseURL + "/streamoff";
$.ajax({
type: "GET",
url: cmdURL,
});
};
// Block and block menu descriptions
var descriptor = {
blocks: [
// Block type, block name, function name
[' ', 'takeoff', 'takeoff_block'],
[' ', 'land', 'land_block'],
[' ', 'fly up with distance %n', 'up_block', 20],
[' ', 'fly down with distance %n', 'down_block', 20],
[' ', 'fly left with distance %n', 'left_block', 20],
[' ', 'fly right with distance %n', 'right_block', 20],
[' ', 'fly forward with distance %n', 'forward_block', 20],
[' ', 'fly back with distance %n', 'back_block', 20],
[' ', 'rotate CW with angle %n', 'cw_block', 90],
[' ', 'rotate CCW with angle %n', 'ccw_block', 90],
[' ', 'flip with duration %s', 'flip_block', 'f'],
[' ', 'set speed %n', 'setspeed_block', 80],
[' ', 'Stream ON', 'setstreamon_block', 80],
[' ', 'Stream OFF', 'setstreamoff_block', 'f'],
]
};
// Register the extension
ScratchExtensions.register('Tello Control', descriptor, ext);
})({});
|
次に、tellocontJp.jsをtellocontJp2.jsにコピーします。
pi@raspberrypi:~/Downloads/Scratch $ cd ~/Downloads[ENTER]
pi@raspberrypi:~/Downloads $ cp -ip tellocontJp.js tellocontJp2.js[ENTER]
|
続いてtellocontJp2.jsを以下の通り編集し、ストリーミングのオンオフのコードを入れた日本語表示のブロックを追加します。
pi@raspberrypi:~/Downloads $ vi tellocontJp2.js[ENTER]
|
赤文字の部分を追加してください。
// Scratch JS extension for Ryze Tello
// This requires Tello.js (node.js HTTP server app.)
(function(ext) {
baseURL = "http://localhost:8001";
// Cleanup function when the extension is unloaded
ext._shutdown = function() {};
// Status reporting code
ext._getStatus = function() {
return {status: 2, msg: 'Ready'};
};
//Takeoff command
ext.takeoff_block = function() {
cmdURL = baseURL + "/takeoff";
$.ajax({
type: "GET",
url: cmdURL,
});
};
//Land command
ext.land_block = function() {
cmdURL = baseURL + "/land";
$.ajax({
type: "GET",
url: cmdURL,
});
};
//Fly up command
ext.up_block = function(dist) {
cmdURL = baseURL + "/up/" + dist;
$.ajax({
type: "GET",
url: cmdURL,
});
};
//Fly down command
ext.down_block = function(dist) {
cmdURL = baseURL + "/down/" + dist;
$.ajax({
type: "GET",
url: cmdURL,
});
};
//Fly left command
ext.left_block = function(dist) {
cmdURL = baseURL + "/left/" + dist;
$.ajax({
type: "GET",
url: cmdURL,
});
};
//Fly right command
ext.right_block = function(dist) {
cmdURL = baseURL + "/right/" + dist;
$.ajax({
type: "GET",
url: cmdURL,
});
};
//Fly forward command
ext.forward_block = function(dist) {
cmdURL = baseURL + "/forward/" + dist;
$.ajax({
type: "GET",
url: cmdURL,
});
};
//Fly back command
ext.back_block = function(dist) {
cmdURL = baseURL + "/back/" + dist;
$.ajax({
type: "GET",
url: cmdURL,
});
};
//Fly CW command
ext.cw_block = function(and) {
cmdURL = baseURL + "/cw/" + and;
$.ajax({
type: "GET",
url: cmdURL,
});
};
//Fly CCW command
ext.ccw_block = function(ang) {
cmdURL = baseURL + "/ccw/" + ang;
$.ajax({
type: "GET",
url: cmdURL,
});
};
//Flip command
ext.flip_block = function(mode) {
cmdURL = baseURL + "/flip/" + mode;
$.ajax({
type: "GET",
url: cmdURL,
});
};
//Set speed command
ext.setspeed_block = function(speed) {
cmdURL = baseURL + "/setspeed/" + speed;
$.ajax({
type: "GET",
url: cmdURL,
});
};
//Stream ON command
ext.setstreamon_block = function() {
cmdURL = baseURL + "/streamon";
$.ajax({
type: "GET",
url: cmdURL,
});
};
//Stream OFF command
ext.land_block = function() {
cmdURL = baseURL + "/streamoff";
$.ajax({
type: "GET",
url: cmdURL,
});
};
// Block and block menu descriptions
var descriptor = {
blocks: [
// Block type, block name, function name
[' ', 'takeoff', 'takeoff_block'],
[' ', 'land', 'land_block'],
[' ', 'fly up with distance %n', 'up_block', 20],
[' ', 'fly down with distance %n', 'down_block', 20],
[' ', 'fly left with distance %n', 'left_block', 20],
[' ', 'fly right with distance %n', 'right_block', 20],
[' ', 'fly forward with distance %n', 'forward_block', 20],
[' ', 'fly back with distance %n', 'back_block', 20],
[' ', 'rotate CW with angle %n', 'cw_block', 90],
[' ', 'rotate CCW with angle %n', 'ccw_block', 90],
[' ', 'flip with duration %s', 'flip_block', 'f'],
[' ', 'set speed %n', 'setspeed_block', 80],
[' ', '動画開始', 'setstreamon_block', 80],
[' ', '動画終了', 'setstreamoff_block', 'f'],
]
};
// Register the extension
ScratchExtensions.register('Tello Control', descriptor, ext);
})({});
|
「tellocont2.js」が英語「tellocontJp2.js」が日本語になります。
念のためこちらのサイトにも公開しておきます。
tellocont2.zip
|