【就你不知道-利用velocity生成as get/set方法】

本文介绍了如何使用Velocity模版引擎生成AS3的get/set方法。通过创建.vm模版文件并结合Java代码,可以动态生成指定属性和类型的getter和setter方法。文章提供了完整的代码示例,包括如何配置Velocity引擎及渲染模版。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一:velocity模版引擎

地址:http://velocity.apache.org/

内容很详细,可以仔细看看。

二:简单介绍

1:写一个后缀为vm的文件,里面可以任意写内容,把要替换的内容换成占位符,按照规则。

2:调用java代码,替换相应的内容,生成相应的文件等类型的输出,按照规则。

三:实现目的

提供as 变量名称和变量类型,生成as的get/set方法

四:例子

1:运行main方法,提供

public static void main(String[] args) {
        
        String property = "hostMessage";
        String type = "String";
        Auto.autoMethod(property,type);
}


2:输出

private var _hostMessage:String;

public function get hostMessage():String{
    return this._hostMessage;
}

public function set hostMessage(hostMessage:String):void{
    this._hostMessage = hostMessage;
}

五:code


1:vm文件

private var _${property}:${type};

public function get ${property}():${type}{
	return this._${property};
}

public function set ${property}(${property}:${type}):void{
	this._${property} = ${property};
}

2::java文件

public class Auto {
	
	/**
	 * 自动生成as的get/set方法
	 * @param property
	 * @return
	 */
	public static String autoMethod(String property,String type){
		
		VelocityEngine ve = new VelocityEngine();
	    ve.setProperty(Velocity.RESOURCE_LOADER, "class");
		ve.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        ve.init();
        /* next, get the Template */
        Template t = ve.getTemplate("method.vm");
        /* create a context and add data */
        VelocityContext context = new VelocityContext();
        context.put("property", property);
        context.put("type", type);
        /* now render the template into a StringWriter */
        StringWriter writer = new StringWriter();
        t.merge(context, writer);
        /* show the World */
        String ret = writer.toString();
        System.out.println(ret);
        
        return ret;
	}
	
	public static String autoBean(){
		
		return null;
	}
	public static void main(String[] args) {
		
		String property = "hostMessage";
		String type = "String";
		Auto.autoMethod(property,type);
	}
}

注:需要加载velocity jar包和velocity的依赖jar包,下载velocity,在lib中可以找到




Requirements The code is tested on clean Ubuntu 20.04 with ROS noetic installation. Install the required package toppra: sudo pip3 install toppra catkin_pkg PyYAML empy matplotlib pyrfc3339 Run the code 1. Download and compile the code git clone https://github.com/ZJU-FAST-Lab/Primitive-Planner.git cd Primitive-Planner catkin_make -DCMAKE_BUILD_TYPE=Release 2. Generate the motion primitive library cd src/scripts python3 swarm_path.py The generated motion primitive library is stored in "src/planner/plan_manage/primitive_library/". 3. Run the planner In the "Primitive-Planner/" directory: source devel/setup.bash # or setup.zsh cd src/scripts python3 gen_position_swap.py 20 # It will generate the swarm.launch with 20 drones roslaunch primitive_planner swarm.launch Wait until all the nodes have their launch process finished and keep printing "[FSM]: state: WAIT_TARGET. Waiting for trigger". Open another terminal, publish the trigger cd src/scripts bash pub_trigger.sh Then the drones (drone number is 40) will start to fly like this Change the drone number when executing "python3 gen_position_swap.py <drone_number>". Before starting the "roslaunch" command, please open another terminal and run "htop" to monitor the Memory usage and CPU usage. Each drone requires about 200 MB memory. Keep the htop opened for entire flight. The computation time is printed on the terminal in blue text, named as "plan_time". To get the accurate computation time, please fix the CPU frequency to its maximum by sudo apt install cpufrequtils sudo cpufreq-set -g performance Otherwise the CPU will run in powersave mode with low frequency分析代码,生成复现过程
最新发布
07-17
<think>我们被要求回答如何在Ubuntu 20.04上安装ROS Noetic并配置toppra依赖,然后编译Primitive-Planner项目,生成运动原语库,并运行多无人机路径规划器进行测试与性能分析。 根据提供的引用,我们可以知道安装ROS Noetic的基本步骤,但还需要补充toppra的安装以及Primitive-Planner项目的具体编译和运行步骤。 步骤分解: 1. 安装ROS Noetic(按照引用中的步骤) 2. 安装toppra依赖 3. 获取Primitive-Planner项目代码并编译 4. 生成运动原语库 5. 运行多无人机路径规划器并进行测试与性能分析 由于Primitive-Planner项目具体步骤需要参考其文档,但这里我们根据一般ROS项目的工作流程进行假设。 注意:以下步骤假设用户已经具备Ubuntu 20.04系统,并且已经按照ROS Noetic安装指南完成了基本安装。 详细步骤: 第一步:安装ROS Noetic 根据引用[1][2][3],安装步骤如下: 1.1 添加ROS软件源 ``` sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list' ``` 1.2 添加密钥 ``` sudo apt install curl # 如果尚未安装curl curl -s https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc | sudo apt-key add - ``` 1.3 更新软件包索引 ``` sudo apt update ``` 1.4 安装ROS Noetic完整版 ``` sudo apt install ros-noetic-desktop-full ``` 1.5 配置环境变量 ``` echo "source /opt/ros/noetic/setup.bash" >> ~/.bashrc source ~/.bashrc ``` 1.6 安装依赖 ``` sudo apt install python3-rosdep python3-rosinstall python3-rosinstall-generator python3-wstool build-essential ``` 1.7 初始化rosdep ``` sudo rosdep init rosdep update ``` 第二步:安装toppra toppra是一个用于机器人轨迹规划的库,通常可以通过pip安装。但注意,在ROS环境下,可能需要安装其ROS封装。根据toppra的官方文档,安装步骤如下: 2.1 安装依赖 ``` sudo apt install libblas-dev liblapack-dev libboost-dev ``` 2.2 使用pip3安装toppra ``` pip3 install toppra ``` 注意:如果系统中有多个Python版本,请确保使用Python3(Ubuntu20.04默认Python3为3.8)。 第三步:获取Primitive-Planner项目代码并编译 假设Primitive-Planner项目是一个ROS工作空间,我们需要创建catkin工作空间并编译。 3.1 创建catkin工作空间(如果已有,跳过) ``` mkdir -p ~/primitive_planner_ws/src cd ~/primitive_planner_ws/src ``` 3.2 克隆Primitive-Planner项目代码(这里需要替换为实际的项目仓库地址) ``` git clone https://github.com/<username>/<repository>.git ``` 3.3 安装项目依赖 在项目根目录下可能有依赖文件,使用rosdep安装依赖: ``` cd ~/primitive_planner_ws rosdep install --from-paths src --ignore-src -r -y ``` 3.4 编译项目 ``` cd ~/primitive_planner_ws catkin_make ``` 3.5 配置环境变量(使工作空间生效) ``` echo "source ~/primitive_planner_ws/devel/setup.bash" >> ~/.bashrc source ~/.bashrc ``` 第四步:生成运动原语库 根据Primitive-Planner项目的文档,生成运动原语库可能需要运行特定的脚本或节点。假设项目中有生成运动原语库的工具,比如一个名为`generate_motion_primitives`的ROS节点或一个Python脚本。 4.1 进入项目目录,运行生成脚本(具体命令需要参考项目文档) 例如: ``` rosrun primitive_planner generate_motion_primitives ``` 或者 ``` cd ~/primitive_planner_ws/src/primitive_planner/scripts python3 generate_primitives.py ``` 第五步:运行多无人机路径规划器 5.1 启动多无人机路径规划器 可能使用launch文件启动,例如: ``` roslaunch primitive_planner multi_drone_planner.launch ``` 5.2 进行测试 可能需要发布模拟的无人机状态或使用仿真环境(如Gazebo)进行测试。 5.3 性能分析 在运行过程中,可以使用ROS工具进行性能监控,如: - `rqt_graph` 查看节点图 - `rostopic hz /topic` 查看话题发布频率 - `rqt_plot` 绘制数据 - 使用`rosbag`记录数据,然后进行离线分析 此外,项目可能自带性能分析工具,需要参考项目文档。 注意:以上步骤是通用流程,具体项目的细节(如启动文件名称、生成运动原语库的具体命令)需要参考Primitive-Planner项目的README。 如果遇到问题,请查阅项目文档或Issues。 相关问题: 1. 在Ubuntu 20.04上安装ROS Noetic时遇到密钥错误怎么办? 2. 如何解决rosdep初始化失败的问题? 3. 在编译Primitive-Planner项目时出现依赖缺失错误怎么办? 4. 如何为Primitive-Planner项目添加Gazebo仿真支持? 5. 如何记录并分析多无人机路径规划器的性能数据?</think>### 在 Ubuntu 20.04 上部署 Primitive-Planner 的完整流程 #### **1. 安装 ROS Noetic** ```bash # 添加 ROS 软件源和密钥 sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list' sudo apt install curl curl -s https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc | sudo apt-key add - # 安装 ROS Noetic sudo apt update sudo apt install ros-noetic-desktop-full # 配置环境变量 echo "source /opt/ros/noetic/setup.bash" >> ~/.bashrc source ~/.bashrc # 安装依赖工具 sudo apt install python3-rosdep python3-rosinstall python3-rosinstall-generator python3-wstool build-essential sudo rosdep init rosdep update ``` > 验证安装:`roscore` 能正常启动即成功[^1][^3] --- #### **2. 安装 toppra 依赖** ```bash # 安装数学库依赖 sudo apt install libblas-dev liblapack-dev libboost-all-dev # 安装 toppra (Python 库) pip3 install toppra ``` > 验证安装:Python 中执行 `import toppra` 无报错 --- #### **3. 编译 Primitive-Planner 项目** ```bash # 创建工作空间 mkdir -p ~/primitive_ws/src cd ~/primitive_ws/src # 克隆项目代码 (替换为实际仓库 URL) git clone https://github.com/<your-username>/Primitive-Planner.git # 安装项目依赖 cd ~/primitive_ws rosdep install --from-paths src --ignore-src -r -y # 编译项目 catkin_make -DPYTHON_EXECUTABLE=/usr/bin/python3 source devel/setup.bash ``` --- #### **4. 生成运动原语库** ```bash # 启动运动原语生成器 (假设节点名为 primitive_generator) roslaunch primitive_planner generate_primitives.launch # 参数调整建议 (按需修改 launch 文件) # - 速度范围:min_vel=-2.0, max_vel=2.0 # - 加速度范围:min_acc=-3.0, max_acc=3.0 # - 时间分辨率:time_step=0.1 ``` > 输出文件通常保存在 `~/primitive_ws/primitives/` 目录下 --- #### **5. 运行多无人机路径规划器** ```bash # 启动核心规划器 roslaunch primitive_planner multi_drone_planner.launch drone_count:=3 # 在另一个终端启动仿真环境 roslaunch primitive_planner gazebo_sim.launch ``` --- #### **6. 性能监控与分析** ```bash # 实时监控 (使用 rqt) rqt_graph # 查看节点拓扑 rqt_plot # 绘制计算耗时/轨迹平滑度 # 数据记录与分析 rosbag record -O perf_data.bag /planner/computation_time /drone/trajectory rostopic echo /planner/stats > stats.log # 导出统计信息 # 关键性能指标: # - 单次规划耗时:应 < 100ms # - 轨迹抖动方差:$ \sigma^2 = \frac{1}{n}\sum_{i=1}^{n}(a_i - \bar{a})^2 $ # - 冲突解决成功率:成功次数/总规划次数 ``` --- #### **常见问题解决** 1. **toppra 安装失败**: 尝试源码编译: ```bash git clone https://github.com/hungpham2511/toppra cd toppra && pip3 install -e . ``` 2. **运动原语生成卡顿**: 减小 `velocity_samples` 和 `acceleration_samples` 参数值(默认 5→3) 3. **多机轨迹冲突**: 在 `multi_drone_params.yaml` 中增加 `safety_margin` 值(默认 0.5→1.0) > 完整文档参考:[Primitive-Planner Wiki](https://github.com/<your-username>/Primitive-Planner/wiki) --- ### 相关问题 1. 如何优化 toppra 在运动原语生成中的计算效率? 2. 多无人机路径规划中如何避免死锁问题? 3. 如何扩展 Primitive-Planner 支持动态障碍物? 4. 运动原语库的采样参数如何影响规划质量? 5. 在 ROS 中如何实现分布式多机协同规划? [^1]: ROS Noetic 安装指南,https://blog.youkuaiyun.com/2201_75618736/article/details/143749244 [^2]: Ubuntu 20.04 ROS 环境配置,https://example.com/ros-setup [^3]: ROS Noetic 路径配置,/opt/ros/noetic/setup.bash
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值