SMALL
아두이노 코드
#include "EmonLib.h"
EnergyMonitor emon1;
int relay1 = 2; //줄어듬
int relay2 = 3; //늘어남
int i = 1;
double W;
double MA;
void extendActuator() {
digitalWrite(relay1, HIGH);
digitalWrite(relay2, LOW);
}
void retractActuator() {
digitalWrite(relay1, LOW);
digitalWrite(relay2, HIGH);
}
void stopActuator() {
digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
}
void setup()
{
Serial.begin(9600);
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
emon1.current(A4, 111.1);
}
void loop()
{
//0:줄어듬, 1:늘어남, 2:정지
double Irms = emon1.calcIrms(1480);
W = Irms*220.0;
MA = Irms*1000;
char serial_data = Serial.read();
if(serial_data == '1'){
retractActuator();
}
else if(serial_data == '0'){
extendActuator();
}
else if(serial_data == '2'){
stopActuator();
}
//초반 5번은 값이 튀는 현상이 있어 출력X
if(i > 5){
Serial.println(MA);
}
i++;
}
파이썬 코드
from serial import Serial
from time import time
from datetime import datetime
def sensor_log() :
thres = 300
is_end = None
is_start = False
dateformat = "%Y-%m-%d %H:%M:%S"
command_input = True
final = None
is_final = None
arduino = Serial(
port='COM6', # Window
baudrate=9600 # 보드 레이트 (통신 속도)
)
while True:
if arduino.readable() :
ma = arduino.readline().decode()
ma = float(ma.strip('\n').strip('\r'))
if command_input == True and final != '3':
while True:
command = input('아두이노에게 내릴 명령 (0-후진, 1-전진, 2-정지): ')
if command == '0':
length_var = '0'
elif command == '1':
length_var = '1'
if command == '0' or command =='1' or command =='2':
break
if command == '0' or command == '1':
final = input('아두이노에게 내릴 명령 (3-끝까지): ')
arduino.write(command.encode())
command_input = False
if ma >= thres and not is_start:
start = time()
start_time = datetime.now().strftime(dateformat)
is_start = True
is_end = True
is_final = False
command_input = True
if ma < thres and is_end:
end = time()
end_time = datetime.now().strftime(dateformat)
act_time = round(end-start, 2)
if length_var == '1':
length = act_time * 11.441647597254
elif length_var == '0':
length = act_time * 11.76470588235294
length = round(length, 2)
save(start_time, end_time, act_time, length)
print(f'{start_time} ~ {end_time} {act_time}초 {length}mm')
is_end = False
is_start = False
is_final = True
command_input = True
if is_final == True:
final = None
is_final = False
def save(start_time, end_time, act_time, length):
file = open('./activate_time.txt', 'a', encoding='UTF-8')
file.write(f'{start_time} ~ {end_time} {act_time}초 {length}mm\n')
sensor_log()
파이썬에서 값을 아두이노로 넘겨주면 액추에이터가 작동되는 방식입니다.
측정이 될때마다 txt파일로 작동 결과를 저장하는 형식입니다.
작동 결과는 액추에이터의 "시작시간 ~ 종료시간 작동시간 작동거리"가 나옵니다
시작시간과 종료시간은 파이썬 내에서 측정하였고 작동시간 측정은 아두이노 전류측정센서를 이용하여
전류값이 특정 범위를 뛰어넘었다가 다시 내려오는 시간으로 측정하였습니다.
시작시간과 종료시간은 아두이노에서 측정을 한게 아니다보니 약간의 오차가 발생할 수 있습니다.
또한 액추에이터의 작동시간이 매번 다릅니다.
제가 사용한 액추에이터는 늘어나는 길이가 300mm인데 끝까지 늘어날때 26.22초 ~ 26.8초 정도를 왔다갔다하고
줄어들때는 25.5초 정도 걸려서 정확한 작동거리 측정이 불가합니다.
작동이 될때 코드에서 오류가 있습니다.
예를 들면 전진을 하는데 전진을 또 입력한다면 제대로 작동하지 않습니다.
혹은 움직이지도 않는데 정지 신호를 입력한다면 제대로 작동하지 않습니다.
코드 작성하는데 논리부분에서 어려움을 느껴서 좀 더 생각해보고 수정을 해야할꺼같습니다.
출력 형식입니다.
2022-08-25 01:23:42 ~ 2022-08-25 01:23:44 2.4초 14.12mm
2022-08-25 01:24:39 ~ 2022-08-25 01:24:41 2.16초 12.71mm
2022-08-25 01:24:55 ~ 2022-08-25 01:24:59 4.09초 24.06mm
2022-08-25 01:26:45 ~ 2022-08-25 01:27:06 21.4초 121.32mm
2022-08-25 01:27:44 ~ 2022-08-25 01:28:10 25.74초 151.41mm
2022-08-25 01:28:43 ~ 2022-08-25 01:28:47 4.33초 24.55mm
2022-08-25 01:28:51 ~ 2022-08-25 01:28:55 4.09초 24.06mm
2022-08-25 01:29:06 ~ 2022-08-25 01:29:11 4.81초 27.27mm
2022-08-25 01:29:18 ~ 2022-08-25 01:29:22 4.09초 24.06mm
2022-08-25 01:29:27 ~ 2022-08-25 01:29:53 25.74초 145.92mm
2022-08-25 01:36:22 ~ 2022-08-25 01:36:48 25.74초 151.41mm
2022-08-25 01:39:22 ~ 2022-08-25 01:39:24 1.92초 10.88mm
2022-08-25 01:47:15 ~ 2022-08-25 01:47:17 1.44초 8.16mm
2022-08-25 01:47:23 ~ 2022-08-25 01:47:30 7.22초 42.47mm
2022-08-25 01:47:43 ~ 2022-08-25 01:47:45 1.44초 8.16mm
2022-08-25 01:49:17 ~ 2022-08-25 01:49:42 25.01초 141.78mm
2022-08-25 01:49:54 ~ 2022-08-25 01:50:20 25.5초 150.0mm
2022-08-25 01:50:27 ~ 2022-08-25 01:50:53 26.21초 148.58mm
2022-08-25 01:53:04 ~ 2022-08-25 01:53:29 25.49초 299.88mm
2022-08-25 01:53:42 ~ 2022-08-25 01:54:08 26.22초 297.28mm
LIST
'아두이노' 카테고리의 다른 글
[아두이노] 액추에이터와 릴레이모듈 적용 (0) | 2022.08.27 |
---|---|
[아두이노] L298N 드라이버 보드 모듈과 LinerActuator 연결하기 (0) | 2022.08.10 |