From e3af211cc6b9f1dc2ca2fc3729fe76552bd51d9e Mon Sep 17 00:00:00 2001 From: MaximZaitsev Date: Thu, 14 Jul 2016 13:35:52 +0300 Subject: [PATCH] added proto file to raspberry server. added handler for command from car control --- car_net/stFlashServer/handlers.js | 44 +++++++++++++++++++++++-- car_net/stFlashServer/main.js | 22 ++++++++++--- car_net/stFlashServer/proto/route.proto | 25 ++++++++++++++ 3 files changed, 85 insertions(+), 6 deletions(-) create mode 100644 car_net/stFlashServer/proto/route.proto diff --git a/car_net/stFlashServer/handlers.js b/car_net/stFlashServer/handlers.js index 383a504bc3f..ac54f090ddc 100644 --- a/car_net/stFlashServer/handlers.js +++ b/car_net/stFlashServer/handlers.js @@ -8,7 +8,7 @@ const exec = require('child_process').exec; function loadBin(httpContent, response) { console.log(httpContent.length); - var uploadClass = main.protoConstructor.Upload; + var uploadClass = main.protoConstructorCarkot.Upload; var uploadObject = uploadClass.decode(httpContent); fs.writeFile(main.binFilePath, uploadObject.data.buffer, "binary", function (error) { var uploadResultClass = main.protoConstructor.UploadResult; @@ -54,5 +54,45 @@ function other(httpContent, response) { response.end(); } +//контроль машинкой, как с пульта управления +function control(httpContent, response) { + var directionClass = main.protoConstructorControl.Direction; + var directionObject = directionClass.decode(httpContent); + var resultByte; + switch (directionObject.command) { + case directionClass.Command.stop : + { + resultByte = 0; + break; + } + case directionClass.Command.forward : + { + resultByte = 1; + break; + } + + case directionClass.Command.backward : + { + resultByte = 2; + break; + } + case directionClass.Command.right : + { + resultByte = 4; + break; + } + case directionClass.Command.left : + { + resultByte = 3; + break; + } + + } + console.log("byte for car: " + resultByte); + response.writeHead(200, {"Content-Type": "text/plain"}); + response.end(); +} + exports.loadBin = loadBin; -exports.other = other; \ No newline at end of file +exports.other = other; +exports.control = control; diff --git a/car_net/stFlashServer/main.js b/car_net/stFlashServer/main.js index 23ffe617c1d..4e4fa0a209f 100644 --- a/car_net/stFlashServer/main.js +++ b/car_net/stFlashServer/main.js @@ -9,14 +9,27 @@ const protoBuf = require("protobufjs"); commander .version('1.0.0') - .option('-p, --protopath [path]', 'path to proto file. default ./proto/carkot.proto') + .option('-p, --protopath [path]', 'path to dir with proto files. need here carkot.proto and route.proto. default ./proto/') .option('-b, --binfile [path]', 'path to save bin file with name f.bin. default ./') .parse(process.argv); -const builder = protoBuf.loadProtoFile(commander.protopath ? commander.protopath : "./proto/carkot.proto"); -const protoConstructor = builder.build("carkot"); +//add slash to end of paths if need +if (commander.protopath) { + if (commander.protopath.substr(commander.protopath.length - 1) != "/") { + commander.protopath = commander.protopath + "/"; + } +} +if (commander.binfile) { + if (commander.binfile.substr(commander.binfile.length - 1) != "/") { + commander.binfile = commander.binfile + "/"; + } +} -exports.protoConstructor = protoConstructor; +const builderCarkot = protoBuf.loadProtoFile(commander.protopath ? commander.protopath + "carkot.proto" : "./proto/carkot.proto");//todo!!1 путь до каталога, прото файлы - в нём +const builderControl = protoBuf.loadProtoFile(commander.protopath ? commander.protopath + "route.proto" : "./proto/route.proto"); + +exports.protoConstructorCarkot = builderCarkot.build("carkot"); +exports.protoConstructorControl = builderControl.build("carkot"); exports.commandPrefix = "./st-flash write"; exports.binFilePath = (commander.binfile ? commander.binfile : "./") + "f.bin"; @@ -24,6 +37,7 @@ var handlers = require("./handlers.js"); var handle = {}; handle["/"] = handlers.other; handle["/loadBin"] = handlers.loadBin; +handle["/control"] = handlers.control; handle["/other"] = handlers.other; server.start(router.route, handle); \ No newline at end of file diff --git a/car_net/stFlashServer/proto/route.proto b/car_net/stFlashServer/proto/route.proto new file mode 100644 index 00000000000..50ac625c415 --- /dev/null +++ b/car_net/stFlashServer/proto/route.proto @@ -0,0 +1,25 @@ +syntax = "proto3"; +package carkot; + +option java_package = "proto.car"; +option java_outer_classname = "RouteP"; + +message Route { + repeated WayPoint way_points = 1; + + message WayPoint { + double distance = 2; + double angle_delta = 3; + } +} + +message Direction { + enum Command { + stop = 0; + forward = 1; + backward = 2; + left = 3; + right = 4; + } + Command command = 1; +}