From 270dd8e36290ea16df77f26306d685126115092b Mon Sep 17 00:00:00 2001 From: Piv <18462828+Piv200@users.noreply.github.com> Date: Thu, 7 May 2020 20:54:26 +0930 Subject: [PATCH 1/6] initial Swifty Car commit --- SwiftyCar/Package.swift | 28 +++++++++++++++++++ SwiftyCar/Sources/SwiftyCar/SwiftyCar.swift | 3 ++ SwiftyCar/Tests/LinuxMain.swift | 7 +++++ .../Tests/SwiftyCarTests/SwiftyCarTests.swift | 15 ++++++++++ .../SwiftyCarTests/XCTestManifests.swift | 9 ++++++ 5 files changed, 62 insertions(+) create mode 100644 SwiftyCar/Package.swift create mode 100644 SwiftyCar/Sources/SwiftyCar/SwiftyCar.swift create mode 100644 SwiftyCar/Tests/LinuxMain.swift create mode 100644 SwiftyCar/Tests/SwiftyCarTests/SwiftyCarTests.swift create mode 100644 SwiftyCar/Tests/SwiftyCarTests/XCTestManifests.swift diff --git a/SwiftyCar/Package.swift b/SwiftyCar/Package.swift new file mode 100644 index 0000000..5a6fb5e --- /dev/null +++ b/SwiftyCar/Package.swift @@ -0,0 +1,28 @@ +// swift-tools-version:5.2 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import PackageDescription + +let package = Package( + name: "SwiftyCar", + products: [ + // Products define the executables and libraries produced by a package, and make them visible to other packages. + .library( + name: "SwiftyCar", + targets: ["SwiftyCar"]), + ], + dependencies: [ + // Dependencies declare other packages that this package depends on. + // .package(url: /* package url */, from: "1.0.0"), + ], + targets: [ + // Targets are the basic building blocks of a package. A target can define a module or a test suite. + // Targets can depend on other targets in this package, and on products in packages which this package depends on. + .target( + name: "SwiftyCar", + dependencies: []), + .testTarget( + name: "SwiftyCarTests", + dependencies: ["SwiftyCar"]), + ] +) diff --git a/SwiftyCar/Sources/SwiftyCar/SwiftyCar.swift b/SwiftyCar/Sources/SwiftyCar/SwiftyCar.swift new file mode 100644 index 0000000..d091fe7 --- /dev/null +++ b/SwiftyCar/Sources/SwiftyCar/SwiftyCar.swift @@ -0,0 +1,3 @@ +struct SwiftyCar { + var text = "Hello, World!" +} diff --git a/SwiftyCar/Tests/LinuxMain.swift b/SwiftyCar/Tests/LinuxMain.swift new file mode 100644 index 0000000..c68226f --- /dev/null +++ b/SwiftyCar/Tests/LinuxMain.swift @@ -0,0 +1,7 @@ +import XCTest + +import SwiftyCarTests + +var tests = [XCTestCaseEntry]() +tests += SwiftyCarTests.allTests() +XCTMain(tests) diff --git a/SwiftyCar/Tests/SwiftyCarTests/SwiftyCarTests.swift b/SwiftyCar/Tests/SwiftyCarTests/SwiftyCarTests.swift new file mode 100644 index 0000000..2d727ae --- /dev/null +++ b/SwiftyCar/Tests/SwiftyCarTests/SwiftyCarTests.swift @@ -0,0 +1,15 @@ +import XCTest +@testable import SwiftyCar + +final class SwiftyCarTests: XCTestCase { + func testExample() { + // This is an example of a functional test case. + // Use XCTAssert and related functions to verify your tests produce the correct + // results. + XCTAssertEqual(SwiftyCar().text, "Hello, World!") + } + + static var allTests = [ + ("testExample", testExample), + ] +} diff --git a/SwiftyCar/Tests/SwiftyCarTests/XCTestManifests.swift b/SwiftyCar/Tests/SwiftyCarTests/XCTestManifests.swift new file mode 100644 index 0000000..02cc67f --- /dev/null +++ b/SwiftyCar/Tests/SwiftyCarTests/XCTestManifests.swift @@ -0,0 +1,9 @@ +import XCTest + +#if !canImport(ObjectiveC) +public func allTests() -> [XCTestCaseEntry] { + return [ + testCase(SwiftyCarTests.allTests), + ] +} +#endif From 98dc0395e3f98f6ba65feb55b5258a34500d6dee Mon Sep 17 00:00:00 2001 From: Piv <18462828+Piv200@users.noreply.github.com> Date: Fri, 8 May 2020 18:24:20 +0930 Subject: [PATCH 2/6] Add initial grpc generation, and add to gradle build --- .gitignore | 13 +++- SwiftyCar/Package.resolved | 70 +++++++++++++++++++++ SwiftyCar/Package.swift | 9 +-- SwiftyCar/Sources/SwiftyCar/SwiftyCar.swift | 3 - SwiftyCar/Sources/SwiftyCar/main.swift | 10 +++ SwiftyCar/build.gradle | 16 +++++ settings.gradle | 1 + 7 files changed, 114 insertions(+), 8 deletions(-) create mode 100644 SwiftyCar/Package.resolved delete mode 100644 SwiftyCar/Sources/SwiftyCar/SwiftyCar.swift create mode 100644 SwiftyCar/Sources/SwiftyCar/main.swift create mode 100644 SwiftyCar/build.gradle diff --git a/.gitignore b/.gitignore index 5d781f8..bd3e4d6 100644 --- a/.gitignore +++ b/.gitignore @@ -20,4 +20,15 @@ build __pycache__ Pods -CarControlleriOS/**/car \ No newline at end of file +CarControlleriOS/**/car +car/.idea + +.build +SwiftyCar/Packages +SwiftyCar/*.xcodeproj +SwiftyCar/Sources/Generated +SwiftyCar/xcuserdata/ +xcuserdata/ +.settings +.project +.classpath \ No newline at end of file diff --git a/SwiftyCar/Package.resolved b/SwiftyCar/Package.resolved new file mode 100644 index 0000000..12fe0a3 --- /dev/null +++ b/SwiftyCar/Package.resolved @@ -0,0 +1,70 @@ +{ + "object": { + "pins": [ + { + "package": "grpc-swift", + "repositoryURL": "https://github.com/grpc/grpc-swift.git", + "state": { + "branch": null, + "revision": "373ffd54c1c1a319d0ddac9476d13be9023584bb", + "version": "1.0.0-alpha.11" + } + }, + { + "package": "swift-log", + "repositoryURL": "https://github.com/apple/swift-log.git", + "state": { + "branch": null, + "revision": "74d7b91ceebc85daf387ebb206003f78813f71aa", + "version": "1.2.0" + } + }, + { + "package": "swift-nio", + "repositoryURL": "https://github.com/apple/swift-nio.git", + "state": { + "branch": null, + "revision": "40bdad80882d307abe2c0bb36cf3bd4d3e03fe04", + "version": "2.16.1" + } + }, + { + "package": "swift-nio-http2", + "repositoryURL": "https://github.com/apple/swift-nio-http2.git", + "state": { + "branch": null, + "revision": "82eb3fa0974b838358ee46bc6c5381e5ae5de6b9", + "version": "1.11.0" + } + }, + { + "package": "swift-nio-ssl", + "repositoryURL": "https://github.com/apple/swift-nio-ssl.git", + "state": { + "branch": null, + "revision": "ae213938e151964aa691f0e902462fbe06baeeb6", + "version": "2.7.1" + } + }, + { + "package": "swift-nio-transport-services", + "repositoryURL": "https://github.com/apple/swift-nio-transport-services.git", + "state": { + "branch": null, + "revision": "85a67aea7caf5396ed599543dd23cffeb6dbbf96", + "version": "1.5.1" + } + }, + { + "package": "SwiftProtobuf", + "repositoryURL": "https://github.com/apple/swift-protobuf.git", + "state": { + "branch": null, + "revision": "7790acf0a81d08429cb20375bf42a8c7d279c5fe", + "version": "1.8.0" + } + } + ] + }, + "version": 1 +} diff --git a/SwiftyCar/Package.swift b/SwiftyCar/Package.swift index 5a6fb5e..ff4bc2e 100644 --- a/SwiftyCar/Package.swift +++ b/SwiftyCar/Package.swift @@ -7,20 +7,21 @@ let package = Package( name: "SwiftyCar", products: [ // Products define the executables and libraries produced by a package, and make them visible to other packages. - .library( - name: "SwiftyCar", - targets: ["SwiftyCar"]), ], dependencies: [ // Dependencies declare other packages that this package depends on. // .package(url: /* package url */, from: "1.0.0"), + .package(url: "https://github.com/grpc/grpc-swift.git", from: "1.0.0-alpha.11") ], targets: [ // Targets are the basic building blocks of a package. A target can define a module or a test suite. // Targets can depend on other targets in this package, and on products in packages which this package depends on. + .target( + name: "Generated", + dependencies: [.product(name: "GRPC", package: "grpc-swift")]), .target( name: "SwiftyCar", - dependencies: []), + dependencies: ["Generated"]), .testTarget( name: "SwiftyCarTests", dependencies: ["SwiftyCar"]), diff --git a/SwiftyCar/Sources/SwiftyCar/SwiftyCar.swift b/SwiftyCar/Sources/SwiftyCar/SwiftyCar.swift deleted file mode 100644 index d091fe7..0000000 --- a/SwiftyCar/Sources/SwiftyCar/SwiftyCar.swift +++ /dev/null @@ -1,3 +0,0 @@ -struct SwiftyCar { - var text = "Hello, World!" -} diff --git a/SwiftyCar/Sources/SwiftyCar/main.swift b/SwiftyCar/Sources/SwiftyCar/main.swift new file mode 100644 index 0000000..115b18d --- /dev/null +++ b/SwiftyCar/Sources/SwiftyCar/main.swift @@ -0,0 +1,10 @@ +// +// main.swift +// +// +// Created by Michael Pivato on 8/5/20. +// + +// Entry-Point to the Swift Car Controller +print("Hello World!") + diff --git a/SwiftyCar/build.gradle b/SwiftyCar/build.gradle new file mode 100644 index 0000000..158e34f --- /dev/null +++ b/SwiftyCar/build.gradle @@ -0,0 +1,16 @@ +configurations{ + swift { + canBeConsumed = false + canBeResolved = true + } +} + +dependencies { + swift project(path: ':protobuf', configuration: 'swift') +} + +task copySwiftCode(type: Copy, dependsOn: configurations.swift) { + // Copy python protobuf code from proto project. + from zipTree(configurations.swift.asPath) + into './Sources/Generated' +} \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index 553d966..1b5247a 100644 --- a/settings.gradle +++ b/settings.gradle @@ -2,4 +2,5 @@ include ':app' include ':protobuf' include 'car' include 'CarControlleriOS' +include 'SwiftyCar' rootProject.name='CarController' From 9c810f2731b20ca8bd517c72012ef0dbdefa0d83 Mon Sep 17 00:00:00 2001 From: Piv <18462828+Piv200@users.noreply.github.com> Date: Fri, 8 May 2020 18:26:05 +0930 Subject: [PATCH 3/6] Add SwiftyGPIO --- SwiftyCar/Package.resolved | 9 +++++++++ SwiftyCar/Package.swift | 5 +++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/SwiftyCar/Package.resolved b/SwiftyCar/Package.resolved index 12fe0a3..d621cf8 100644 --- a/SwiftyCar/Package.resolved +++ b/SwiftyCar/Package.resolved @@ -63,6 +63,15 @@ "revision": "7790acf0a81d08429cb20375bf42a8c7d279c5fe", "version": "1.8.0" } + }, + { + "package": "SwiftyGPIO", + "repositoryURL": "https://github.com/uraimo/SwiftyGPIO.git", + "state": { + "branch": null, + "revision": "4127ff9dd5c6aa8acb6be34b7ce2af2f6e0b942d", + "version": "1.1.14" + } } ] }, diff --git a/SwiftyCar/Package.swift b/SwiftyCar/Package.swift index ff4bc2e..99a62f3 100644 --- a/SwiftyCar/Package.swift +++ b/SwiftyCar/Package.swift @@ -11,7 +11,8 @@ let package = Package( dependencies: [ // Dependencies declare other packages that this package depends on. // .package(url: /* package url */, from: "1.0.0"), - .package(url: "https://github.com/grpc/grpc-swift.git", from: "1.0.0-alpha.11") + .package(url: "https://github.com/grpc/grpc-swift.git", from: "1.0.0-alpha.11"), + .package(url: "https://github.com/uraimo/SwiftyGPIO.git", from: "1.0.0"), ], targets: [ // Targets are the basic building blocks of a package. A target can define a module or a test suite. @@ -21,7 +22,7 @@ let package = Package( dependencies: [.product(name: "GRPC", package: "grpc-swift")]), .target( name: "SwiftyCar", - dependencies: ["Generated"]), + dependencies: ["Generated", "SwiftyGPIO"]), .testTarget( name: "SwiftyCarTests", dependencies: ["SwiftyCar"]), From de50b87d889c6f3c230dc32ad96641b7eb26e64e Mon Sep 17 00:00:00 2001 From: Piv <18462828+Piv200@users.noreply.github.com> Date: Wed, 13 May 2020 21:17:51 +0930 Subject: [PATCH 4/6] Bulk of swift code done for simple controller case. Just need to get the server running, then can test --- .gitignore | 1 + SwiftyCar/Package.swift | 5 +- .../Sources/SwiftyCar/MotorProvider.swift | 57 ++++++++++ SwiftyCar/Sources/SwiftyCar/PwmWrappers.swift | 104 ++++++++++++++++++ SwiftyCar/Sources/SwiftyCar/Vehicle.swift | 65 +++++++++++ SwiftyCar/Sources/SwiftyCar/main.swift | 3 +- .../Tests/SwiftyCarTests/SwiftyCarTests.swift | 2 +- SwiftyCar/build.gradle | 2 +- 8 files changed, 232 insertions(+), 7 deletions(-) create mode 100644 SwiftyCar/Sources/SwiftyCar/MotorProvider.swift create mode 100644 SwiftyCar/Sources/SwiftyCar/PwmWrappers.swift create mode 100644 SwiftyCar/Sources/SwiftyCar/Vehicle.swift diff --git a/.gitignore b/.gitignore index bd3e4d6..fa18d1a 100644 --- a/.gitignore +++ b/.gitignore @@ -28,6 +28,7 @@ SwiftyCar/Packages SwiftyCar/*.xcodeproj SwiftyCar/Sources/Generated SwiftyCar/xcuserdata/ +SwiftyCar/Sources/SwiftyCar/car xcuserdata/ .settings .project diff --git a/SwiftyCar/Package.swift b/SwiftyCar/Package.swift index 99a62f3..93417da 100644 --- a/SwiftyCar/Package.swift +++ b/SwiftyCar/Package.swift @@ -17,12 +17,9 @@ let package = Package( targets: [ // Targets are the basic building blocks of a package. A target can define a module or a test suite. // Targets can depend on other targets in this package, and on products in packages which this package depends on. - .target( - name: "Generated", - dependencies: [.product(name: "GRPC", package: "grpc-swift")]), .target( name: "SwiftyCar", - dependencies: ["Generated", "SwiftyGPIO"]), + dependencies: ["SwiftyGPIO", .product(name: "GRPC", package: "grpc-swift")]), .testTarget( name: "SwiftyCarTests", dependencies: ["SwiftyCar"]), diff --git a/SwiftyCar/Sources/SwiftyCar/MotorProvider.swift b/SwiftyCar/Sources/SwiftyCar/MotorProvider.swift new file mode 100644 index 0000000..fadc40a --- /dev/null +++ b/SwiftyCar/Sources/SwiftyCar/MotorProvider.swift @@ -0,0 +1,57 @@ +// +// MotorProvider.swift +// +// +// Created by Michael Pivato on 13/5/20. +// + +import Foundation +import GRPC +import NIO +import SwiftProtobuf + +class MotorProvider: MotorControl_CarControlProvider{ + private var vehicle: Vehicle2D + + init(vehicle: Vehicle2D){ + self.vehicle = vehicle + } + + func set_throttle(request: MotorControl_ThrottleRequest, context: StatusOnlyCallContext) -> EventLoopFuture { + self.vehicle.throttle = request.throttle + return context.eventLoop.makeSucceededFuture(.with{ + $0.throttleSet = true + }) + } + + func set_steering(request: MotorControl_SteeringRequest, context: StatusOnlyCallContext) -> EventLoopFuture { + self.vehicle.steering = request.steering + return context.eventLoop.makeSucceededFuture(.with{ + $0.steeringSet = true + }) + } + + func stream_vehicle_2d(context: UnaryResponseCallContext) -> EventLoopFuture<(StreamEvent) -> Void> { + return context.eventLoop.makeSucceededFuture({event in + switch event{ + case .message(let movement): + self.vehicle.throttle = movement.throttle.throttle + self.vehicle.steering = movement.steering.steering + case .end: + context.responsePromise.succeed(Google_Protobuf_Empty()) + } + + }) + } + + func record(request: MotorControl_RecordingReqeust, context: StatusOnlyCallContext) -> EventLoopFuture { + // TODO: Recording... + return context.eventLoop.makeSucceededFuture(Google_Protobuf_Empty()) + } + + func save_recorded_data(request: MotorControl_SaveRequest, context: StatusOnlyCallContext) -> EventLoopFuture { + // TODO Recording... + return context.eventLoop.makeSucceededFuture(Google_Protobuf_Empty()) + } +} + diff --git a/SwiftyCar/Sources/SwiftyCar/PwmWrappers.swift b/SwiftyCar/Sources/SwiftyCar/PwmWrappers.swift new file mode 100644 index 0000000..688351e --- /dev/null +++ b/SwiftyCar/Sources/SwiftyCar/PwmWrappers.swift @@ -0,0 +1,104 @@ +// +// PwmWrappers.swift +// +// Simple wrappers for PwmOutput I'll create as I go along, to match gpioZero's implementation I'm currently using. +// See gpiozero code here: https://github.com/gpiozero/gpiozero/blob/master/gpiozero/output_devices.py +// +// Created by Michael Pivato on 9/5/20. +// + +import Foundation +import SwiftyGPIO + +enum ServoError: Error{ + case invalidMinPulseWidth + case invalidMaxPulseWidth +} + +class Servo{ + private (set) var frameWidth : Float + private var minDc: Float + private var dcRange: Float + private var minValue: Float + private var valueRange: Float + private var internalValue: Float + + // TODO: Use a factory to provide the correct pwm rather than passing the pin in. + private var device: PWMOutput + private var currentPulseWIdth: Float + + var minPulseWidth: Float { + get{ + return self.minDc * self.frameWidth + } + } + + var maxPulseWidth: Float{ + get{ + return (self.dcRange * self.frameWidth) + self.minPulseWidth + } + } + + // TODO: Also store the pulse width (needs to be within this class) + // var pulseWidth: Float{ + // get{ + // return self.device//self.pwm_device.pin.state * self.frame_width + // } + // set (value){ + // + // } + // } + + var value: Float{ + get{ + internalValue + } + set(newValue){ + self.internalValue = newValue + self.device.startPWM(period: Int(self.frameWidth), + // Multiple by 100 to get into percentage. I don't like that... + duty: self.minDc + self.dcRange * ((internalValue - self.minValue) / self.valueRange) * 100) + } + } + + // TODO: Change this to nanoseconds to match PWMOutput + init?(forPin pin: PWMOutput, startingAt initialValue: Float = 0, withMinPulseWidth minPulseWidth: Float = 1000000, + withMaxPulseWidth maxPulseWidth: Float = 2000000, withFrameWidth frameWidth: Float = 20000000) throws { + if(minPulseWidth >= maxPulseWidth){ + throw ServoError.invalidMinPulseWidth + } + + if(maxPulseWidth >= frameWidth){ + throw ServoError.invalidMaxPulseWidth + } + self.frameWidth = frameWidth + self.minDc = minPulseWidth / frameWidth + self.dcRange = (maxPulseWidth - minPulseWidth) / frameWidth + self.minValue = -1 + self.valueRange = 2 + + self.currentPulseWIdth = 0 + + // Initialise pin immediately. + self.device = pin + self.device.initPWM() + self.internalValue = initialValue + self.device.startPWM(period: Int(self.frameWidth), duty: self.minDc + self.dcRange * ((internalValue - self.minValue) / self.valueRange) * 100) + } + + func min(){ + self.value = -1 + } + + func mid(){ + self.value = 0 + } + + func max() { + self.value = 1 + } + + func detach(){ + self.device.stopPWM() + } +} diff --git a/SwiftyCar/Sources/SwiftyCar/Vehicle.swift b/SwiftyCar/Sources/SwiftyCar/Vehicle.swift new file mode 100644 index 0000000..957bc15 --- /dev/null +++ b/SwiftyCar/Sources/SwiftyCar/Vehicle.swift @@ -0,0 +1,65 @@ +// +// Vehicle.swift +// +// +// Created by Michael Pivato on 8/5/20. +// + +import Foundation +import SwiftyGPIO + +protocol Vehicle{ + mutating func move2D(magnitude: Float, angle: Float) +} + +protocol Vehicle2D: Vehicle{ + var throttle: Float {get set} + var steering: Float {get set} +} + +class MockVehicle: Vehicle2D{ + var throttle: Float = 0 + var steering: Float = 0 + + func move2D(magnitude: Float, angle: Float) { + + } +} + +class RPiVehicle2D: Vehicle2D{ + public let pwmThrottle: Servo + public let pwmSteering: Servo + + var throttle: Float{ + get{ + return pwmThrottle.value + } + set(value){ + pwmThrottle.value = value + } + } + + var steering: Float{ + get{ + return pwmSteering.value + } + set(value){ + pwmSteering.value = value + } + } + + init(withThrottlePin: Servo, withSteeringPin: Servo){ + pwmThrottle = withThrottlePin + pwmSteering = withSteeringPin + } + + func move2D(magnitude: Float, angle: Float) { + + } + + func stop(){ + pwmThrottle.detach() + pwmSteering.detach() + } + +} diff --git a/SwiftyCar/Sources/SwiftyCar/main.swift b/SwiftyCar/Sources/SwiftyCar/main.swift index 115b18d..6572f74 100644 --- a/SwiftyCar/Sources/SwiftyCar/main.swift +++ b/SwiftyCar/Sources/SwiftyCar/main.swift @@ -6,5 +6,6 @@ // // Entry-Point to the Swift Car Controller -print("Hello World!") +var text = "Hello World!" +print(text) diff --git a/SwiftyCar/Tests/SwiftyCarTests/SwiftyCarTests.swift b/SwiftyCar/Tests/SwiftyCarTests/SwiftyCarTests.swift index 2d727ae..bebfbd1 100644 --- a/SwiftyCar/Tests/SwiftyCarTests/SwiftyCarTests.swift +++ b/SwiftyCar/Tests/SwiftyCarTests/SwiftyCarTests.swift @@ -6,7 +6,7 @@ final class SwiftyCarTests: XCTestCase { // This is an example of a functional test case. // Use XCTAssert and related functions to verify your tests produce the correct // results. - XCTAssertEqual(SwiftyCar().text, "Hello, World!") +// XCTAssertEqual(SwiftyCar().text, "Hello, World!") } static var allTests = [ diff --git a/SwiftyCar/build.gradle b/SwiftyCar/build.gradle index 158e34f..38b27e0 100644 --- a/SwiftyCar/build.gradle +++ b/SwiftyCar/build.gradle @@ -12,5 +12,5 @@ dependencies { task copySwiftCode(type: Copy, dependsOn: configurations.swift) { // Copy python protobuf code from proto project. from zipTree(configurations.swift.asPath) - into './Sources/Generated' + into './Sources/SwiftyCar' } \ No newline at end of file From 4ad8342033b8f911409996ad3fe118317853f4be Mon Sep 17 00:00:00 2001 From: Piv <18462828+Piv200@users.noreply.github.com> Date: Wed, 20 May 2020 19:07:11 +0930 Subject: [PATCH 5/6] Add initial grpc server implementation Still need to use a factory to get the correct vehicle, based on environment variables. --- SwiftyCar/Package.resolved | 4 +-- SwiftyCar/Package.swift | 4 +-- SwiftyCar/Sources/SwiftyCar/main.swift | 46 ++++++++++++++++++++++++-- 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/SwiftyCar/Package.resolved b/SwiftyCar/Package.resolved index d621cf8..aec55de 100644 --- a/SwiftyCar/Package.resolved +++ b/SwiftyCar/Package.resolved @@ -6,8 +6,8 @@ "repositoryURL": "https://github.com/grpc/grpc-swift.git", "state": { "branch": null, - "revision": "373ffd54c1c1a319d0ddac9476d13be9023584bb", - "version": "1.0.0-alpha.11" + "revision": "b83ee1ee2caa0660eb02444977b9b6e353c2adbf", + "version": "1.0.0-alpha.12" } }, { diff --git a/SwiftyCar/Package.swift b/SwiftyCar/Package.swift index 93417da..22eebe7 100644 --- a/SwiftyCar/Package.swift +++ b/SwiftyCar/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version:5.2 +// swift-tools-version:5.1 // The swift-tools-version declares the minimum version of Swift required to build this package. import PackageDescription @@ -11,7 +11,7 @@ let package = Package( dependencies: [ // Dependencies declare other packages that this package depends on. // .package(url: /* package url */, from: "1.0.0"), - .package(url: "https://github.com/grpc/grpc-swift.git", from: "1.0.0-alpha.11"), + .package(url: "https://github.com/grpc/grpc-swift.git", from: "1.0.0-alpha.12"), .package(url: "https://github.com/uraimo/SwiftyGPIO.git", from: "1.0.0"), ], targets: [ diff --git a/SwiftyCar/Sources/SwiftyCar/main.swift b/SwiftyCar/Sources/SwiftyCar/main.swift index 6572f74..116c98e 100644 --- a/SwiftyCar/Sources/SwiftyCar/main.swift +++ b/SwiftyCar/Sources/SwiftyCar/main.swift @@ -5,7 +5,47 @@ // Created by Michael Pivato on 8/5/20. // -// Entry-Point to the Swift Car Controller -var text = "Hello World!" -print(text) +import NIO +import SwiftyGPIO +import GRPC +func doServer() throws { + // Copied from examples + // Create an event loop group for the server to run on. + let group = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount) + defer { + try! group.syncShutdownGracefully() + } + + let pwms = SwiftyGPIO.hardwarePWMs(for:.RaspberryPi3)! + + // Read the feature database. + let vehicle = try RPiVehicle2D(withThrottlePin: Servo(forPin: (pwms[0]?[.P18])!)!, withSteeringPin:Servo(forPin: (pwms[1]?[.P19])!)!) + // Create a provider using the features we read. + let provider = MotorProvider(vehicle: vehicle) + + // Start the server and print its address once it has started. + let server = Server.insecure(group: group) + .withServiceProviders([provider]) + .bind(host: "localhost", port: 0) + + server.map { + $0.channel.localAddress + }.whenSuccess { address in + print("server started on port \(address!.port!)") + } + + // Wait on the server's `onClose` future to stop the program from exiting. + _ = try server.flatMap { + $0.onClose + }.wait() +} + +// Entry-Point to the Swift Car Controller +print("Starting Server") +do{ +try doServer() +} +catch{ + print("Server failed") +} From 59834d2afc6228f829c3f1b6209395fee19ae3ad Mon Sep 17 00:00:00 2001 From: Piv <18462828+Piv200@users.noreply.github.com> Date: Wed, 20 May 2020 19:32:01 +0930 Subject: [PATCH 6/6] Add factory to initialise vehicles --- .../Sources/SwiftyCar/VehicleFactory.swift | 27 +++++++++++++++++++ SwiftyCar/Sources/SwiftyCar/main.swift | 6 +---- 2 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 SwiftyCar/Sources/SwiftyCar/VehicleFactory.swift diff --git a/SwiftyCar/Sources/SwiftyCar/VehicleFactory.swift b/SwiftyCar/Sources/SwiftyCar/VehicleFactory.swift new file mode 100644 index 0000000..d1fcf0a --- /dev/null +++ b/SwiftyCar/Sources/SwiftyCar/VehicleFactory.swift @@ -0,0 +1,27 @@ +// +// File.swift +// +// +// Created by Michael Pivato on 20/5/20. +// + +import Foundation +import SwiftyGPIO + + + +func getVehicle2D() throws -> Vehicle2D { + if let value = ProcessInfo.processInfo.environment["CAR_VEHICLE"] { + switch value{ + case "CAR_2D": + // Get car for rpi. + let pwms = SwiftyGPIO.hardwarePWMs(for:.RaspberryPi3)! + + // Read the feature database. + return try RPiVehicle2D(withThrottlePin: Servo(forPin: (pwms[0]?[.P18])!)!, withSteeringPin:Servo(forPin: (pwms[1]?[.P19])!)!) + default: + return MockVehicle() + } + } + return MockVehicle() +} diff --git a/SwiftyCar/Sources/SwiftyCar/main.swift b/SwiftyCar/Sources/SwiftyCar/main.swift index 116c98e..7afc07c 100644 --- a/SwiftyCar/Sources/SwiftyCar/main.swift +++ b/SwiftyCar/Sources/SwiftyCar/main.swift @@ -6,7 +6,6 @@ // import NIO -import SwiftyGPIO import GRPC func doServer() throws { @@ -17,12 +16,9 @@ func doServer() throws { try! group.syncShutdownGracefully() } - let pwms = SwiftyGPIO.hardwarePWMs(for:.RaspberryPi3)! - // Read the feature database. - let vehicle = try RPiVehicle2D(withThrottlePin: Servo(forPin: (pwms[0]?[.P18])!)!, withSteeringPin:Servo(forPin: (pwms[1]?[.P19])!)!) // Create a provider using the features we read. - let provider = MotorProvider(vehicle: vehicle) + let provider = try MotorProvider(vehicle: getVehicle2D()) // Start the server and print its address once it has started. let server = Server.insecure(group: group)