NOAA提取磁偏角数据

最近开发定位跟踪系统,计算需要用到磁偏角数据来校正磁北和真北方向,借鉴了px4源码中的利用计算航向调用磁偏角的方法,从NOAA Geomagnetic Field Calculator中去提取磁偏角。现简单记录下方法,由于地球磁场长期变化的,需要定时维护。

打开该网站后,选择“Magnetic Field Component Grid”计算和导出磁偏角。

Southern most lat&&Nourthern  most lat:纬度坐标设置在-60~60°之间,其他区域都在南北极了,一般人应该也用不到,可以看个人需求。

Western most long:&&Eastern most long:经度-180~180°之间,这没什么好说的,或者看个人需求。

Lat Step Size&&Lon Step Size:步进精度选择10°,看个人需求。

Elevation::海报高度选择GPS  0km,也可以看个人需求。

“Magnetic component”磁性元件,选择磁偏角“Declination”,如果有其他磁倾角、磁场等需求也可以选择其他项。

Model:选择IGRF模型或者WMM都可以。

Start Date&&End Date&&Step size 默认选择当天即可。最后计算我是选择csv导出格式,通过python导出 Declination_sv in Degree列数据索引数组,代码如下:

import csv


with open('C:\\Users\\yanfabu\\Desktop\\igrfgridData.csv', mode='r') as file:
    reader = csv.reader(file)
    data = list(reader)


filtered_data = []
for row in data:
    if row[0].startswith('#') or row[0].startswith('Year'):
        continue
    filtered_data.append(row)


declination_table = []


for row in filtered_data:
    try:
        lat = float(row[1])
        lon = float(row[2])
        declination = float(row[4])


        print(f"Lat: {lat}, Lon: {lon}, Declination: {declination}")


        if len(declination_table) == 0 or len(declination_table[-1]) == 37:
            declination_table.append([declination])
        else:
            declination_table[-1].append(declination)
    except ValueError:
        continue


print("static const float declination_table[13][37] = {")
for row in declination_table:
    print("    {" + ", ".join(map(str, row)) + "},")
print("};")

借鉴px4 “geo_mag_declination”查找表方法,获取到该坐标的磁偏角校准真北方向。借鉴代码如下:

get_lookup_table_index(float *val, float min, float max)
{
	/* for the rare case of hitting the bounds exactly
	 * the rounding logic wouldn't fit, so enforce it.
	 */

	/* limit to table bounds - required for maxima even when table spans full globe range */
	/* limit to (table bounds - 1) because bilinear interpolation requires checking (index + 1) */
	*val = constrain(*val, min, max - SAMPLING_RES);

	return static_cast<unsigned>((-(min) + *val) / SAMPLING_RES);
}

static float
get_table_data(float lat, float lon, const int8_t table[13][37])
{
	/*
	 * If the values exceed valid ranges, return zero as default
	 * as we have no way of knowing what the closest real value
	 * would be.
	 */
	if (lat < -90.0f || lat > 90.0f ||
	    lon < -180.0f || lon > 180.0f) {
		return 0.0f;
	}

	/* round down to nearest sampling resolution */
	float min_lat = floorf(lat / SAMPLING_RES) * SAMPLING_RES;
	float min_lon = floorf(lon / SAMPLING_RES) * SAMPLING_RES;

	/* find index of nearest low sampling point */
	unsigned min_lat_index = get_lookup_table_index(&min_lat, SAMPLING_MIN_LAT, SAMPLING_MAX_LAT);
	unsigned min_lon_index = get_lookup_table_index(&min_lon, SAMPLING_MIN_LON, SAMPLING_MAX_LON);

	const float data_sw = table[min_lat_index][min_lon_index];
	const float data_se = table[min_lat_index][min_lon_index + 1];
	const float data_ne = table[min_lat_index + 1][min_lon_index + 1];
	const float data_nw = table[min_lat_index + 1][min_lon_index];

	/* perform bilinear interpolation on the four grid corners */
	const float lat_scale = constrain((lat - min_lat) / SAMPLING_RES, 0.0f, 1.0f);
	const float lon_scale = constrain((lon - min_lon) / SAMPLING_RES, 0.0f, 1.0f);

	const float data_min = lon_scale * (data_se - data_sw) + data_sw;
	const float data_max = lon_scale * (data_ne - data_nw) + data_nw;

	return lat_scale * (data_max - data_min) + data_min;
}

float get_mag_declination(float lat, float lon)
{
	return get_table_data(lat, lon, declination_table);
}

### 单片机实现一键开关机电路设计方案 #### 设计概述 单片机实现的一键开关机电路通常由按键、MOS管、三极管以及单片机构成。其核心原理是利用按键触发信号,通过单片机控制外部元件的状态来完成设备的开/关操作[^3]。 #### 控制流程描述 当按下按键时,三极管 Q1 导通,使得单片机获得供电并复位进入工作状态。随后,单片机会检测按键输入 K-IN 是否处于低电平。如果是,则单片机输出端口 K-OUT 输出高电平,使另一只三极管 Q2 导通,从而模拟按键长按的效果。此时 LED 指示灯点亮,表示系统已启动完毕。 在运行过程中,如果再次按下按键超过一定时间(例如 1 秒),单片机将识别为关机指令,并将 K-OUT 设置为低电平,切断 Q2 的导通路径。LED 熄灭后,释放按键会使 Q1 截止,最终导致单片机失去供电而关闭。 以下是基于上述逻辑的一个典型硬件连接方式: ```plaintext +-------------------+ | | | PMOS (Q2) |-----> 板载电源供应 (+Vcc) | | +-------+-----------+ | v R_pullup | v +---------+ | G| | Single-|---> K_OUT (GPIO Pin of MCU) | Chip |---> K_IN (Input GPIO Pin with Pull-up Resistor) | S| +---------+ | v GND ``` 其中 `R_pullup` 是用于上拉 K-IN 输入引脚的电阻;PMOS 负责切换主电源供给线路。 #### 关于零功耗待机功能 为了达到真正的零功耗待机效果,在实际应用中可结合光耦隔离技术或者专用芯片进一步优化设计。比如采用 CD4013 构建双稳态触发电路作为辅助部分,能够有效减少静态电流消耗[^4]。 #### 示例代码片段 下面给出一段针对 PIC16F84A 微控制器编写的基础 C 语言伪码,展示如何判断按键动作及时序管理: ```c #include <pic.h> #define DELAY_TIME_MS 1000 // 定义延时时长为一秒 void main() { TRISB = 0b00000001; // 配置 RB0 为输入模式,其余皆设为输出. while(1){ if(PORTBbits.RB0 == 0){ // 如果检测到按钮被按下... delay_ms(DELAY_TIME_MS); // 延迟一段时间 if(PORTBbits.RB0 == 0 && !is_power_on()){ turn_on_system(); // 执行开机命令 }else if(is_long_press()){ turn_off_system(); // 否则执行关机命令 } } } } ``` 注意以上仅为示意性质的功能框架,具体实现需依据所选用型号调整寄存器配置及相关参数设置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值