機器人控制器編程課程-教案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等編程。
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); }
