机器人控制器编程课程-教案02-基础

  • 2019 年 10 月 5 日
  • 筆記

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/ZhangRelay/article/details/100888007

经过第一次课程,需要理解为何学习这门课?!

  • 是教学大纲要求不得不学?
  • 还是的确有趣并实用,必须要学?

那么如何学习一门技术,包括机器人控制器编程技术???

现在技术更新速度十分快,如何学习最新并使用最广的相关技术呢???

  • 要点:跨平台,或者称之为多平台支持

arduino

Arduino软件支持windows、Mac OS X和Linux,凡是主流并广泛使用的工具都是全平台支持的。

如ROS2、Webots和V-Rep都是全平台支持的,也是本课程学有余力的同学推荐安装并学习的(还有Matlab^_^)。

ROS 2

Webots

V-Rep

还有其他特点吗????

  • 要点:多语言支持,如支持至少3种以上语言(C++、Python、Java)进行程序编写

同样以Arduino,通过合适的配置不仅可以实现仿真,还支持Python、Matlab等编程。

所有IoT(物联网)硬件都是多平台多语言支持的!!!

Arduino微控制器和SoC世界无处不在的“Hello World”程序是“闪烁LED”。以下代码演示了如何使用Johnny-Five框架完成此操作。

var five = require("johnny-five");  var board = new five.Board();    board.on("ready", function() {    // Create an Led on pin 13    var led = new five.Led(13);    // Blink every half second    led.blink(500);  });

Arduino(JavaScript Robotics和IoT编程框架)

第2章,25-73页,分别介绍了Arduino语言结构、扩展模块等。

基础包括软件和硬件。

顺序结构(灯闪烁):

Blink

void setup() {    pinMode(13, 1);  }    void loop() {    digitalWrite(13, 1);    delay(1000);    digitalWrite(13, 0);    delay(1000);  }              

选择结构(判断字符):

int val;//定义变量val  int ledpin=13;//定义数字接口13  void setup()  {    Serial.begin(9600);//设置波特率为9600,这里要跟软件设置相一致。当接入特定设备(如:蓝牙)时,我们也要跟其他设备的波特率达到一致。    pinMode(ledpin,OUTPUT);//设置数字13 口为输出接口,Arduino 上我们用到的I/O 口都要进行类似这样的定义。  }  void loop()  {    val=Serial.read();//读取PC 机发送给Arduino 的指令或字符,并将该指令或字符赋给val    if(val=='X')//判断接收到的指令或字符是否是“X”。    {//如果接收到的是“X”字符      digitalWrite(ledpin,HIGH);//点亮数字13 口LED。      delay(500);      digitalWrite(ledpin,LOW);//熄灭数字13 口LED      delay(500);      Serial.println("Hello World!");//显示“Hello World!”字符串    }    else    {      Serial.println("No XXXX");    }  }

循环结构(跑马灯):

for 1

for 2

int BASE = 2 ;  //第一顆 LED 接的 I/O 腳  int NUM = 6;    //LED 的總數    void setup()  {     for (int i = BASE; i < BASE + NUM; i ++)     {       pinMode(i, OUTPUT);   //設定數字I/O腳為輸出     }  }    void loop()  {     for (int i = BASE; i < BASE + NUM; i ++)     {       digitalWrite(i, 1);    //設定數字I/O腳輸出為"低",即逐漸關燈       delay(200);            //延遲     }     for (int i = BASE; i < BASE + NUM; i ++)     {       digitalWrite(i, 0);    //設定數字I/O腳輸出為"低",即逐漸開燈       delay(200);            //延遲     }  }

从PWM到呼吸灯:

int led = 9;           // the PWM pin the LED is attached to  int brightness = 0;    // how bright the LED is  int fadeAmount = 5;    // how many points to fade the LED by    // the setup routine runs once when you press reset:  void setup() {    // declare pin 9 to be an output:    pinMode(led, OUTPUT);  }    // the loop routine runs over and over again forever:  void loop() {    // set the brightness of pin 9:    analogWrite(led, brightness);      // change the brightness for next time through the loop:    brightness = brightness + fadeAmount;      // reverse the direction of the fading at the ends of the fade:    if (brightness <= 0 || brightness >= 255) {      fadeAmount = -fadeAmount;    }    // wait for 30 milliseconds to see the dimming effect    delay(30);  }

模拟量输入控制LED灯闪烁时间:

analog

int sensorPin = A0;    // select the input pin for the potentiometer  int ledPin = 13;      // select the pin for the LED  int sensorValue = 0;  // variable to store the value coming from the sensor    void setup() {    // declare the ledPin as an OUTPUT:    pinMode(ledPin, OUTPUT);  }    void loop() {    // read the value from the sensor:    sensorValue = analogRead(sensorPin);    // turn the ledPin on    digitalWrite(ledPin, HIGH);    // stop the program for <sensorValue> milliseconds:    delay(sensorValue);    // turn the ledPin off:    digitalWrite(ledPin, LOW);    // stop the program for for <sensorValue> milliseconds:    delay(sensorValue);  }