[toc]

Autonomous Helicopter Flight via Reinforcement Learning

Paper download link

多平台维护不易,内容实时更新于 个人网站,请移步阅读最新内容。

Abstract

直升机飞行被普遍认为是一个具有挑战性的控制问题。作者在本文中描述了强化学习在自动直升机飞行控制器设计上的成功应用:本文首先拟合了一个随机、非线性的直升机动态模型,然后使用该模型去学习如何定点悬浮 (hover in place),以及如何飞出 RC 直升机竞赛的复杂行为 (maneuvers)。

In this paper, we describe the successful application of reinforcement learning to designing a controller for autonomous helicopter flight.

CM (Comment): 博客着重分析直升机模型建立,以及基于模型的控制策略学习。

INTRODUCTION

  • 直升机悬停的难度,旋翼顺时针转动,直升机底盘会逆时针旋转;引入尾部旋翼来克服底盘的转动,但同时带来了漂移 (drift) 的问题。所以,直升机悬停时,一般是机体向右倾斜。

    So, for a helicopter to hover in place, it must actually be tilted slightly to the right, so that the main rotor’s thrust is directed downwards and slightly to the left, to counteract this tendency to drift sideways.

  • 对于没有较多直升机控制知识的同学,建议先观看一个讲解直升机飞行原理和控制策略的科普视频,复习一下牛顿三大定律在直升机控制上的应用,否则后面的控制学习不容易理解。

  • 看完视频后,相比可以看到直升机设计精巧 (ingenious solutions),然而不直观的直升机动力学 (nonintuitive dynamics) 使得直升机的控制具有难度。

Autonomous Helicopter

硬件设备

  • Yamaha R-50 helicopter (长 3.6m,载重 20kg)
    • an Inertial Navigation System (INS)
    • a differential GPS system (a resolution of 2cm)
    • a digital compass
    • an onboard navigation computer

  • 最后,基于卡尔曼滤波 (the position estimates given by the Kalman filte),上述硬件以 50Hz 对外输出直升机的状态估计 (数字量):位置 (position),方向 (orientation),速度 (velocity) 和角速度 (angular velocities)。

基于上述视频容易理解,大多数直升机是通过 4 维的运动空间 (4-dimensional action space) 来实现飞行控制的:桨旋转平台的前后左右的倾斜 (tilting this plane either forwards/backwards or sideways),主螺旋桨角度的变化,机翼螺旋桨角度的变化。

  1. Cyclic pitch: The longtitudinal (front-back) and latitudinal (left-right) cyclic pitch controls. By tilting this plane (the helicopter’s rotors rotate) either forwards/backwards or sideways, these controls cause the helicopter to accelerate forward/backwards or sideways.
  1. Collective pitch: The (main rotor) collective pitch control. By varying the tilt angle ofthe rotor blades, the collective pitch control affects the main rotor’s thrust.
  1. Yaw motion: The tail rotor collective pitch control. Using a mechanism similar to the main rotor collective pitch control, this controls the tail rotor’s thrust.

综上,本文的任务就是基于卡尔曼滤波的位置估计,周期 (50Hz) 挑选好的控制行为 (pick good control actions)。

Using the position estimates given by the Kalman filter, our task is to pick good control actions every 50th of a second.

Model identification

Fit the helicopter’s dynamics model

  • 数据输入:记录人类驾驶员操作直升机的数据 (12-dimensional helicopter state & 4-dimensional helicopter control inputs),使用这些飞行数据进行模型拟合 (model fittin)。

    We began by asking a human pilot to fly the helicopter for several minutes, and recorded the 12-dimensional helicopter state and 4-dimensional helicopter control inputs as it was flown.

  • 使用机体坐标系 (helicopter body coordinates),可以减少参数辨识量。

    in which the x, y, and z axes are forwards, sideways, and down relative to the current position of the helicopter.

    Our model is identified in the body coordinates, which has four fewer variables than the spatial (world) coordinates.

Locally weighted linear regression (局部加权线性回归)

  • 局部加权线性回归知识

  • s(t+1) = f(s(t), a(t), noise)

    By applying locally-weighted regression with the state s(t) and action a(t) as inputs, and the one-step differences of each of the state variables in turn as the target output, this gives us a non-linear, stochastic, model ofthe dynamics, allowing us to predict s(t+1) as a function of s(t) and a(t) plus noise.

  • 此外,和直升机的先验知识结合,提出优化模型的一些策略方法,减少拟合的参数量。

    Similar to the use of body coordinates to exploit symmetries, there is other prior knowledge that can be incorporated.

    Similar reasoning allows us to conclude that certain other parameters should be 0, 1/50(50Hz) or g(gravity), and these were also hard-coded into the model.

  • 加入三个不可观测的参数 (unobserved variables) 来描述模型在控制上的延迟 (model latencies)。

    Finally, we added three extra (unobserved) variables to model latencies in the responses to the controls.

  • 通过图片 (plots) 将长时间段的均方差显示出来的交互方式,更容易看出差异和比较来选择模型,观察模型在长时间段内的拟合准确程度。

    Our main tool for choosing among the models was plots.

    查看在一段时间内 (at longer time scales) 的预测位置 (estimated position) 和真实位置 (true position) 的均方差 (mean-squared error)。

    For a model, the mean-squared error (as measured on test data) between the helicopter’s true position and the estimated position at a certain time in the future.

  • 建立好模型的意义,增强后期在真实环境下的可靠性。

    We wanted to verify the fitted model carefully, so as to be reasonably confident that a controller tested successfully in simulation will also be safe in real life.

    对没有建模的噪声输入,担心噪声超出模型的预测。为次,通过图片检查 (plots) 直升机的预测输出是否超出误差阈值。

    One of our concerns was the possibility that unmodeled correlations in noise might mean the noise variance of the actual dynamics is much larger than predicted by the model.

Reinforcement learning: The PEGASUS algorithm

对 PEGASUS 的描述

  • 这些使用程序很难精确计算,但是可以借助于计算机来仿真 MDP 的动力学。

Learning to Hover

  • 以其他控制器尝试为例 (μ-synthesis),来说明问题的难度和微妙

    These comments should not be taken as conclusive of the viability of any of these methods; rather, we take them to be indicative of the difficulty and subtlety involved in learning a helicopter controller.

  • 从直升机悬停开始学习策略。

    We began by learning a policy for hovering in place.

    • 直升机悬停控制的神经网络设计

      The picture inside the circles indicate whether a node outputs the sum of their inputs, or the tanh of the sum of their inputs. Each edge with an arrow in the picture denotes a tunable parameter. The solid lines show the hovering policy class. The dashed lines show the extra weights added for trajectory following.

      • 很明显,需要具备直升机控制的理论和实践知识。

      Each of the edges in the figure represents a weight, and the connections were chosen via simple reasoning about which control channel should be used to control which state variables.

  • 结合具体的问题和目标,设计奖励函数和惩罚函数,这是需要除计算机知识和强化学习知识外的专业知识和实践经验。

    • 二阶奖励函数 (靠近目标点 + 速度变化慢)

      This encourages the helicopter to hover near target position, while also keeping the velocity small and not making abrupt movements.

    • 二阶惩罚函数 (鼓励平滑控制和小行动)

      To encourage small actions and smooth control of the helicopter, we also used a quadratic penalty for actions.

  • 基于辨识的模型 (model identified),使用 PEGASUS 算法来获得实用策略 (the utilities of policies) 的近似值 $[\hat {U}(\pi)]$。

    Using the model identified, we can now apply PEGASUS to define approximations $[\hat{U}(\pi)]$ to the utilities of policies.

  • 策略和动态特性的可连续和平滑性。

    Since policies are smoothly parameterized in the weights, and the dynamics are themselves continuous in the actions, the estimates of utilities are also continuous in the weights.

  • 求取策略的权重时,应用爬山算法来最大化 $[\hat {U}(\pi)]$,其中使用梯度上升 (gradient ascent algorithm) 和随机游走 (random-walk algorithm) 两种算法,来最大化策略 $[\pi]$。

    We may thus apply standard hillclimbing algorithms to maximize $[\hat{U}(\pi)]$ in terms of the policy’s weights.

  • 在策略学习中,最消耗资源的是重复加速蒙特卡罗评价 (Monte Carlo evaluation) 来获得策略,文章提出使用并行计算 (parallelized our implementation) 来加速蒙特卡罗评价的训练。

    • 在不同的计算机使用不同的样本来计算;
    • 然后聚合训练结果,获得策略 $[\hat {U}(\pi)]$。

    The most expensive step in policy search was the repeated Monte Carlo evaluation to obtain $[\hat{U}(\pi)]$. To speed this up, we parallelized our implementation, and Monte Carlo evaluations using different samples were run on different computers, and the results were then aggregated to obtain $[\hat{U}(\pi)]$.

  • 如何评价控制策略的表现:文章比较基于学习的策略和人类驾驶员的性能表现,来证明算法的稳定性。

    We also compare the performance of our learned policy against that of our human pilot trained and licensed by Yamaha to fly the R-50 helicopter.

Flying competition maneuvers

  • 作者参加了 AMA (the Academy of Model Aeronautics) 组织的直升机飞行挑战 (an annual RC helicopter competition),挑战极具难度的 3 个行为,比如如下图所示的 pirouette (turning in place)。

    We took the first three maneuvers from the most challenging, Class III, segment of their competition.

  • 文章目前只是学习了如何悬停 (hover),那么如何能够沿着轨迹飞行呢?作者给出的一种做法是让直升机沿着特定的轨迹点,慢慢变化状态,控制策略和悬停基本一致。

    How does one design a controller for flying trajectories? Given a controller for keeping a system’s state at a point, one standard way to make the system move through a particular trajectory is to slowly vary along a sequence of set points on that trajectory.

  • 另一种做法,重新训练参数来准确跟踪轨迹

    Retrain the policy’s parameters for accurate trajectory following.

    • trajectory following vs trajectory tracking

      Path/Trajectory following is all about following a predefined path which does not involve time as a constraint. Thus, if you are on the path and following it with whatever speed you have reached your goal. On the contrary, trajectory tracking involves time as a constraint. Meaning that you have to be at a certain point at a certain time.

    • 基于直升机的理论机制,优化网络模型。

      Since we are now flying trajectories and not only hovering, we also augmented the policy class to take into account more of the coupling between the helicopter’s different sub-dynamics. / 比如旋转时,尾翼控制会带来漂移;上升下降控制时,带来旋转的影响,这一部分在最开始的视频中有讲到。

    • 设计 (specify) 轨迹跟踪 (trajectory following) 时的奖励函数。

      • 贴近轨迹 (penalize deviation):惩罚位置的偏差,计算直升机当前位置和其投影到理想轨迹的位置偏差。

        “tracked” position: the “projection” of the helicopter’s position onto the path of the idealized, desired trajectory.

        the learning algorithm pays a penalty that is quadratic between the actual position and the “tracked” position on the idealized trajectory.

      • 前进奖励 (making progress):使用沿着轨迹增加的势函数 (potential function),如果直升机前进,那么可以获得正奖励 (positive reward)。

        Since, we are already tracking where along the desired trajectory the helicopter is, we chose a potential function that increases along the trajectory. Thus, whenever the helicopter’s makes forward progress along this trajectory, it receives positive reward.

      • 定义解耦 (decouple our definition)

        Finally, our modifications have decoupled our definition of the reward function from (x*, y*, z*, w*) and the evolution of (x*, y*, z*, w*) in time.

        • (x*, y*, z*, w*) represents a desired hovering position and orientation.

        We considered several alternatives, but the main one used ended up being a modification for flying trajectories that have both a vertical and a horizontal component (such as along the two upper edges of the triangle in III.1).

      • 模型控制需要根据实际测试,人为干预,不断调整,而不是一劳永逸,比如在 z 方向上人为加入时延,来避免 “bowed-out” 或者 “bowed-in” 轨迹。

        the z (vertical)-response of the helicopter is very fast. In contrast, the x and y responses are much slower.

References

挺喜欢这篇文章的几个非理财观点,文章转载于 实用的建议。

通过阅读《小狗钱钱》这本书,我们除了可以收获很多金钱、理财相关的观念和知识外,还可以获得很多实用的建议,我认为将这些实践应用于日常生活中,可以帮助我们进步、达成自己的目标。

本书关于金钱、理财的观点和建议,大家可以看我的另一篇读后感《小狗钱钱》读后感 - 关于金钱。

实用的建议

你只有两个选择:做与不做,没有试试看

本书关于做一件事情的理念:你只有两个选择:做与不做,没有试试看。

当小狗钱钱给吉娅提出梦想储钱罐和梦想相册时,吉娅说自己会试试看,小狗钱钱对吉娅说:

你只有两个选择:做或不做,不能是我试一下”

如果你只是抱着试试看的心态,那么你只会以失败告终,你会一事无成。尝试纯粹是一种借口,你还没有做,就已经给自己想好了退路。

反思一下我们自己,在做一件事情时,我们是不是要么犹豫半天后选择不做,要么是不是会说 “我尝试一下吧”,其实那时候内心已经允许自己完不成了,甚至已经接受了失败这个结局。为了避免这样的事情发生,我们可以像吉娅一样,虽然没有监督自己的狗狗,但是可以自己监督自己,一旦说 “尝试一下” 就在本子上记录一次,最后争取只留下 “做” 与 “不做” 两个选项。

多想实现目标后的美好,少想事情做不好的原因

钱钱给吉娅提出她看待问题的一个错误思维:首先考虑的总是事情做不成的原因,而不是去想象达成目标后的美好。

你必须设想你已经拥有了这些东西,这样捏的一个小愿望才会变成一种强烈的愿望,你想象的越多,你的愿望就越强烈,那么你就会开始寻找机会来实现自己的梦想。

基于此,钱钱给她提出的可实践性建议是建立梦想相册,让她把梦想 “去美国交流”、“笔记本电脑” 相关的图片保存到相册中,时常拿出来看,加强自己的渴望。

关于梦想储钱罐和梦想相册,我自己也进行了实践了一下,我找出本子罗列自己十个需要金钱来实现的愿望,挑选出了三个,并设定了各自的资金来源,刚好咕咕机到手了,所以打算把与愿望相关的图片都打印出来,而且还要把梦想写的更详细一点,等我达成某个梦想时,再来与大家分享。

自信决定了是否可以成功

本书还强调了自信的重要性,而且认为过于自信比不够自信要好得多。

你的自信程度决定了你是否相信自己的能力,是否相信你自己,如果你根本不相信你能做到的话,那么你就根本不会动手去做,而假如你不开始去做,那么你就什么也得不到。

这段话我深感理解,我是一个自卑的人,任何时候都不自信,而且由于没有信心做成功,自然也不会付出相应的努力,所以很多事情不出所料地都失败了,而这些事情的失败像是地上的雪花,自卑这个雪球经过一片片雪花逐渐变的越来越大,之后又更容易沾上雪花。自卑、不为之努力、失败形成了一个完美的循环,自己无法逃脱。

关于如何建立自信,钱钱给吉娅的建议是:设立自己的 “成功日记”,每天记录至少 5 件个人成果,任何小事都可以,而且不用纠结有些事情是否属于成果,可以给予自己肯定的答复。这个建议之前也有在别的地方听到过,但是都没有付诸实践,这次我计划开始写 “成功日记”,到目前为止已经坚持了 17 天,未有过间断,我发现自己喜欢上了这项活动。虽然里面记录的大部分搜是很小的事情,现在我每天晚上睡觉前拿出我的小本本回想今天的个人成果,感觉很开心,这对自己至少是一种积极的暗示,而且我发现开始更愿意完成自己每天的任务计划,不知道是不是有它的功劳呢。

把精力集中在你知道的、能做的和拥有的东西上

当吉娅决定要开始赚钱,但是不知道如何下手时,钱钱给她讲了个故事,借故事提出两个建议:

  1. 为别人解决一个难题,那么你就能赚到很多钱;
  2. 把精力集中在你知道的、能做的和拥有的东西上。

如果你也有 “开源” 的打算,我感觉这两个建议非常好,考虑下别人需要什么和自己有什么。而第二个建议可能不仅限于赚钱,自己的事业或工作也一样,人的精力是有限的,专注于自己知道、能做和拥有的东西上,不要跟随潮流看大家干嘛自己也干嘛,最终可能什么都不精,找到自己会的,把某一方面做的很精通,可能整体带来的收益更大。

重要的事情要坚持做

由于小狗钱钱是吉娅捡到的,当吉娅找到狗主人时,由于害怕失去钱钱而十分害怕和烦恼,因此停止了成功日记的记录和梦想相册的整理。钱钱因此非常生气,它说:

困难总是在不断地出现。尽管如此,你每天还是要不间断地去做对你地未来意义重大地事情,这件事情可能花费时间不多,但就是这段时间会让一切变得不同。

这个有点观点比较像四现象法则中提出的对待 “重要不紧急” 事件的态度,这些事情可能不是十分紧急的,但是这些事情的重要性却很高,无论如何都应该坚持,那么有什么好的建议吗?钱钱给的建议是,应该在固定的时间里,有规律地做这些事情,这样可以防止由于其他紧急事情、或者心情等原因导致的中断。

72 小时规定

钱钱告诉吉娅在想做什么事情时,在 72 小时内去完成,不要拖太久时间,否则这件事情你可能永远不会再做了。

这个规定很适合于我们平常一些灵感和想法的实施,这些事情完全属于自主自愿,更多的是靠一时的冲动和兴趣,如果开始的太晚,或者持续时间太久,很容易中途放弃,最终就不了了之。

写在最后

关于实用的建议,我大概就梳理出了这么几个,希望大家可以找到适合自己的建议并进行实践,我实践一段时间后,也会和大家分享我的成长和收获。

[toc]

前言

FYI 本博客初稿完成于 2017 年,内容更新于 个人网站 - 简明版墨卡托投影坐标系,请移步阅读最新内容。

本文讲述了墨卡托投影坐标系的基本原理和实现,但是因为地球非标准椭圆,经纬度和米坐标的转换复杂,本文提供的算法存在较大误差,仅适用于初步验证。

墨卡托投影坐标系

墨卡托投影 (Mercator Projection) 是一种 “等角正切圆柱投影”,荷兰地图学家墨卡托 (Mercator) 在 1569 年拟定:假设地球被围在一个中空的圆柱里,其赤道与圆柱相接触,然后再假想地球中心有一盏灯,把球面上的图形投影到圆柱体上,再把圆柱体展开,这就是一幅标准纬线为零度(即赤道)的 “墨卡托投影” 绘制出的世界地图。
墨卡托投影在今天对于航海事业起着极为重要的作用,目前世界各国绘制海洋地图时仍广泛使用墨卡托投影,国际水路局 (IHB) 规定:“除特殊情况外,各国都要用墨卡托投影绘制海图”。国际水路局发行的《大洋水深总图》是把全世界分成 24 幅编辑的,在南北纬 72 度之间就是使用墨卡托投影绘成的。

墨卡托投影性质

由于墨卡托投影的经纬线离开赤道逐渐以相同倍数伸长,所以又称为渐长投影,由于它是具有等角性质的圆筒投影,所以也叫做等角圆筒投影。注意:这种投影不适合高纬地区,通常纬度 60 度以上区域,不用此投影。

墨卡托投影有一个特别的特性:所有罗盘等角线,或称斜航线(就是与所经过的所有经线形成相同角度的航线,也称恒向航线)在墨卡托投影下都是直线。这使得在航海领域这个投影非常重要。
注意:经纬线的伸长与纬线的正割成比例变化,随纬度增高极具拉伸,到极点成为无穷大;面积的扩大更为明显,在 60 度的地方面积要扩大四倍。如下图所示,地理上等半径圆在高纬度面积明显扩大。

墨卡托投影是按等角条件修改透视圆筒投影而得到的投影,等角(也称为保形) 是指当地图上任何一点的各方向具有相同的比例,称为局部保形,透视圆筒投影如图 1 所示。从墨卡托投影图上可以看出,经线间隔的经度如果相等,则经线是等距平行的直线, 纬线也是平行的直线,而且经纬线是相互垂直的。墨卡托投影对透视圆筒投影改造点:要使圆筒投影称为等角的性质,必须使由赤道向两极经线逐渐伸长的倍数与经线上各点相应的纬度扩大的倍数相同。

透视圆筒投影

墨卡托投影方程式

墨卡托投影以整个世界范围,赤道作为标准纬线,本初子午线作为中央经线,两者交点为坐标原点,向东向北为正,向西向南为负。南北极在地图的正下、上方,而东西方向处于地图的正右、左。由于墨卡托投影在两极附近是趋于无限值,因此它并没完整展现了整个世界,地图上最高纬度是 85.05 度(通过纬度取值范围 ys 反解计算可得到纬度值为 85.05112877980659)。为了简化计算,我们采用球形映射,而不是椭球体形状。

公式推导具体见文献 墨卡托投影与大圆投影的构成及其在定航线计算航程与航向方面的应用_程光举。


利用等角条件 m=n 来讨论具体公式,具体分为三步:

  1. 根据 m=n 得到地球表面投影到平面上的微积线段的关系式。
  2. 把地球视为球体:
    设地球表面 A 点经纬坐标为(λ,Φ),对应的投影坐标为(x,y), 基准纬线设置为赤道,则 R 为地球半径;

墨卡托投影方程式为:

  1. 把地球视为旋转椭球体

墨卡托投影正反解公式:

公式推导具体见文献 墨卡托投影与大圆投影的构成及其在定航线计算航程与航向方面的应用_程光举。

程序实现

最新版见 Geography Coordinate Transform Lite/Mercator

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
// Lite版,仅适用于初步验证
//把地球视为球体实现经纬度和墨卡托投影的函数
typedef struct Point
{
double x;
double y;
}WayPoint;

//经纬度转墨卡托
WayPoint lonLat2Mercator(WayPoint lonLat)
{
WayPoint mercator;
double x = lonLat.x * 20037508.34 / 180;
double y = log(tan((90 + lonLat.y) * Pi / 360))/(Pi / 180);
y = y * 20037508.34 / 180;
mercator.x = x;
mercator.y = y;
return mercator;
}

//墨卡托转经纬度
WayPoint Mercator2lonLat(WayPoint mercator)
{
WayPoint lonLat;
double x = mercator.x / 20037508.34 * 180;
double y = mercator.y / 20037508.34 * 180;
y = 180 / Pi * (2 * atan(exp(y * Pi / 180)) - Pi / 2);
lonLat.x = x;
lonLat.y = y;
return lonLat;
}

Reference

[toc]

最近在研究动态障碍物避障算法,在 Python 语言进行算法仿真时需要实时显示障碍物和运动物的当前位置和轨迹,利用 Anaconda 的 Python 打包集合,在 Spyder 中使用 Python3.5 语言和 matplotlib 实现路径的动态显示和交互式绘图 (和 Matlab 功能类似)。

本博客初稿完成于 2017 年,多平台维护不易,内容更新于 个人网站,请移步阅读最新内容。

背景知识

Anaconda

Anaconda 是一个用于科学计算的 Python 发行版,支持 Linux, Mac, Windows 系统,提供了包管理与环境管理的功能,可以很方便地解决多版本 python 并存、切换以及各种第三方包安装问题。

Anaconda 利用工具 / 命令 conda 来进行 package 和 environment 的管理,并且已经包含了 Python 和相关的配套工具。

matplotlib

matplotlib 是 python 最著名的绘图库,它提供了一整套和 matlab 相似的命令 API,十分适合交互式地进行制图。而且也可以方便地将它作为绘图控件,嵌入 GUI 应用程序中。其中,matplotlib 的 pyplot 子库提供了和 matlab 类似的绘图 API,方便用户快速绘制 2D 图表,它的文档相当完备,并且 Gallery 页面中有上百幅缩略图,打开之后都有源程序。

matplotlib 交互式绘图

原理

在调研 matplotlib 动态绘制曲线方法中,和 matlab 相似有 animation 方法和交互式绘图,但是 animation 方法灵活性不高,不太适合路径的实时动态显示,本文最后采用交互式绘图模(interactive mode)-- Using matplotlib in a python shell。

The interactive property of the pyplot interface controls whether a figure canvas is drawn on every pyplot command. If interactive is False, then the figure state is updated on every plot command, but will only be drawn on explicit calls to draw(). When interactive is True, then every pyplot command triggers a draw.

实践

当绘图语句中加入 pl.ion () 时,表示打开了交互模式。此时 python 解释器解释完所有命令后,给你出张图,但不会结束会话,而是等着你跟他交流交流。如果你继续往代码中加入语句,run 之后,你会实时看到图形的改变。当绘图语句中加入 pl.ioff () 时或不添加 pl.ion () 时,表示打关了交互模式。此时要在代码末尾加入 pl.show () 才能显示图片。python 解释器解释完所有命令后,给你出张图,同时结束会话。如果你继续往代码中加入语句,再不会起作用,除非你关闭当前图片,重新 run。

Example

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
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 25 23:28:29 2017
@author: wyl
original link: https://www.yanlongwang.net/Python/python-interactive-mode/
"""

import matplotlib.pyplot as plt
from matplotlib.patches import Circle
import numpy as np
import math

plt.close() #clf() # 清图 cla() # 清坐标轴 close() # 关窗口
fig=plt.figure()
ax=fig.add_subplot(1,1,1)
ax.axis("equal") #设置图像显示的时候XY轴比例
plt.grid(True) #添加网格
plt.ion() #interactive mode on
IniObsX=0000
IniObsY=4000
IniObsAngle=135
IniObsSpeed=10*math.sqrt(2) #米/秒
print('开始仿真')
try:
for t in range(180):
#障碍物船只轨迹
obsX=IniObsX+IniObsSpeed*math.sin(IniObsAngle/180*math.pi)*t
obsY=IniObsY+IniObsSpeed*math.cos(IniObsAngle/180*math.pi)*t
ax.scatter(obsX,obsY,c='b',marker='.') #散点图
#ax.lines.pop(1) 删除轨迹
#下面的图,两船的距离
plt.pause(0.001)
except Exception as err:
print(err)

[toc]

FYI 本博客实时更新于 个人网站 - Hexo 网站引入 google AdSense,请移步阅读最新内容。

前言

个人网站 www.yanlongwang.net 已经运营近一年,每日的浏览量不断上升,现在维持在两位数,打算承接一点广告赚睡后收入,用来维持网站的日常运营,希望能覆盖网站的服务器和域名开销。

目前,自己通过 Google AdSense 在个人网站的广告位承接广告,下面主要讲解从注册 Google AdSense,到审核通过后设置广告的基本操作。

点击 www.yanlongwang.net 测试体验,如下图。在侧边栏下部应该可以看到 AdSense 推送的广告,如果看不到,查看浏览器是否安装了 AdBlock 等广告屏蔽插件,请暂时关闭这些插件。

1
2
3
4
5
6
// 系统
- Next Theme
// 在 hexo 目录下, 输入 “hexo version” 可以查看 Next 及依赖的版本
- hexo: 3.8.0
- hexo-cli: 1.1.0
- ...

广告联盟选择

在国内,比较知名的广告联盟都要求网站备案,比如百度广告联盟,否则无法承接广告,而自己的小网站暂时没有备案,所以只能选择国外的广告联盟。

国外的广告联盟,毫无疑问会选择 Google AdSense,申请门槛相对较低。网站一般运营几个月,有十几篇文章,是比较容易通过审核的。并且,AdSense 的广告相比百度联盟的还是比较优质的,所以最后选择 google AdSense 联盟。

注册 AdSense 账号

  • 注册网站 搬梯子登录
  • 点击右上角 Get started 进行注册;如有 google 账号,可以直接点击 sign in 进行登录。

  • 填写完信息之后,页面会生成一段代码,要求你放置到你的网站的标记中。注意,当审核通过后,这一段代码在开启 “自动广告” 后,会自动推送广告。
    • 针对 Next Theme,可以复制代码到 themes\next\layout\_partials\head.swig 中任意一个 script 块下。
1
2
3
4
5
6
7
8
9
10
<!-- layout example in head.swig -->
{% if theme.google_site_verification %}
<meta name="google-site-verification" content="{{ theme.google_site_verification }}" />
{% endif %}

<script data-ad-client="ca-pub-612**56" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>

{% if theme.bing_site_verification %}
<meta name="msvalidate.01" content="{{ theme.bing_site_verification }}" />
{% endif %}
  • 在你添加完成之后,在 google AdSense 上点击确认,谷歌会到你的网站上进行核查和验收。
  • 博主是 2020.02.05 上午申请,2020.02.06 上午收到申请通过的邮件。

配置广告位

在收到审核通过的邮件后,可以登录 google AdSense 在自己的网站上进行广告位置的筛选和设计了,目前 google AdSense 主要提供了自动广告和广告单元两种形式的广告添加方式。

自动广告

自动广告是 google AdSense 近来提供的一种广告形式,它能够通过分析你的博客布局结构,自定义的在你的网站中插入合适的广告,无论是内容,还是广告尺寸,都是完全契合网站内容本身的,算是一种比较高质量的广告。

打开 AdSense 首页,然后转到广告。您可以在概览中为各个网站设置自动广告。

如果您的网站已经启用自动广告,在审核通过的几个小时内,您便会在网站上看到相关的广告,并开始累积收入 (之前插入的检验代码,接入了 google AdSense 的自动广告)。

这种广告投放的几率比较小,在 PC 端效率比较低,如果你的网站支持移动端查看的话,会自动投放移动端自适应的广告。

广告单元

为了能够最高效的利用自己博客的广告位,AdSense 提供了三种固定广告位。

  • 文字广告和展示广告 (即侧边栏,评论区之类的固定广告位)
  • 信息流广告 (插入在信息流内容的广告位置)
  • 文章内嵌广告 (主要是插入在每篇文章内部的开始,中间,结尾部分,展示次数比较多,强烈推荐)

由于本人的是博客网站,所以第二种信息流广告没有投入使用,第三种影响阅读体验,所以博主现在主要采用第一种方式,在侧边栏展示广告。

具体的操作流程是,在网站上,选择广告单元 -> 新建广告位 -> 选择对应的广告类型 -> 生成对应的广告代码。

广告单元插入

博主启用了自动广告,广告单元仅使用了第一种方式,在网站的侧边栏和评论区展示广告,但下面会提供几种针对 Hexo 的 Next Theme 广告单元代码位置的插入。

插入评论区

将广告单元生成的广告代码插入 \themes\next\layout_partials\comments.swig 中的末尾即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  {% elseif theme.valine.appid and theme.valine.appkey %}
<div class="comments" id="comments">
</div>
{% endif %}

<!-- Insert google ad blocks -->
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- Comment_Below -->
<ins class="adsbygoogle"
style="display:block"
data-ad-client="ca-pub-61***56"
data-ad-slot="83***73"
data-ad-format="auto"
data-full-width-responsive="true"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

{% endif %}

插入侧边栏

将代码插入 \themes\next\layout\_macro\sidebar.swig 文件中

的最下侧。

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
      {% if theme.sidebar.b2t %}
<div class="back-to-top">
<i class="fa fa-arrow-up"></i>
{% if theme.sidebar.scrollpercent %}
<span id="scrollpercent"><span>0</span>%</span>
{% endif %}
</div>
{% endif %}

<!-- Insert google ad blocks -->
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- Comment_Below -->
<ins class="adsbygoogle"
style="display:block"
data-ad-client="ca-pub-6129496365361356"
data-ad-slot="8323610673"
data-ad-format="auto"
data-full-width-responsive="true"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

</div>
</aside>
{% endmacro %}

插入文章头部

在 \themes\next\layout\_custom\ 目录下,新建 google_adsense.swig,并将 AdSense 提供的广告代码放入其中,然后将

1
{% include '../_custom/google_adsense.swig' %}

插入到 \themes\next\layout\_macro\post.swig 文件中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
        {% else %}
{% if post.type === 'picture' %}
<a href="{{ url_for(post.path) }}">{{ post.content }}</a>
{% else %}
{{ post.content }}
{% endif %}
{% endif %}
{% else %}
<!-- Insert google ad blocks -->
{% include '../_custom/google_adsense.swig' %}

{{ post.content }}
{% endif %}
</div>
{#####################}
{### END POST BODY ###}
{#####################}

我不建议在一个网页中布置多个广告,广告内容雷同,会严重影响用户阅读体验。在页面的侧边布置一个广告,难道不香吗?

注意事项

在成功接入 AdSense 后,google 会根据几种方式和数据判断广告点击是否作弊,从而注销你的账号,所以不要心存侥幸心理,好好发原创文章,提高网站的质量才是王道。

  • 作弊广告点击者的 IP 地址与你 Adsense 账户登录 IP 地址相同
  • 作弊广告点击的 CTR 数据太高
  • 作弊广告点击者的 IP 地址来自同一个地理区域
  • 根据 Cookies 判断作弊 Adsense 广告点击
  • 作弊广告点击者页面停留时间太短
  • 直接访问者的广告点击率过高
  • 流量小但广告点击率高
  • 在网页上用文字提示请求鼓动点击广告

Last but not least

现在通过 AdSense 能赚到的收入本身并不高,尤其是博客类的网站更是如此,但在有空余的地方放置一点广告还是能起到丰富博客的作用。

既然大家愿意花时间和精力去搭建个人博客,除了钱以外,肯定还有其他的目的,希望大家不忘初心,更不要舍本逐末,忘记了自己搭建博客的初衷,毕竟广告收入这个事情,有当然好,没有也不用气馁,尽力就好~

References


坚持原创技术分享,您的支持将鼓励我继续创作!

[toc]

  Unmanned surface vehicles (USVs) are autonomous marine vessels that can execute multiple tasks in a variety of cluttered marine environments without human supervision. The future progress of USVs depends on the development of full autonomy, which would enable USVs to work in any unstructured or unpredictable environment without human intervention.
  One of the main challenges in enhancing USV autonomy is the simultaneous detection and avoidance of obstacles in either open or confined waters. Local obstacle avoidance for USVs is used to avoid static and dynamic obstacles by adopting a timely avoidance manoeuvre. Local obstacle avoidance algorithms are of two types: path searching-based local path planning, which can generate appropriate trajectories or waypoints followed by the vehicle, and the behaviour-based reactive obstacle avoidance method, which can generate the guidance angle and guidance velocity realized by the navigation controller in real time.
  A novel path searching-based algorithm called the local normal distribution-based trajectory (LNDT) is proposed in this paper to address the local obstacle avoidance problem of USVs in complicated encounter situations. The design and implementation of LNDT has been published in Ocean Engineering with reference link. Besides, there is a copy only for personal research purpose.
  The following sections mainly discuss the implementation principle of the algorithm and provide some critical information to facilitate the following researchers. About the detailed information, you can refer my master thesis - Research on the Technology of Path Planning for Unmanned Surface Vehicle, while it is written in Chinese. The LNDS method is mainly extracted from my master thesis and polished in simulation.

Abstract

英文在中文之后 / English follows Chinese
  本文讲述了一种新型的、遵守 COLREGs 规则的无人水面艇 (USV) 自动避障方法的初步研究结果。这个方法本质上是一种基于路径搜索的算法,称为局部正态分布轨迹法,该算法可以在存在静态和动态障碍物的情况下规划出可行的避障轨迹。算法可以根据正态分布的钟形曲线生成符合 COLREGs 的次优轨迹,并提取航路点以供导航控制器安全操纵 USV。此外,我们讨论了构成钟形曲线的三个关键参数,并提出了一种轨迹重新规划策略,以提高该方法的安全性和灵活性。我们在实验中模拟了常见的超车、交叉和正面碰撞情况来验证算法,仿真表明该方法考虑了多个因素,可以规划出能够安全、平滑地避开障碍物的路径。此外,该方法还和另一种遵守 COLREGs 规则的反应式避障方法做了比较。
  This paper reports the preliminary research results of a novel automatic obstacle avoidance approach based on the COLREGs for unmanned surface vehicles (USVs). The approach presented is essentially a path searching-based algorithm called the local normal distribution-based trajectory, which plans viable avoidance trajectories in the presence of both static and dynamic obstacles. The proposed algorithm can generate a COLREGs-compliant suboptimal trajectory based on the bell-shaped curve of normal distribution and extract waypoints for the navigation controller to steer USVs safely. In addition, we discuss three key parameters and present a trajectory replanning strategy to improve the safety and flexibility of our approach. The common overtaking, crossing and head-on collision scenarios are each simulated in experiments. It is shown through simulations that the proposed approach considers multiple factors and can plan paths to avoid obstacles safely and smoothly. A comparison is also made with a reactive path planning algorithm which has been modified to follow the COLREGs.

Design and Implementation

  The overall scheme of obstacle avoidance is illustrated in following figure. The decision-making module in the dashed frame includes a two-layered distributed architecture of obstacle avoidance; we primarily study the COLREGs-based obstacle avoidance approach.

  The LNDT algorithm includes six processes, as shown below. We first integrates COLREGs rules into the obstacle avoidance approach of USVs. When there is a collision risk, it would determine three key parameters of the trajectory by a fixed procedure, and the primitive trajectory would be rotated and translated to adapt the real collision scenario. The USV then follows the planned trajectories. We would replan the trajectory once USVs can not follow the planned trajectory.

  Besides, we propose a scalable and flexible obstacle avoidance architecture. And we proposes six evaluation indicators to establish a scientific and impartial obstacle avoidance behaviour assessment (OABA) method in this paper.

Others

  The LNDT algorithm is mainly implemented in 2018, and it has not been tested in complex sea trial, so there must be some drawbacks to improve.
  In order to put it into wide practice, I’m glad to discuss some topics with you via email and polish this method, even share part of the source code with you.

基于电子海图的水面无人艇全局路径规划

该论文已经在 ICMIR2017 会议上发表,附上 springer 的文献地址 Research and Implementation of Global Path Planning for Unmanned Surface Vehicle Based on Electronic Chart,以及 arXiv 上的 文献地址。本文接下来主要对论文的实现原理进行分析,在最后给出程序代码,方便后来者研究和参考。

摘要

英文在中文之后 / English follows Chinese

水面无人艇 (Unmanned Surface Vehicles, USVs) 是一种新型的水面智能载具,其中全局路径规划是 USV 研究的关键技术,它可以反映出 USV 的智能程度。为了解决 USV 的全局路径规划问题,本文基于电子海图提出航行代价寻优 (sailing cost optimization) 的改进 A * 算法。我们使用 S-57 电子海图实现八叉树网格的环境模型建立,并基于航行安全权重、引导量和路径曲线平滑处理,提出优化的 A * 算法来实现规划路径航行安全,减少规划时间,提高路径平滑度。仿真结果表明,环境建模方法和改进 A * 算法可以规划出安全合理的全局路径。

Unmanned Surface Vehicle (USV) is a new type of intelligent surface boat, and global path planning is the key technology of USV research, which can reflect the intelligent level of USV. In order to solve the problem of global path planning of USV, this paper proposes an improved A* algorithm for sailing cost optimization based on electronic chart. This paper uses the S-57 electronic chart to realize the establishment of the octree grid environment model, and proposes an improved A* algorithm based on sailing safety weight, pilot quantity and path curve smoothing to ensure the safety of the route, reduce the planning time, and improve path smoothness. The simulation results show that the environmental model construction method and the improved A* algorithm can generate safe and reasonable global path.

论文实现原理

水面无人艇的全局路径规划,是指在存在障碍物的复杂环境下,给定起始点和目标点,根据任务要求自主规划出一条能够安全避开环境中静态障碍物的最优路径。

本文提出基于电子海图的 USV 全局路径规划方案,分为电子海图解析、全局环境模型建立和路径搜索算法三部分。通过解析电子海图文件,提取其中的海域地理信息及碍航物等信息,将海图信息渲染成为自主航路规划系统能够识别的信息模式,建立由可航行网格和不可航行网格组成的环境模型。在此基础上,本文综合考虑航行安全性、航行距离、路径平滑度等因素,采用基于航行代价寻优的改进 A * 算法来搜索环境模型中的最优路径,实现水面无人艇的全局航路自主规划,提高水面无人艇的自主决策能力和智能化水平。

水面无人艇全局路径规划系统无法直接利用电子海图进行自主路径规划,所以需要研究矢量电子海图 S-57 数据结构,提取出全局路径规划需要的海洋环境信息,裁剪无关数据,将电子海图转换为全局航路规划系统能够直接使用的信息。其中,利用开源的 TinyXML 库保存到 xml 文件中,供路径规划系统的后续分析和处理。

在提取得到电子海图 xml 文件后,通过网格化的方法把感兴趣区域划分为若干块大小相等的矩形网格,然后利用 Shapely 开源地理库依次判断网格中是否存在从电子海图中解析的静态障碍物(如陆地、海岛、浅滩),从而将网格环境地图划分为可航区域和不可航区域。

在实现环境建模后,全局路径搜索算法优化的目标是在确保航行安全性的前提下,尽可能使规划的路径航行代价最小,最大程度地减小与最短路径、最小航行代价无关的计算量。其中,Hart 等使用启发式函数来评估从初始点到目标点的代价,在 Dijkstra 算法基础上,结合启发式函数形成新的路径搜索策略–A * 算法,通过减少搜索空间来提高搜索效率,所以更适合网格情形下的全局路径规划。本文对传统的 A * 算法进行改进,实现全局路径的初步搜索。

网格环境下,如果直接把 A * 算法搜索得到的节点顺序连接起来作为水面无人艇的全局规划路径,通常会出现阶梯或者锯齿状路径的情况,很明显规划的路径并不是两点间的期望路径。所以,本文提出去除多余航点的曲线平滑算法,在保证安全的前提下减少路径中不必要的航点,使多段折线化为直线,增加路径的光滑度,规划的路径会更加符合起止点之间的期望路径,符合水面无人艇的运动学特性。

实现程序

因为程序是 2017 年 4 月份实现的,仍然不够完善,可能存在 bug 或者性能优化点,欢迎交流和改善。程序最新代码请参考本人 github wylloong。

电子海图解析

S-57 电子海图解析中,本文在之前文章中已经进行详细的分析和讲解,并给出源代码,可以参见 S57-Electronic-Chart-Reading-and-Storing 和 基于 8211lib 库对 s57 电子海图的解析和存储。

建立环境模型

该部分代码可详见 github GlobalEnviStructWithSquare,如有需要请备份,可能后期会删除掉部分程序。

改进 A * 算法和路径平滑处理

该部分代码可详见 github GlobalPathPlanWithSquare,如有需要请备份,可能后期会删除掉部分程序。

程序示例

程序的全局路径规划示意图如下所示:

Note: It’s been years since I last wrote electronic chart, all examples may not be syntactically valid but the idea should be correct.

[toc]

背景

  针对多任务点的全局路径规划,是指在存在静态障碍物的环境中,给定水面无人艇起始点、目标点以及多个任务点的情况下,设计从起始点出发,安全地遍历各个任务点,最终返回目标点的全局路径,要求行驶的航路代价总和最小。

多平台维护不易,最新文章更新于 个人网站,欢迎访问和留言。

  本文主要解决水面无人艇在对多个任务点进行全局路径规划时的设计和实现算方法,相关研究和设计已在 International Journal of Vehicle Autonomous Systems (IJVAS) EI 期刊发表。附 InderScience Publiers - IJVAS 的官方文献下载链接 Design and Implementation of Global Path Planning System for Unmanned Surface Vehicle among Multiple Task Points,以及 arXiv 的下载链接。官方文献下载链接需要科学上网才可以打开。
  本文主要讨论论文的实现原理,并给出部分程序源代码,方便后来者研究和参考。

摘要

英文在中文之后 / English follows Chinese
  全局路径规划是水面无人艇设计的关键技术之一。本文基于电子海图和六边形网格建立全局环境模型,并且证明六边形网格在有效性,安全性和快速性方面优于传统的正方形网格。此外,本文引入 Cube 坐标系来简化六边形坐标运算。进一步地,我们提出了一种改进的 A * 算法来实现两点之间的路径规划。在此基础上,我们构建了多个任务点的全局路径规划模型,并提出了一种改进的蚁群优化算法来准确实现多任务点的全局路径规划。仿真结果表明,全局路径规划系统可以安全、快速地规划出多个任务点的最优路径,在安全性,快速性和路径长度方面均优于传统方法。此外,全局规划路径可直接用于 USV 的实际场景。
  Global path planning is the key technology in the design of unmanned surface vehicles. This paper establishes global environment modelling based on electronic charts and hexagonal grids which are proved to be better than square grids in validity, safety and rapidity. Besides, we introduce Cube coordinate system to simplify hexagonal algorithms. Furthermore, we propose an improved A* algorithm to realize the path planning between two points. Based on that, we build the global path planning modelling for multiple task points and present an improved ant colony optimization to realize it accurately. The simulation results show that the global path planning system can plan an optimal path to tour multiple task points safely and quickly, which is superior to traditional methods in safety, rapidity and path length. Besides, the planned path can directly apply to actual applications of USVs.

论文实现原理

  本文提出的多任务点全局路径规划算法主要分为电子海图解析、六边形网格化建模、两点间路径搜索、多任务点的路径规划四部分。

  其中,通过解析电子海图文件,提取其中的海域地理信息及碍航物等信息,利用六边形进行网格化,建立由可航行六边形网格和不可航行六边形网格组成的环境模型。相关资料可以参考 基于 8211lib 库对 s57 电子海图的解析和存储 和 正六边形网格化 (Hexagonal Grids) 原理与实现。
  在实现环境建模后,两点间路径搜索算法优化的目标是在确保航行安全性的前提下,尽可能使规划的路径航行代价最小,最大程度地减小与最短路径、最小航行代价无关的计算量。本文设计综合多因素的航行代价,利用 Hart 提出的 A * 算法实现两点间的初步搜索,然后利用去除多余航点的曲线平滑算法,在保证安全的前提下减少路径中不必要的航点,使多段折线化为直线,增加路径的光滑度,规划的路径会更加符合起止点之间的期望路径,符合水面无人艇的运动学特性。相关资料可以参考 Research and Implementation of Global Path Planning for Unmanned Surface Vehicle Based on Electronic Chart 论文。
  在多任务点设计中,本文分析了港口巡逻、水质采样、水文勘测等任务对于全局路径规划的具体要求,从中抽象出针对这些任务通用的路径规划需求:在存在障碍物的海洋环境中,在给定多个任务点坐标的情形下,要求水面无人艇从起始点出发,安全地巡行到各个任务点执行具体的任务,再返回到目标点,要求总体的航路代价和最小。如在水质采样中,航路任务要求水面无人艇从固定点出发,根据设定路线行驶到各个采样点,根据采样要求完成全自动采集水样后,返回到目标点进行下一步处理,该航路任务要求完全符合本文抽象出来的全局路径规划要求。
  在水面无人艇复杂任务应用场景下,多任务点的路径规划可以抽象为网络最短路径问题,该网络本质上是一个有权值的有向图。多任务点的全局路径规划抽象模型–网络最短路径问题,可以近似为旅行商问题 (Traveling Salesman Problem, TSP),是非常典型的 NP (Nondeterministic Polynomial) 难问题。在应用蚁群算法解决多任务点的全局路径规划问题时,路径规划问题可以转化为在一个由节点和边组成的网络权重图(网络最短路径模型)中搜索最优闭环路径,如下图所示。相关资料可以参考 基于蚁群算法的机械臂打孔路径规划。

实现程序

  因为程序实现于 2017 年,在性能和流程中仍然不够完善,欢迎邮件进行交流和给出建议。程序最新代码请参考本人的 GitHub wylloong。
  程序运行结果如下,其中黑点为任务点,红色线段为最后规划的全局路径。

程序运行结果

[toc]

本文主要在 S-57 电子海图数据的理论模型和数据结构的基础上,实现对 S-57 电子海图数据文件的解析,将海图信息解析后保存到 xml 文件中,供下游程序调用和后续研究。

什么是电子海图

电子导航图 (Electronic Navigational Chart, ENC) 是为航海需要而绘制的包含海域地理信息和航海信息的一种数字化的专题地图,是由各个国家的航道官方部门根据国际航道组织的《数字海道测量数据传输标准》 (S-57 标准) 而制作的。符合国际标准的电子海图数据统称为 S-57 电子海图, 它是一种面向对象的矢量格式的电子地图,其存储形式是二进制。
电子海图的理论模型和数据结构,可以查看国际标准《数字海道测量数据传输标准》,这里不再详述,对应英文版是《IHO TRANSFER STANDARD for DIGITAL HYDROGRAPHIC DATA》。
S-57 电子海图的结构如下图所示,来自 Research and Implementation of Global Path Planning for Unmanned Surface Vehicle Based on Electronic Chart。

S57数据结构

ISO 8211lib 和 OGR 简介

S-57 标准封转格式是 ISO/IEC 8211 国际标准,封装标准的基础是文件,逻辑记录是封装标准的基本成分。
ISO 8211lib 是 C++ 编写的开源库,专门用于读取符合 ISO 8211 封装标准的数据文件,如 S-57 电子海图数据格式和空间数据传输标准数据集 (SDTS); GDAL 是一个开源栅格空间数据转换库,其中 OGR 是对 GDAL 的扩展,功能与 GDAL 类似,主要提供对矢量数据格式的读写,支持 S-57 电子海图格式,其中 S57reader 类是对 ISO 8211lib 库的封装,该类中包含读取 S-57 电子海图数据文件所用到的基本函数。

程序实现

读取 ENC

OGR 有关于 S-57 读取的官方文档 IHO S-57 (ENC),其中需要注意 OGR 的 S-57 驱动模块将处理 S-57 文件里所有的 feature 要素,其中 S-57 的特征物标 (feature objects) 转换为要素 (feature),空间物标 (geometry objects) 自动转换化对应要素下的空间几何对象。
在 OGR 中,从 S-57 文件读取的所有要素根据物标类型 (OBJL) 归属于相应的图层,可以类比于 GIS 里的 shape 格式,对应的图层下有要素 (feature), 要素下面有字段 (field) 和几何对象 (geometry),要素的几何形状可以从几何形状中获取,要素属性可以从字段中获取。

1
2
3
4
layer according to OBJL
repeated feature
repeated field
repeated geometry

其中,深度信息 (Depth soundings) 是 S-57 文件特有的,在读取 Depth soundings 需要设置 S57 Control Options 中的 ADD_SOUNDG_DEPTH 选项为 ON,对于 S57 Control Options 读取和导出的深层次知识请阅读官网文档。
最后,注意在程序中附加 s57objectclasses.csv 和 s57attributes.csv 文件,否则会缺少信息,如图层只有 5 层,无法读取等深线等。官方文档是这么解释的:

The S-57 reader depends on having two supporting files, s57objectclasses.csv, and s57attributes.csv available at runtime in order to translate features in an object class specific manner. These should be in the directory pointed to by the environment variable S57_CSV, or in the current working directory.

保存到 xml 文件中

因为项目需要,需要把 S-57 文件里特定图层的信息提取出来,S-57 文件解析后,把感兴趣的信息保存到 xml 文件中,方便后续的处理。

在 C++ 中,对 XML 文件处理中,使用一个开源的解析 XML 的解析库 – TinyXML,这个解析库的模型通过解析 XML 文件,然后在内存中生成 DOM 模型,从而让我们很方便的遍历这棵 XML 树。

一些实现细节

如果无法得到一个 object class 的属性或者值不正确,如 VALDCO 的值始终为 0。根据论坛的讨论 OGR to access s57 file, missing attributes issue,注意是否加载了 S-57 的支持文件.csv。

The solution to that is to set the GDAL_DATA environment variable to point the directory with the s57objectclasses.csv and related files。

代码和结果

解析和存储效果如下图所示,按图层要素顺序依次组织。

S57结构

程序实现源代码及示例,欢迎访问 Github: wylloong/S57-Electronic-Chart-Reading-and-Storing。
程序开发环境是 VS2015,其中需要在编译的时候修改两个位置,如下图所示,否则编译会报错。

S57结构

S57结构

如果有内容讨论,可以下面留言。

References

[toc]

Hexo 搭建博客后,如果想在博客中分享视频,避免网站服务器带宽的限制,需要添加其他视频网站的外链播放器,具体该怎么操作呢?

Solution 1: hexo-tag-aplayer

hexo-tag-dplayer 是 DPlayer 播放器的 Hexo 标签插件。

因为版本维护者不再支持新版本的更新,有些 bug 没有解决,所以对于高版本的 Hexo 支持不友好,甚至安装阶段就会报错。所以,我不是很支持继续使用该方法。

ps. 想继续坚持使用该方法的,可以移步阅读 在 Hexo 搭建的博客中插入音乐或者视频。

Solution 2: 视频网站外链

Hexo 支持 iframe 的方式插入视频,所示我们可以把视频上传到哔哩哔哩、优酷、腾讯视频等,然后生成外链再拿来用。

大部分视频网站的外链播放器在视频开头都会加上 30 秒 - 2 分钟的广告,于是选择 B 站,无广告,无广告,无广告!

去 B 站获取视频外链

选择视频下方 “分享” 里的嵌入代码,而不是视频地址。否则进入 Hexo 博客后,网页会自动跳转到 bilibili 的视频页面,而不是自己的博客页面。

在文章中插入视频外链

Markdown 中可以直接插入 html 代码,但是 hexo 给我们特意准备了一个 raw 标签来插入 html 代码,防止一些其他因素的干扰,这里我们就使用 raw 来插入视频外链。

因为外链 html 没有和 Hexo 适配,所以在网页上打开,界面靠左并且窗口小,显得很难看,不够大气。

美化播放器样式

这里分享一个很巧妙的偷懒方法,我们可以去 Hexo 的 官网,看看作者是如何布局的,然后把代码抄回来。

那么,我们只要把布局中的 src,替换为自己的外链地址,就可以设置好视频的布局。

因为简书不支持 iframe,大家可以去我的网站看看效果。

References

0%