Velocity Quick Start [1] - 第一个实例

本文介绍了一个简单的Velocity学习示例,通过该示例可以了解如何使用Velocity模板引擎将数据填充到模板中,并输出到字符串。示例代码展示了如何初始化Velocity引擎、准备上下文数据以及执行模板解析。

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

源文件
package nc.jonathan.velocity;
import java.io.StringWriter;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;

/**
 *<p> 第一个velocity学习类 </p>
 *
 *<p> 采用默认初始化设置,解析字符串, 输出到字符串</p>
 *
 * Create on 2006-4-3 10:12:38
 *
 * @author Jonathan Q. Bo
 * @version 1.0 valocity study
 */
public class QuickStartExample {
 public static void main(String[] args){
  try {
   /* 初始化运行时引擎, 默认初始化 */
   Velocity.init();
   
   /* 建立context, 并放入数据*/
   VelocityContext context = new VelocityContext();
   context.put("project","jonathan's velocity");
   context.put("name","first example");
   
   /* 解析后数据的输出目标,java.io.Writer的子类 */
   StringWriter w = new StringWriter();
   
   /* 此处采用字符串模版 */
   String s = "We are using string velocity to generate $project $name";
   
   /* 进行解析 */
   Velocity.evaluate(context,w,"mystring",s);
   System.out.println(w);
  } catch (ResourceNotFoundException e1) {
   System.out.println("## 源文件不存在!");
   e1.printStackTrace();
  } catch (ParseErrorException e2) {
   System.out.println("## 解析文件错误!");
   e2.printStackTrace();
  } catch (MethodInvocationException e3) {
   System.out.println("## 方法调用异常!");
   e3.printStackTrace();
  } catch (Exception e4){
   System.out.println("## 其他错误!");
   e4.printStackTrace();
  }
 }
}
输出结果
We are using string velocity to generate jonathan's velocity first example
 
找出代码中的所有变量 def step_robot(r: rtb.ERobot, Tep): wTe = r.fkine(r.q) eTep = np.linalg.inv(wTe) @ Tep # Spatial error et = np.sum(np.abs(eTep[:3, -1])) # Gain term (lambda) for control minimisation Y = 0.01 # Quadratic component of objective function Q = np.eye(r.n + 6) # Joint velocity component of Q Q[: r.n, : r.n] *= Y Q[:2, :2] *= 1.0 / et # Slack component of Q Q[r.n :, r.n :] = (1.0 / et) * np.eye(6) v, _ = rtb.p_servo(wTe, Tep, 1.5) v[3:] *= 1.3 # The equality contraints Aeq = np.c_[r.jacobe(r.q), np.eye(6)] beq = v.reshape((6,)) # The inequality constraints for joint limit avoidance Ain = np.zeros((r.n + 6, r.n + 6)) bin = np.zeros(r.n + 6) # The minimum angle (in radians) in which the joint is allowed to approach # to its limit ps = 0.1 # The influence angle (in radians) in which the velocity damper # becomes active pi = 0.9 # Form the joint limit velocity damper Ain[: r.n, : r.n], bin[: r.n] = r.joint_velocity_damper(ps, pi, r.n) # Linear component of objective function: the manipulability Jacobian c = np.concatenate( (np.zeros(2), -r.jacobm(start=r.links[4]).reshape((r.n - 2,)), np.zeros(6)) ) # Get base to face end-effector kε = 0.5 bTe = r.fkine(r.q, include_base=False).A θε = math.atan2(bTe[1, -1], bTe[0, -1]) ε = kε * θε c[0] = -ε # The lower and upper bounds on the joint velocity and slack variable lb = -np.r_[r.qdlim[: r.n], 10 * np.ones(6)] ub = np.r_[r.qdlim[: r.n], 10 * np.ones(6)] # Solve for the joint velocities dq qd = qp.solve_qp(Q, c, Ain, bin, Aeq, beq, lb=lb, ub=ub,solver='piqp').copy() qd = qd[: r.n] if et > 0.5: qd *= 0.7 / et else: qd *= 1.4 if et < 0.02: return True, qd else: return False, qd env = swift.Swift() env.launch(realtime=True) ax_goal = sg.Axes(0.1) env.add(ax_goal) frankie =
最新发布
04-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值