1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
//
// Copyright (C) 2018 Kubos Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License")
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#![deny(missing_docs)]
#![deny(warnings)]

//! Kubos Service for interacting with an [Adcole Maryland Aerospace MAI-400](https://www.adcolemai.com/adacs)
//!
//! # Configuration
//!
//! The service can be configured in the `/etc/kubos-config.toml` with the following fields:
//!
//! ```toml
//! [mai400-service.addr]
//! ip = "127.0.0.1"
//! port = 8120
//! ```
//!
//! Where `ip` specifies the service's IP address, and `port` specifies the port which UDP requests should be sent to.
//!
//! # Starting the Service
//!
//! The service should be started automatically by its init script, but may also be started manually:
//!
//! ```shell
//! $ mai400-service
//! Kubos MAI-400 service started
//! Listening on: 10.63.1.20:8120
//! ```
//!
//! # Queries
//!
//! ## Ping
//!
//! Test query to verify service is running without attempting
//! to communicate with the underlying subsystem
//!
//! ```json
//! {
//!     ping: "pong"
//! }
//! ```
//!
//! ## ACK
//!
//! Get the last run mutation
//!
//! ```json
//! {
//!     ack: AckCommand
//! }
//! ```
//!
//! ## Errors
//!
//! Get all errors encountered since the last time this field was queried
//!
//! ```json
//! {
//!     errors: [String]
//! }
//! ```
//!
//! ## Power Status
//!
//! Get the current power state and uptime of the system
//!
//! ```json
//! {
//!     power {
//!         state: PowerState,
//!         uptime: Int
//!     }
//! }
//! ```
//!
//! ## Configuration
//!
//! Get the current configuration of the system
//!
//! ```json
//! {
//!     config: "Not Implemented"
//! }
//! ```
//!
//! ## Telemetry
//!
//! Get current telemetry information for the system
//!
//! ```json
//! {
//!     telemetry{
//!         nominal{
//!             gpsTime: i32,
//!             timeSubsec: i32,
//!             cmdValidCntr: i32,
//!             cmdInvalidCntr: i32,
//!             cmdInvalidChksumCntr: i32,
//!             lastCommand: i32,
//!             acsMode: i32,
//!             css: [i32; 6],
//!             eclipseFlag: i32,
//!             sunVecB: [i32; 3],
//!             iBFieldMeas: [i32; 3],
//!             bd: [f64; 3],
//!             rwsSpeedCmd: [i32; 3],
//!             rwsSpeedTach: [i32; 3],
//!             rwaTorqueCmd: [f64; 3],
//!             gcRwaTorqueCmd: [i32; 3],
//!             torqueCoilCmd: [f64; 3],
//!             gcTorqueCoilCmd: [i32; 3],
//!             qboCmd: [i32; 4],
//!             qboHat: [i32; 4],
//!             angleToGo: f64,
//!             qError: [i32; 4],
//!             omegaB: [f64; 3],
//!             nb: [i32; 3],
//!             neci: [i32; 3],
//!         },
//!         debug{
//!             irehs{
//!                 thermopilesA: [i32; 4],
//!                 thermopilesB: [i32; 4],
//!                 tempA: [i32; 4],
//!                 tempB: [i32; 4],
//!                 dipAngleA: i32,
//!                 dipAngleB: i32,
//!                 solutionDegraded: [Vec<String>; 8],
//!                 thermopileStructA{
//!                     dipAngle: i32,
//!                     earthLimb: {
//!                         adc: i32,
//!    		               temp: i32,
//!    		               errors: bool,
//!    		               flags: Vec<String>,
//!     	               },
//!                     earthRef: {...},
//!                     spaceRef: {...},
//!                     wideFov: {...},
//!                 thermopileStructB{...}
//!             },
//!             rawImu{
//!                 accel: [i32; 3],
//!                 gyro: [i32; 3],
//!                 gyroTemp: i32,
//!             },
//!             rotating{
//!                 bFieldIgrf: [f64; 3],
//!                 sunVecEph: [f64; 3],
//!                 scPosEci: [f64; 3],
//!                 scVelEci: [f64; 3],
//!                 keplerElem{
//!                     semiMajorAxis: f32,
//!                     eccentricity: f32,
//!                     inclination: f32,
//!                     raan: f32,
//!                     argParigee: f32,
//!                     trueAnomoly: f32,
//!                 },
//!                 kBdot: [f64; 3],
//!                 kp: [f64; 3],
//!                 kd: [f64; 3],
//!                 kUnload: [f64; 3],
//!                 cssBias: [i32; 6],
//!                 magBias: [i32; 3],
//!                 rwsVolt: i32,
//!                 rwsPress: i32,
//!                 attDetMode: i32,
//!                 rwsResetCntr: [i32; 3],
//!                 sunMagAligned: i32,
//!                 minorVersion: i32,
//!                 maiSn: i32,
//!                 orbitPropMode: i32,
//!                 acsOpMode: i32,
//!                 procResetCntr: i32,
//!                 majorVersion: i32,
//!                 adsOpMode: i32,
//!                 cssGain: [f64; 6],
//!                 magGain: [f64; 3],
//!                 orbitEpoch: i32,
//!                 trueAnomolyEpoch: f64,
//!                 orbitEpochNext: i32,
//!                 scPosEciEpoch: [f64; 3],
//!                 scVelEciEpoch: [f64; 3],
//!                 qbXWheelSpeed: i32,
//!                 qbXFilterGain: f64,
//!                 qbXDipoleGain: f64,
//!                 dipoleGain: [f64; 3],
//!                 wheelSpeedBias: [i32; 3],
//!                 cosSunMagAlignThresh: f64,
//!                 unloadAngThresh: f64,
//!                 qSat: f64,
//!                 rwaTrqMax: f64,
//!                 rwsMotorCurrent: [i32; 3],
//!                 rwsMotorTemp: i32,
//!             }
//!         }
//!     }
//! }
//! ```
//!
//! ## Test Results
//!
//! Get the test results of the last run test
//!
//! Note: For this service, this actually just fetches the nominal
//! and debug telemetry of the system, since there is no actual
//! built-in test
//!
//! ```json
//! {
//!     testResults{
//!         success,
//!         telemetryNominal{...},
//!         telemetryDebug{...}
//!     }
//! }
//! ```
//!
//! ## System Mode
//!
//! Get the current mode of the system
//!
//! ```json
//! {
//!     mode: Mode
//! }
//! ```
//!
//! ## System Orientation
//!
//! Get the last reported orientation of the system
//!
//! ```json
//! {
//!     orientation: "Not Implemented"
//! }
//! ```
//!
//! ## System Spin
//!
//! Get the last reported spin values of the system
//! Note: The spin values are automatically updated every six seconds
//!
//! ```json
//! {
//!     spin{
//!         x: f64,
//!         y: f64,
//!         z: f64
//!     }
//! }
//! ```
//!
//! # Mutations
//!
//! ## Errors
//!
//! Get all errors encountered while processing this GraphQL request
//!
//! Note: This will only return errors thrown by fields which have
//! already been processed, so it is recommended that this field be specified last.
//!
//! ```json
//! mutation {
//!     errors: [String]
//! }
//! ```
//!
//! ## No-Op
//!
//! Execute a trivial command against the system
//!
//! ```json
//! mutation {
//!     noop {
//!         errors: String,
//!         success: Boolean
//!    }
//! }
//! ```
//!
//! ## Set Power State
//!
//! Control the power state of the system
//!
//! - state: Power state the system should be changed to
//!   Note: The only valid input for this service is `RESET`
//!
//! ```json
//! mutation {
//!     controlPower(state: PowerState) {
//!         errors: String,
//!         success: Boolean,
//!         power: PowerState
//!     }
//! }
//! ```
//!
//! ## Configuration
//!
//! Configure the system
//!
//! ```json
//! mutation {
//!     configureHardware: "Not Implemented"
//! }
//! ```
//!
//! ## System Self-Test
//!
//! Run a system self-test
//!
//! - test: Type of self-test to perform
//!
//! ```json
//! mutation {
//!     testHardware(test: TestType) {
//!         ... on IntegrationTestRsults {
//!             errors: String,
//!             success: Boolean,
//!             telemetryNominal{...},
//!             telemetryDebug{...}
//!         }
//!         ... on HardwareTestResults {
//!             errors: "Not Implemented",
//!             success: true,
//!             data: Empty
//!         }
//!    }
//! }
//! ```
//!
//! ## Passthrough
//!
//! Pass a custom command through to the system
//!
//! - command: String containing the hex values to be sent (ex. "C3").
//!          It will be converted to a byte array before transfer.
//!
//! ```json
//! mutation {
//!     issueRawCommand(command: String) {
//!         errors: String,
//!         success: Boolean,
//!         response: String
//!     }
//! }
//! ```
//!
//! ## Set ADC Mode
//!
//! Set the attitude control mode
//!
//! - mode: Control mode to change to
//! - qbiCmd: Optional array of four values needed for Qinertial and Qtable mode
//! - sunAngleEnable: Optional. Specifies whether the sun rotating angle should be updated when
//!                 using Normal-Sun or LatLong-Sun mode
//! sunRotAngle: Optional. The sun rotating angle for use in Normal-Sun and LatLong-Sun mode
//!
//! ```json
//! mutation {
//!     setMode(mode: Mode, qbiCmd: Vec<i32>, sunAngleEnable: bool, sunRotAngle: f64) {
//!         errors: String,
//!         success: Boolean,
//!         response: String
//!     }
//! }
//! ```
//!
//! ## Set System Values
//!
//! Update system values
//!
//! - gpsTime: Optional. If specified, updates the system's ADACS clock
//! - rv: Optional. If specified, updates the orbital position and velocity at epoch.
//!     The argument has the following sub-fields:
//!         - eciPos: Vector containing the new X, Y, and Z ECI position values
//!         - eciVel: Vector containing the new X, Y, and Z ECI velocity values
//!         - timeEpoch: GPS time at which the eciPos and eciVel values will go into effect
//!
//! ```json
//! mutation {
//!     update(gps_time: Option<i32>,
//!         rv: Option<{eciPos: [f64; 3], eciVel: [f64; 3], timeEpoch: i32}>) {
//!         errors: String,
//!         success: Boolean,
//!     }
//! }
//! ```
//!

#![recursion_limit = "256"]

#[macro_use]
extern crate juniper;

mod model;
mod objects;
mod schema;
#[cfg(test)]
mod tests;

use crate::model::{ReadData, Subsystem};
pub use crate::objects::*;
use crate::schema::{MutationRoot, QueryRoot};
use kubos_service::{Config, Logger, Service};
use log::error;
use mai400_api::MAIResult;
use std::sync::Arc;

fn main() -> MAIResult<()> {
    Logger::init("mai400-service").unwrap();

    Service::new(
        Config::new("mai400-service")
            .map_err(|err| {
                error!("Failed to load service config: {:?}", err);
                err
            })
            .unwrap(),
        Subsystem::new("/dev/ttyS5", Arc::new(ReadData::new())).map_err(|err| {
            error!("Failed to initialize subsystem: {:?}", err);
            err
        })?,
        QueryRoot,
        MutationRoot,
    )
    .start();

    Ok(())
}