diff options
author | Jakob Odersky <jodersky@gmail.com> | 2014-11-06 10:28:36 +0100 |
---|---|---|
committer | Jakob Odersky <jodersky@gmail.com> | 2014-11-06 10:28:36 +0100 |
commit | 2915f8bf09b4d4db6f5e1b574ba988a0fc0fb90a (patch) | |
tree | fe5da7fd7b93feb8682a0beb65bc501482d75839 /vfd-backend | |
parent | 850863823caf3c33e7f020d43877143ce8de00f5 (diff) | |
download | mavigator-2915f8bf09b4d4db6f5e1b574ba988a0fc0fb90a.tar.gz mavigator-2915f8bf09b4d4db6f5e1b574ba988a0fc0fb90a.tar.bz2 mavigator-2915f8bf09b4d4db6f5e1b574ba988a0fc0fb90a.zip |
migrate to using mavlink
Diffstat (limited to 'vfd-backend')
-rw-r--r-- | vfd-backend/.gitignore | 3 | ||||
-rw-r--r-- | vfd-backend/app/controllers/Application.scala | 12 | ||||
-rw-r--r-- | vfd-backend/app/plugins/UavPlugin.scala | 71 | ||||
-rw-r--r-- | vfd-backend/app/util/package.scala | 22 | ||||
-rw-r--r-- | vfd-backend/conf/application.conf | 25 | ||||
-rw-r--r-- | vfd-backend/conf/routes | 2 |
6 files changed, 74 insertions, 61 deletions
diff --git a/vfd-backend/.gitignore b/vfd-backend/.gitignore index ed1de16..1d1f507 100644 --- a/vfd-backend/.gitignore +++ b/vfd-backend/.gitignore @@ -22,4 +22,5 @@ project/plugins/project/ *.swp *.class *.log -*~
\ No newline at end of file +*~ +/bin/ diff --git a/vfd-backend/app/controllers/Application.scala b/vfd-backend/app/controllers/Application.scala index efdf1bd..1c1fe13 100644 --- a/vfd-backend/app/controllers/Application.scala +++ b/vfd-backend/app/controllers/Application.scala @@ -7,26 +7,20 @@ import play.api.Play.current import play.api.mvc.WebSocket.FrameFormatter import play.api.libs.json._ - -import vfd.uav.DataFrame import plugins.UavPlugin - object Application extends Controller { - implicit val dataFrameFormat = spicklerFormat[DataFrame] - implicit val dataFrameFormatter = FrameFormatter.jsonFrame[DataFrame] - def use[A <: Plugin](implicit app: Application, m: Manifest[A]) = { app.plugin[A].getOrElse(throw new RuntimeException(m.runtimeClass.toString + " plugin should be available at this point")) } def index = Action { implicit request => - Ok(views.html.index(routes.Application.socket.webSocketURL())) + Ok(views.html.index(routes.Application.mavlink.webSocketURL())) } - - def socket = WebSocket.acceptWithActor[String, DataFrame] { implicit request => + def mavlink = WebSocket.acceptWithActor[Array[Byte], Array[Byte]] { implicit request => out => use[UavPlugin].register(out) } + }
\ No newline at end of file diff --git a/vfd-backend/app/plugins/UavPlugin.scala b/vfd-backend/app/plugins/UavPlugin.scala index a94ed9d..19be049 100644 --- a/vfd-backend/app/plugins/UavPlugin.scala +++ b/vfd-backend/app/plugins/UavPlugin.scala @@ -1,44 +1,65 @@ package plugins -import akka.actor._ -import play.api._ +import akka.actor.Actor +import akka.actor.ActorRef +import akka.actor.Props +import akka.actor.actorRef2Scala +import play.api.Application +import play.api.Plugin import play.api.libs.concurrent.Akka -import vfd.uav._ +import vfd.uav.Connection +import vfd.uav.DummyConnection +import vfd.uav.SerialConnection class UavPlugin(app: Application) extends Plugin { - object conf { - private val config = app.configuration.getConfig("uav") - val connection = config.flatMap(_.getString("connection")).getOrElse("mock") - val port = config.flatMap(_.getString("port")).getOrElse("/dev/ttyACM0") - val baud = config.flatMap(_.getInt("baud")).getOrElse(9600) - } + private lazy val config = app.configuration.getConfig("uav") + + lazy val systemId = config.flatMap(_.getInt("system_id")).getOrElse(1) + + private lazy val connection = { + val conn = config.flatMap(_.getConfig("connection")) + val tpe = conn.flatMap(_.getString("type")).getOrElse("mock") + val heartbeat = conn.flatMap(_.getInt("heartbeat")).getOrElse(2000) + val id = conn.flatMap(_.getInt("component_id")).getOrElse(99).toByte + + val props = tpe match { + case "mock" => + DummyConnection.apply + + case "serial" => + val serial = config.flatMap(_.getConfig("serial")) + SerialConnection( + id, + heartbeat, + serial.flatMap(_.getString("port")).getOrElse("/dev/ttyUSB0"), + serial.flatMap(_.getInt("baud")).getOrElse(115200), + serial.flatMap(_.getBoolean("two_stop_bits")).getOrElse(false), + serial.flatMap(_.getInt("parity")).getOrElse(0)) - lazy val connection: ActorRef = { - val props = conf.connection match { - case "mock" => Connection.dummy - case "fcu" => Connection.fcu(conf.port, conf.baud) - case _ => throw new RuntimeException("Unknown connection type.") + case unknown => throw new RuntimeException("Unsupported connection type '" + unknown + "'") } - Akka.system(app).actorOf(props, name = "uav") + + Akka.system(app).actorOf(props, name = "uav-connection") + } - def register(out: ActorRef): Props = Repeater(out, connection) + def register(out: ActorRef): Props = Props(classOf[Repeater], out, connection) } class Repeater(out: ActorRef, connection: ActorRef) extends Actor { - override def preStart = { - connection ! Connection.Register - } + override def preStart = { + connection ! Connection.Register + } - def receive = { - case Connection.NewDataFrame(df) => out ! df - } - + def receive = { + case msg => + if (sender == connection) + out ! msg + else + connection ! msg } -object Repeater { - def apply(out: ActorRef, connection: ActorRef) = Props(classOf[Repeater], out, connection) }
\ No newline at end of file diff --git a/vfd-backend/app/util/package.scala b/vfd-backend/app/util/package.scala deleted file mode 100644 index 4a6d2e6..0000000 --- a/vfd-backend/app/util/package.scala +++ /dev/null @@ -1,22 +0,0 @@ - - -import org.scalajs.spickling._ -import org.scalajs.spickling.playjson._ -import play.api.data.validation.ValidationError -import play.api.libs.json._ - - -package object util { - - def spicklerFormat[A](implicit manifest: Manifest[A], pickler: Pickler[A], unpickler: Unpickler[A]) = new Format[A] { - PicklerRegistry.register[A] - - def writes(o: A): JsValue = PicklerRegistry.pickle(o) - - def reads(j: JsValue): JsResult[A] = PicklerRegistry.unpickle(j) match { - case a: A => JsSuccess(a) - case _ => JsError("unpickling yielded wrong type") - } - } - -}
\ No newline at end of file diff --git a/vfd-backend/conf/application.conf b/vfd-backend/conf/application.conf index 96478c0..1559b89 100644 --- a/vfd-backend/conf/application.conf +++ b/vfd-backend/conf/application.conf @@ -61,8 +61,27 @@ logger.play=INFO logger.application=DEBUG # UAV -uav.connection=mock -uav.port="/dev/ttyACM0" -uav.baud=9600 +# ~~~~~ +# Settings to control the communication with UAVs + +# Mavlink system id identifying this base station +uav.system_id=1 + +# Type of connection to use +# 'mock' for a sample connection +uav.connection.type=serial + +# Mavlink component id used by this connection +# (not the web frontend), in case it needs to inject messages +uav.connection.component_id=99 +# Delay in milliseconds between heartbeat messages injected by +# the connection +# 0 = no heartbeats injected +uav.connection.heartbeat=2000 +# Serial connection specific +uav.serial.port="/dev/ttyUSB0" +uav.serial.baud=115200 +uav.serial.two_stop_bits=false +uav.serial.parity=0
\ No newline at end of file diff --git a/vfd-backend/conf/routes b/vfd-backend/conf/routes index b211b07..45b6ab9 100644 --- a/vfd-backend/conf/routes +++ b/vfd-backend/conf/routes @@ -4,7 +4,7 @@ # Home page GET / controllers.Application.index -GET /socket controllers.Application.socket +GET /mavlink controllers.Application.mavlink # Map static resources from the /public folder to the /assets URL path GET /assets/*file controllers.Assets.at(path="/public", file) |