< >
Home » TurtleBot-Blockly教程 » TurtleBot-Blockly入门教程-创建带输入的block

TurtleBot-Blockly入门教程-创建带输入的block

TurtleBot-Blockly入门教程-创建带输入的block

说明:

  • 介绍如何编写带输入功能的block
  • block类型有三种:
    • 带输入的block
    • 带输出的block
    • 不带输入或输出的block

带输入的block:

  • 涉及的两个文件:
turtlebot_blockly/frontend/blockly/generators/python/customName.js
turtlebot_blockly/frontend/blockly/blocks/customName.js
  • 我们将看到move_forward块的同一个例子。
  • ../generators/python/customName.js中的文件具有以下代码
Blockly.Python['move_forward'] = function(block) {

var dropdown_speed = block.getFieldValue('speed');

var code = "";
code += "dropdown_speed = \"" + dropdown_speed.toString() + "\"\n";
code += Blockly.readPythonFile("../blockly/generators/python/scripts/turtlebot3/move_forward.py");
return code;

};
  • 下面的代码片段为用户提供了速度{SLOW,NORMAL或FAST}的值。
var dropdown_speed = block.getFieldValue('speed');
  • 位置../blockly/blocks/customName.js中的.js文件具有以下代码
Blockly.Blocks['move_forward'] = {
  init: function() {
    this.appendDummyInput()
        .appendField("Move_Forward ")
        .appendField(new Blockly.FieldDropdown([["SLOW", "SLOW"], ["NORMAL", "NORMAL"], ["FAST", "FAST"]]), "speed")
        .appendField("Speed");
    this.setPreviousStatement(true);
    this.setNextStatement(true);
    this.setColour(65);
    this.setTooltip('');
    this.setHelpUrl('http://erlerobotics.com/docs/Robot_Operating_System/ROS/Blockly/Intro.html');
  }
};
  • 除了输入来自用户的速度值之外,我们还看到以下内容:
this.setPreviousStatement(true);
this.setNextStatement(true);
  • 这两行为块提供了连接前一块和未来块的方法。
  • 如图:

请输入图片描述

  • 重复块连接到move_forward块再次连接到Wait块。
  • 该组块的python脚本是:
for count in range(2):
  dropdown_speed = "SLOW"
  import rospy, sys
  import time
  from geometry_msgs.msg import Twist

  pub = rospy.Publisher('cmd_vel', Twist, queue_size=10)
  #rospy.init_node('circle_mode', anonymous=True)
  rate = rospy.Rate(10) # 10Hz
  twist = Twist()
  start = time.time()
  flag=True #time flag
  # Angular velocity = linear velocity / radius
  speed=dropdown_speed # SLOW, NORMAL, FAST
  twist.linear.z = 0.00

  # CLOCKWISE rotation
  if speed =='SLOW':
      twist.linear.y = 0.05
      twist.linear.x = 0.05
  elif speed =='NORMAL':
      twist.linear.y = 0.25
      twist.linear.x = 0.25
  elif speed == 'FAST':
      twist.linear.y = 0.75
      twist.linear.x = 0.75
  while not rospy.is_shutdown() and flag:
      sample_time=time.time()
      if ((sample_time - start) > 3):
        flag=False
      pub.publish(twist)
  twist = Twist()
  pub.publish(twist)
  rate.sleep()
  import time
  time.sleep(3)

纠错,疑问,交流: 请进入讨论区点击加入Q群

获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号


标签: turtlebot-blockly入门教程