McKay 阅读(7) 评论(0)

从vb6到vb.net,一路c#, java, python, nn, c,对技术的切换早已经没有害怕的感觉了,一直有的是技术的热情和我所认为的技术信仰。

扯完,开始正文。

看看效果图:

使用的是Arduino UNO+16 Servo Drive Board+2个SG90舵机(左右手分别1个舵机摇摆)

Arduino UNO:

16 Servo Drive Board

然后将这2块叠在一起插上引脚硬件就好了 

连的是5和6引脚

 

接下来就程序了,直接编码算是很简单的,就2步:

  1. 拷贝厂商的.h/.cpp文件夹到arduino的libraries目录
  2. 写代码如下:
  3. #include <Adafruit_PWMServoDriver.h>
    
    #define SERVOMIN 200
    #define SERVOMAX 540
    #define SERVOMIN_Angle 0
    #define SERVOMAX_Angle 112
    
    Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
    
    void servo_goto_angle(int pinId, int angle)
    {
         int anglePwmValue=map(angle, SERVOMIN_Angle, SERVOMAX_Angle, SERVOMIN, SERVOMAX);
         pwm.setPWM(pinId,0, anglePwmValue);
    }
    
    void servo_init()
    {
         Serial.begin(9600);
         pwm.begin();
         pwm.setPWMFreq(60);
         delay(200);
    }
    
    void setup(){
      servo_init();
    }
    
    void loop(){
      for (int i = 1; i <= 112; i = i + (1)) {
        servo_goto_angle(5, i);
        delay(10);
      }
      for (int i = 112; i >= 1; i = i + (-1)) {
        servo_goto_angle(5, i);
        delay(10);
      }
      for (int i = 1; i <= 112; i = i + (1)) {
        servo_goto_angle(6, i);
        delay(10);
      }
      for (int i = 112; i >= 1; i = i + (-1)) {
        servo_goto_angle(6, i);
        delay(10);
      }
    
    }

     

然后交叉编译,上传就好了。

 

 

 

 

 

 

 

 

 

 

 

可是(哈哈,这里还有,故意空出了一大段),考虑到我还要教女儿学会这个,就不能这么直接代码给她看了,毕竟才小学1年级,还好,有mixly,好,希望来了,图形化编程来了。

 

鼓捣了一般,最后发现通用的组件都能用,只是这个舵机由于是厂商买来的,还没法很好的支持,得自定义!这下麻烦了,找了点mixly二次开发的资料看了看,最终决定自己写mixly插件。

看了看mixly插件其实就是用js编写外观以及生成代码部分拼c代码出来,然后就好办了,下面是完整插件的目录结构(xml是描述文件、block中的js是生成积木外观的、generator中的js是生成c代码的):

 

McKay.xml

<!--
type="company"
block="block/McKay.js"
generator="generator/McKay.js"
media="media/McKay"
language="language/McKay"
-->
<script type="text/javascript" src="../../blocks/company/McKay.js"></script>
<script type="text/javascript" src="../../generators/arduino/company/McKay.js"></script>
<category id="McKay_ServoDriveBoard" name="McKay-舵机" colour="20">
    <block type="ServoDriveBoard_Init">
        <value name="MinAngle">
        </value>
        <value name="MaxAngle">
        </value>
    </block>

    <block type="ServoDriveBoard_Do">
        <value name="ServoNumber">
        </value>
        <value name="Angle">
        </value>
    </block>
</category>

 

block\McKay.js

'use strict';

goog.provide('Blockly.Blocks.McKay');

goog.require('Blockly.Blocks');


Blockly.Blocks.ServoDriveBoard_Init={
init:function(){
    this.appendDummyInput("")
          .appendField("初始化舵机");
        this.appendValueInput("MinAngle").setCheck(Number).appendField("最小角度");
        this.appendValueInput("MaxAngle").setCheck(Number).appendField("最大角度");

        this.setColour(20);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setInputsInline(false);
        this.setTooltip("初始化舵机");
  }
};




Blockly.Blocks.ServoDriveBoard_Do={
init:function(){
    this.appendDummyInput("")
          .appendField("舵机运行");
        this.appendValueInput("ServoNumber").setCheck(Number).appendField("舵机编号");
        this.appendValueInput("Angle").setCheck(Number).appendField("角度");

        this.setColour(20);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setInputsInline(true);
        this.setTooltip("舵机运行");
  }
};

 

generator\McKay.js

'use strict';

goog.provide('Blockly.Arduino.McKay');

goog.require('Blockly.Arduino');



Blockly.McKay_ServoDriveBoard_MinAngle=0;
Blockly.McKay_ServoDriveBoard_MaxAngle=0;
Blockly.Arduino.ServoDriveBoard_Init=function(){
    var MinAngle = Blockly.Arduino.valueToCode(this, 'MinAngle', Blockly.Arduino.ORDER_ATOMIC) || '0';
    var MaxAngle = Blockly.Arduino.valueToCode(this, 'MaxAngle', Blockly.Arduino.ORDER_ATOMIC) || '112';

    Blockly.McKay_ServoDriveBoard_MinAngle=parseInt(MinAngle);
    Blockly.McKay_ServoDriveBoard_MaxAngle=parseInt(MaxAngle);

    Blockly.Arduino.definitions_["define_McKay_ServoDriveBoard_Include"] = "#include <Adafruit_PWMServoDriver.h>\n";

    var defines="#define SERVOMIN 200\n";
    defines+="#define SERVOMAX 540\n";
    defines+="#define SERVOMIN_Angle "+Blockly.McKay_ServoDriveBoard_MinAngle+"\n";
    defines+="#define SERVOMAX_Angle "+Blockly.McKay_ServoDriveBoard_MaxAngle+"\n";

    Blockly.Arduino.definitions_["define_McKay_ServoDriveBoard_Defines"] = defines;

    Blockly.Arduino.definitions_["define_McKay_ServoDriveBoard_pwmObj"] = "Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();\n";


    var gotoCode="void servo_goto_angle(int pinId, int angle)\n";
    gotoCode+="{\n";
    gotoCode+="     int anglePwmValue=map(angle, SERVOMIN_Angle, SERVOMAX_Angle, SERVOMIN, SERVOMAX);\n";
    gotoCode+="     pwm.setPWM(pinId,0, anglePwmValue);\n";
    gotoCode+="}\n";
    Blockly.Arduino.definitions_["define_McKay_ServoDriveBoard_goto_angle"] = gotoCode;

    var initCode="void servo_init()\n";
    initCode+="{\n";
    initCode+="     Serial.begin(9600);\n";
    initCode+="     pwm.begin();\n";
    initCode+="     pwm.setPWMFreq(60);\n";
    initCode+="     delay(200);\n";
    initCode+="}\n";
    Blockly.Arduino.definitions_["define_McKay_ServoDriveBoard_initCode"] = initCode;

    return "servo_init();\n";
};



Blockly.Arduino.ServoDriveBoard_Do=function(){
    var ServoNumber = Blockly.Arduino.valueToCode(this, 'ServoNumber', Blockly.Arduino.ORDER_ATOMIC) || '0';
    var Angle = Blockly.Arduino.valueToCode(this, 'Angle', Blockly.Arduino.ORDER_ATOMIC) || '0';

  var code="servo_goto_angle("+ServoNumber+", "+Angle+");\n";
  return code;
};

 

多年编程经验的你肯定得出结论其实就是通过js拼string,string里是c代码。。。

然后就可以老少皆宜的写代码了:

 

有兴趣玩玩这些,挺有意思的

欢迎加入钉钉创客Maker群: