Flutter卫星轨道计算插件sgp4_sdp4的使用

发布于 1周前 作者 ionicwang 来自 Flutter

Flutter卫星轨道计算插件sgp4_sdp4的使用

开始使用

在寻找用于卫星跟踪的Dart语言包时,我发现市面上并没有合适的。因此,sgp4_sdp4插件诞生了!

sgp4_sdp4插件实现了NORAD算法,用于确定地球轨道上的卫星位置和速度。这些算法来源于1980年12月的NORAD文档《空间跟踪报告第3号》。插件中实现的轨道算法包括SGP4(适用于近地轨道)和SDP4(适用于深空轨道)。这些算法在卫星跟踪社区中广泛使用,并且当提供当前的NORAD两行元素数据时,能够产生非常准确的结果。

该插件包含完整的SGP4/SDP4算法源代码、各种支持类以及一个演示程序,展示了如何计算卫星的ECI位置以及从地面站点观测到的角度。

示例

以下是一个简单的使用示例:

import 'package:sgp4_sdp4/sgp4_sdp4.dart';

void main() {
  /// TLE示例。可以从https://www.celestrak.com/NORAD/elements/获取
  const String name = "NOAA 1";
  const String line1 =
      "1 04793U 70106A   21165.13556590 -.00000028  00000-0  10004-3 0  9995";
  const String line2 =
      "2 04793 101.6071 232.4155 0031175 318.8433  69.5245 12.53999256311423";

  /// 获取当前日期和时间
  final dateTime = DateTime.now();

  /// 解析TLE
  final TLE tleSGP4 = TLE(name, line1, line2);

  /// 创建一个轨道对象并打印是否为
  /// SGP4(适用于近地轨道)或SDP4(适用于深空轨道)
  final Orbit orbit = Orbit(tleSGP4);
  print("is SGP4: ${orbit.period() < 255 * 60}");

  /// 获取UTC时间的儒略日
  /// + 4/24 需要它,不同的时区(古巴-4小时)
  final double utcTime = Julian.fromFullDate(dateTime.year, dateTime.month,
              dateTime.day, dateTime.hour, dateTime.minute)
          .getDate() +
      4 / 24.0;

  final Eci eciPos =
      orbit.getPosition((utcTime - orbit.epoch().getDate()) * MIN_PER_DAY);

  /// 获取卫星的当前位置
  final CoordGeo coord = eciPos.toGeo();
  if (coord.lon > PI) coord.lon -= TWOPI;

  print("lat: ${rad2deg(coord.lat)}  lng: ${rad2deg(coord.lon)}");
}

更多关于Flutter卫星轨道计算插件sgp4_sdp4的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter卫星轨道计算插件sgp4_sdp4的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是一个关于如何在Flutter项目中使用sgp4_sdp4插件来计算卫星轨道的示例代码。这个插件可以用来根据卫星的TLE(两行轨道数据)进行轨道计算。

首先,确保你的Flutter项目中已经添加了sgp4_sdp4依赖。在pubspec.yaml文件中添加以下依赖:

dependencies:
  flutter:
    sdk: flutter
  sgp4_sdp4: ^x.y.z  # 请使用最新版本号替换x.y.z

然后运行flutter pub get来安装依赖。

接下来是一个示例代码,展示如何使用sgp4_sdp4插件来计算卫星的位置。

import 'package:flutter/material.dart';
import 'package:sgp4_sdp4/sgp4_sdp4.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Satellite Orbit Calculation'),
        ),
        body: Center(
          child: SatelliteOrbitCalculator(),
        ),
      ),
    );
  }
}

class SatelliteOrbitCalculator extends StatefulWidget {
  @override
  _SatelliteOrbitCalculatorState createState() => _SatelliteOrbitCalculatorState();
}

class _SatelliteOrbitCalculatorState extends State<SatelliteOrbitCalculator> {
  String tleLine1 = "1 25544U 98067A   21274.71792528  .00000132  00000-0  10000-3 0  9997";
  String tleLine2 = "2 25544  51.6416 247.4627 0006703 130.5360 357.0958 15.04395391103537";
  double julianDate = 2459600.5; // Example Julian Date

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        Text('TLE Line 1:'),
        TextField(
          text: tleLine1,
          onChanged: (value) => setState(() => tleLine1 = value),
          decoration: InputDecoration(border: OutlineInputBorder()),
        ),
        Text('TLE Line 2:'),
        TextField(
          text: tleLine2,
          onChanged: (value) => setState(() => tleLine2 = value),
          decoration: InputDecoration(border: OutlineInputBorder()),
        ),
        Text('Julian Date:'),
        TextField(
          keyboardType: TextInputType.numberWithOptions(signed: true, decimal: true),
          text: julianDate.toString(),
          onChanged: (value) => setState(() => julianDate = double.tryParse(value) ?? julianDate),
          decoration: InputDecoration(border: OutlineInputBorder()),
        ),
        Button(
          child: Text('Calculate Orbit'),
          onPressed: () {
            calculateOrbit();
          },
        ),
        Text('Satellite Position: ${_satellitePosition?.toString() ?? 'N/A'}'),
      ],
    );
  }

  SGP4? _propagator;
  Vector3? _satellitePosition;

  void calculateOrbit() {
    try {
      var tle = TLE.fromString(tleLine1, tleLine2);
      _propagator = SGP4.fromTLE(tle);
      var time = Time.fromJulianDate(julianDate);
      _satellitePosition = _propagator!.propagate(time);
      setState(() {});
    } catch (e) {
      print('Error calculating orbit: $e');
      _satellitePosition = null;
    }
  }
}

// Note: The above code assumes that the `sgp4_sdp4` package provides `TLE`, `SGP4`, `Time`, and `Vector3` classes.
// However, you might need to adapt the exact usage based on the actual API provided by the package.
// Please refer to the `sgp4_sdp4` documentation for the exact usage of its classes and methods.

注意事项

  1. 实际API:上面的代码假设sgp4_sdp4包提供了TLE, SGP4, Time, 和 Vector3等类。实际使用时,你需要根据该包的文档进行适当调整。
  2. 时间格式julianDate应该是一个合适的儒略日期。你需要确保输入的时间格式正确。
  3. 错误处理:在实际应用中,你需要添加更多的错误处理逻辑,以确保应用的健壮性。

确保你阅读并理解sgp4_sdp4的文档,因为具体的API可能会有所不同。

回到顶部