From a41de68066007852d7d3dbf019d75b4caf7463ad Mon Sep 17 00:00:00 2001 From: Jakob Odersky Date: Wed, 24 Feb 2016 20:29:30 -0800 Subject: Major refactorings --- mavigator-uav/src/main/resources/application.conf | 37 -------------------- mavigator-uav/src/main/resources/reference.conf | 39 ++++++++++++++++++++++ .../src/main/scala/mavigator/uav/MavlinkUtil.scala | 6 ++-- .../src/main/scala/mavigator/uav/Uav.scala | 18 ++++++++++ .../scala/mavigator/uav/mock/MockConnection.scala | 36 +++++++------------- 5 files changed, 72 insertions(+), 64 deletions(-) delete mode 100644 mavigator-uav/src/main/resources/application.conf create mode 100644 mavigator-uav/src/main/resources/reference.conf (limited to 'mavigator-uav') diff --git a/mavigator-uav/src/main/resources/application.conf b/mavigator-uav/src/main/resources/application.conf deleted file mode 100644 index f468111..0000000 --- a/mavigator-uav/src/main/resources/application.conf +++ /dev/null @@ -1,37 +0,0 @@ -# Settings related to the connection with a UAV -mavigator.uav { - # The type of connection to use - # 'mock' or 'serial' - type = mock - - # Mavlink component ID used by this connection, - # in case it needs to inject messages. I.e. heartbeats - # will originate from this ID. - component_id = 1 - - # Interval in milliseconds between heartbeat messages injected by - # the connection - # 0 = no heartbeats injected - heartbeat = 2000 - - # Settings related to serial connections - serial { - # Serial port - port = "/dev/ttyUSB0" - # Baud rate (b/s) - baud = 115200 - # Use two stop bits - two_stop_bits = false - # Parity check - # 0 = None, 1 = Odd, 2 = Even - parity = 0 - } - - # Settings related to mock connections - mock { - # Mavlink system ID of the simulated UAV - remote_system_id = 42 - # Delay between simulated messages - frequency = 50 - } -} \ No newline at end of file diff --git a/mavigator-uav/src/main/resources/reference.conf b/mavigator-uav/src/main/resources/reference.conf new file mode 100644 index 0000000..3b43d37 --- /dev/null +++ b/mavigator-uav/src/main/resources/reference.conf @@ -0,0 +1,39 @@ +# Settings related to the connection with a UAV +mavigator.uav { + # The type of connection to use + # 'mock' or 'serial' + type = mock + + # Mavlink component ID used by this connection, + # in case it needs to inject messages. I.e. heartbeats + # will originate from this ID. + component_id = 1 + + # Interval in milliseconds between heartbeat messages injected by + # the connection + # 0 = no heartbeats injected + heartbeat = 2000 + + # Settings related to serial connections + serial { + # Serial port + port = "/dev/ttyUSB0" + # Baud rate (b/s) + baud = 115200 + # Use two stop bits + two_stop_bits = false + # Parity check + # 0 = None, 1 = Odd, 2 = Even + parity = 0 + } + + # Settings related to mock connections + mock { + # Mavlink system ID of the simulated UAV + remote_system_id = 42 + # Mavlink component ID of the simulated UAV + remote_system_id = 0 + # Divide simulated message frequency + prescaler = 1 + } +} \ No newline at end of file diff --git a/mavigator-uav/src/main/scala/mavigator/uav/MavlinkUtil.scala b/mavigator-uav/src/main/scala/mavigator/uav/MavlinkUtil.scala index 3756dd6..0fb56b0 100644 --- a/mavigator-uav/src/main/scala/mavigator/uav/MavlinkUtil.scala +++ b/mavigator-uav/src/main/scala/mavigator/uav/MavlinkUtil.scala @@ -11,16 +11,16 @@ import akka.util.ByteString /** Provides utilities for actors representing a mavlink connection. */ trait MavlinkUtil { myself: Actor with ActorLogging => - + /** Mavlink system ID of this connection. */ val systemId: Byte /** Mavlink component ID of this connection. */ val componentId: Byte - + /** Assembler for creating packets originating from this connection. */ private lazy val assembler = new Assembler(systemId, componentId) - + /** Assembles a message into a bytestring representing a packet sent from this connection. */ protected def assemble(message: Message): ByteString = { val (messageId, payload) = Message.pack(message) diff --git a/mavigator-uav/src/main/scala/mavigator/uav/Uav.scala b/mavigator-uav/src/main/scala/mavigator/uav/Uav.scala index ed677ea..06a8f00 100644 --- a/mavigator-uav/src/main/scala/mavigator/uav/Uav.scala +++ b/mavigator-uav/src/main/scala/mavigator/uav/Uav.scala @@ -1,6 +1,7 @@ package mavigator package uav +import java.lang.IllegalArgumentException import mock._ import akka._ import akka.actor._ @@ -10,6 +11,23 @@ import akka.stream.scaladsl._ class Uav(system: ExtendedActorSystem) extends Extension { private lazy val config = system.settings.config.getConfig("mavigator.uav") + private lazy val tpe = config.getString("type") + private lazy val componentId = config.getInt("componentId").toByte + private lazy val heartbeat = config.getInt("heartbeat") + private lazy val connection = config.getConfig(tpe) + + lazy val source = tpe match { + case "mock" => + new MockConnection( + connection.getInt("remote_system_id").toByte, + componentId, + connection.getDouble("prescaler") + ) + + case "serial" => ??? + + case _ => throw new IllegalArgumentException(s"Unsupported connection type: $tpe") + } def connect(): Flow[ByteString, ByteString, NotUsed] = { Flow.fromSinkAndSource( diff --git a/mavigator-uav/src/main/scala/mavigator/uav/mock/MockConnection.scala b/mavigator-uav/src/main/scala/mavigator/uav/mock/MockConnection.scala index c7bc8d4..58b4977 100644 --- a/mavigator-uav/src/main/scala/mavigator/uav/mock/MockConnection.scala +++ b/mavigator-uav/src/main/scala/mavigator/uav/mock/MockConnection.scala @@ -2,19 +2,16 @@ package mavigator package uav package mock -import org.mavlink.enums._ -import org.mavlink.messages.{Heartbeat, Message} -import akka.stream.scaladsl._ import scala.concurrent.duration._ -import scala.util.Random -import akka.stream._ + import akka.NotUsed +import akka.stream._ +import akka.stream.Attributes._ +import akka.stream.scaladsl._ import akka.util._ import org.mavlink._ -import Attributes._ - +import org.mavlink.messages.{Heartbeat, Message} -//case class Heartbeat(`type`: Byte, autopilot: Byte, baseMode: Byte, customMode: Int, systemStatus: Byte, mavlinkVersion: Byte) extends Message class MockConnection( remoteSystemId: Byte, @@ -23,12 +20,14 @@ class MockConnection( ) { import MockConnection._ + private lazy val assembler = new Assembler(remoteSystemId, remoteComponentId) + private def delayed(delaySeconds: Double)(message: RandomFlightPlan => Message): Flow[RandomFlightPlan, Message, NotUsed] = { val dt = delaySeconds / prescaler Flow[RandomFlightPlan].withAttributes(inputBuffer(1,1)).delay(dt.seconds).map(message) } - val messages: Source[Message, _] = streamFromPlan(new RandomFlightPlan)( + private val messages: Source[Message, NotUsed] = fromPlan(new RandomFlightPlan)( delayed(2)(_.heartbeat), delayed(0.2)(_.position), delayed(0.05)(_.attitude), @@ -36,31 +35,20 @@ class MockConnection( delayed(0.1)(_.distance) ) - - private lazy val assembler = new Assembler(remoteSystemId, remoteComponentId) - - /** Assembles a message into a bytestring representing a packet sent from this connection. */ - def assemble(message: Message): ByteString = { + val data: Source[ByteString, NotUsed] = messages.map{ message => val (messageId, payload) = Message.pack(message) - val packet: Packet = assembler.assemble(messageId, payload) + val packet = assembler.assemble(messageId, payload) ByteString(packet.toArray) } - val data = messages.map{ msg => - if (msg.isInstanceOf[Heartbeat]){println(msg)} - assemble(msg) - } - } object MockConnection { - final val ClockTick: FiniteDuration = 0.01.seconds - - private def streamFromPlan(plan: RandomFlightPlan)(messages: Flow[RandomFlightPlan, Message, _]*): Source[Message, NotUsed] = { + final val ClockTick: FiniteDuration = 0.02.seconds + private def fromPlan(plan: RandomFlightPlan)(messages: Flow[RandomFlightPlan, Message, _]*): Source[Message, NotUsed] = { import GraphDSL.Implicits._ - Source.fromGraph(GraphDSL.create() { implicit b => val clock = Source.tick(ClockTick, ClockTick, plan) map { plan => -- cgit v1.2.3