This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import serial | |
import threading | |
import json | |
import struct | |
class Protocol(object): | |
def __init__(self, comport = 'COM13'): | |
self.ser = serial.Serial(comport, 115200) | |
self.ser.timeout = 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
float areaCircle = getAreaCircle(5.2); // แบบมี return ค่า จะสามารถสร้างตัวแปรมาเก็บค่าที่มัน return กลับมาได้ หรือจะไม่ใช้ตัวแปรมารับก็ได้ | |
openLed(1000); // เรียก function เพื่อสั่งให้ LED เปิดเป็นเวลา 1 วินาที แล้วดับ | |
closeLedMain(); // เรียก function เพื่อปิดตัว LED หลัก |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
float areaCircle = getAreaCircle(5.5); // ค่าที่ function รับ เราให้มันเป็นค่า radius หรือรัศมี ดังนั้นตอนเรียก function เราควรจะกำหนดให้มันด้วย | |
// เมื่อ function คำนวณเสร็จแล้ว มันก็จะ return ค่า area กลับมา เราก็สามารถสร้างตัวแปรเพื่อมาเก็บค่านั้นได้ | |
// ถ้ามองอีกมุมนึง ก็เหมือนว่า | |
// float areaCircle = getAreaCircle(5.5); | |
// มันก็คือ | |
// float areaCircle = area; นั่นแหละลองย้อนกลับไปดู เพราะ area คือตัวแปรที่ return กลับมาจาก code ก่อนหน้านี้ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
การสร้าง function มีด้วยกันอยู่ 3 ส่วนใหญ่ ๆ ดังนี้ | |
(ชนิดตัวแปรที่ต้อง return กลับ) (ชื่อ Function) (ตัวแปรที่ต้องการป้อนเข้าให้กับ Function) | |
float getAreaCircle (float radius) | |
*/ | |
//ทีนี้เขียนใหม่ให้มันถูกกับภาษา c++ จะได้ | |
float getAreaCircle(float radius){ | |
// เขียนคำสั่งของ Function ไว้ในนี้ | |
float area = 2*3.1413*(radius * radius); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// digitalWrite(PIN , STATUS); | |
// การเรียกใช้คำสั่งให้ pin ที่ 13 มี Output เป็น High และเป็น Low โดยมีการหน่วงเวลาไว้ก่อน 1000mS | |
digitalWrite(13,HIGH); | |
delay(1000); | |
digitalWrite(13,LOW); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int age = 20; | |
// แบบ 1 | |
if (age > 50) { | |
// ส่วนที่เอาไว้เขียนว่าจะให้ทำอะไรถ้าเงื่อนไขเป็นจริง | |
} | |
// แบบ 2 | |
if (age > 50) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int temperature = getTempFromSensor(); // อ่านค่าอุณหภูมิจาก sensor แล้วเอาเช็คเงื่อนไขเพื่อทำอะไรบางอย่าง | |
if (temperature < 20){ // ถ้าอุณหภูมิน้อยกว่า 20 ให้ปิดแอร์ หนาว!! | |
closeAirCondition(); | |
} else { // แต่ถ้าไม่ใช่ก็ให้เปิดแอร์ เริ่มร้อนไง | |
openAirCondition(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int group = 10; | |
int student = 5; | |
int totalStudentInClass = group * student; | |
float totalStudentInclass = 100.0; | |
float group = 5.0; | |
float studentInGroup = totalStudentInclass / group; // ที่ใช้ Float เพราะว่าในกรณีที่มีการหาร ข้อมูลที่ได้อาจจะมีทศนิยม | |
// เพราะฉะนั้นตัวแปรอื่นที่เอามาคำนวณด้วย ก็ควรจะเป็นตัวแปรแบบทศนิยมเช่นเดียวกัน |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int x = 10; | |
int y = -23; // สร้างตัวแปรแบบ จำนวนเต็ม | |
float z = 2.44; | |
float pi = 3.14; // สร้างตัวแปรแบบ ทศนิยม | |
char bloodGroup = 'B'; // สร้างตัวแปรแบบ ตัวอักษร ซึ่งจะใส่ตัวอักษรได้แค่ตัวเดียวเท่านั้นภายใต้เครื่องหมาย '' เช่น 'x' , 'b' | |
bool isDoorOpen = true; // สร้างตัวแปรแบบ boolean หรือ ตรรกะ ซึ่งจะมีค่าแค่ true กับ false หรือ 1 กับ 0 นั่นเอง |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%1.) Load logged data & create Data for System Identification | |
data = csvread('log_file.csv'); | |
t = data(:,1); | |
u = data(:,2); | |
y = data(:,3); | |
dt = t(2)-t(1); % sampling time | |
z = iddata(y, u, dt, 'Name', 'DC-motor'); |