http://www.saeon.co.kr/main/sub_page.php?page_idx=92
https://www.altinocoding.com/lessons-1-10
http://cars.altinocoding.com/
www.anaconda.com/products/individual
pip install pyserial
import time #운영 체제의 시간 관련 모듈
import serial #블루투스 시리얼 통신을 위한 모듈
from timeit import default_timer as timer #
import threading # 쓰레드 생성하기 위한 모듈
te=0
tx_d = [2,28,35,1,10,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3]
rx_data= [0,0,0,0,0,0,0,0,0,0];
rx_d = []
rx_d56 = [0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0]
rx_dsensor1 = [0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0]
rx_dsensor2 = [0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0]
speed = 0
connectstate = 0
ser = serial.Serial() #B
ser.baudrate = 115200 #Bluetooth serial baudrate
rxcnt=0
class SensorData :
IR = [0,0,0,0,0,0,0]
Tor = [0,0,0]
Temp = 0
CDS = 0
GSensor = [0,0,0,0]
MSensor = [0,0,0,0]
GySensor = [0,0,0,0]
StVar = 0
StTor = 0
Bat = 0
Remote = 0
MTemp = 0
def delay(ms):
time.sleep(ms/1000)
return
def check():
tx_d[26]=255
te=tx_d[1]
ircheck =tx_d[21] | 0x01
tx_d[21]=ircheck
for i in range(3,28):
te=te+tx_d[i]
te=te%256
tx_d[2]=te
ser.write(bytearray(tx_d))
return
def Go(left, right):
if left>1000:
left=1000
if left<-1000:
left=-1000
if right>1000:
right=1000
if right<-1000:
right=-1000
if left<0:
left=32768-left
if right<0:
right=32768-right
tx_d[7]=int(right//256)
tx_d[8]=int(right%256)
tx_d[10]=int(left//256)
tx_d[11]=int(left%256)
#check()
return
def Display(dot):
if type(dot)==int :
if dot > 128 :
dot=dot-128
elif dot>0:
dot=128+dot
tx_d[12]=dot
else :
tx_d[12]=128+ord(dot)
tx_d[13]=0
tx_d[14]=0
tx_d[15]=0
tx_d[16]=0
tx_d[17]=0
tx_d[18]=0
tx_d[19]=0
tx_d[20]=0
#check()
return
def DisplayLine(dot1, dot2, dot3, dot4, dot5, dot6, dot7, dot8):
tx_d[12]=0
tx_d[13]=dot8
tx_d[14]=dot7
tx_d[15]=dot6
tx_d[16]=dot5
tx_d[17]=dot4
tx_d[18]=dot3
tx_d[19]=dot2
tx_d[20]=dot1
#check()
return
def Displayon(x, y) :
tx_d[12]=0
dotx=tx_d[21-x]
tx_d[21-x]=dotx | pow(2,(y-1))
#check()
return
def Displayoff(x, y) :
tx_d[12]=0
dotx=tx_d[21-x]
tx_d[21-x]= dotx & (255-pow(2,(y-1)))
#check()
return
def Sound(buzzer) :
tx_d[22]=buzzer
#check()
return
def Light(led) :
tx_d[21]=int(led//256)
tx_d[23]=int(led%256)
# check()
return
def Led(led):
Light(led)
return
def Steering(st):
tx_d[24]=0
tx_d[5]=st
#check()
return
def Steering2(value1, value2):
if value1>127:
value1=127
if value1<-127:
value1=-127
if value2>127:
value2=127
if value2<-127:
value1=-127
if value1<0:
value1=128-value1
if value2<0:
value2=128-value2
tx_d[24]=1
tx_d[5]=value1
tx_d[25]=value2
#check()
return
def Steering3(value1):
if value1>127:
value1=127
if value1<-127:
value1=-127
if value1<0:
value1=128-value1
tx_d[24]=1
tx_d[5]=value1
tx_d[25]=0
#check()
return
def checkdata56():
rx_check_sum=0
if rx_d56[0]==2 and rx_d56[55] == 3 and rx_d56[1] == 56:
rx_check_sum = rx_d56[0]
rx_check_sum = rx_check_sum + rx_d56[1]
rx_check_sum = rx_check_sum + rx_d56[3]
rx_check_sum = rx_check_sum + rx_d56[4]
rx_check_sum = rx_check_sum + rx_d56[5]
rx_check_sum = rx_check_sum + rx_d56[6]
rx_check_sum = rx_check_sum + rx_d56[7]
rx_check_sum = rx_check_sum + rx_d56[8]
rx_check_sum = rx_check_sum + rx_d56[9]
rx_check_sum = rx_check_sum + rx_d56[10]
rx_check_sum = rx_check_sum + rx_d56[11]
rx_check_sum = rx_check_sum + rx_d56[12]
rx_check_sum = rx_check_sum + rx_d56[13]
rx_check_sum = rx_check_sum + rx_d56[14]
rx_check_sum = rx_check_sum + rx_d56[15]
rx_check_sum = rx_check_sum + rx_d56[16]
rx_check_sum = rx_check_sum + rx_d56[17]
rx_check_sum = rx_check_sum + rx_d56[18]
rx_check_sum = rx_check_sum + rx_d56[19]
rx_check_sum = rx_check_sum + rx_d56[20]
rx_check_sum = rx_check_sum + rx_d56[21]
rx_check_sum = rx_check_sum + rx_d56[22]
rx_check_sum = rx_check_sum + rx_d56[23]
rx_check_sum = rx_check_sum + rx_d56[24]
rx_check_sum = rx_check_sum + rx_d56[25]
rx_check_sum = rx_check_sum + rx_d56[26]
rx_check_sum = rx_check_sum + rx_d56[27]
rx_check_sum = rx_check_sum + rx_d56[28]
rx_check_sum = rx_check_sum + rx_d56[29]
rx_check_sum = rx_check_sum + rx_d56[30]
rx_check_sum = rx_check_sum + rx_d56[31]
rx_check_sum = rx_check_sum + rx_d56[32]
rx_check_sum = rx_check_sum + rx_d56[33]
rx_check_sum = rx_check_sum + rx_d56[34]
rx_check_sum = rx_check_sum + rx_d56[35]
rx_check_sum = rx_check_sum + rx_d56[36]
rx_check_sum = rx_check_sum + rx_d56[37]
rx_check_sum = rx_check_sum + rx_d56[38]
rx_check_sum = rx_check_sum + rx_d56[39]
rx_check_sum = rx_check_sum + rx_d56[40]
rx_check_sum = rx_check_sum + rx_d56[41]
rx_check_sum = rx_check_sum + rx_d56[42]
rx_check_sum = rx_check_sum + rx_d56[43]
rx_check_sum = rx_check_sum + rx_d56[44]
rx_check_sum = rx_check_sum + rx_d56[45]
rx_check_sum = rx_check_sum + rx_d56[46]
rx_check_sum = rx_check_sum + rx_d56[47]
rx_check_sum = rx_check_sum + rx_d56[48]
rx_check_sum = rx_check_sum + rx_d56[49]
rx_check_sum = rx_check_sum + rx_d56[50]
rx_check_sum = rx_check_sum + rx_d56[51]
rx_check_sum = rx_check_sum + rx_d56[52]
rx_check_sum = rx_check_sum + rx_d56[53]
rx_check_sum = rx_check_sum + rx_d56[54]
rx_check_sum = rx_check_sum + rx_d56[55]
rx_check_sum = rx_check_sum % 256
if rx_check_sum == rx_d56[2]:
rx_dsensor1[7]=rx_d56[7]
rx_dsensor1[8]=rx_d56[8]
rx_dsensor1[9]=rx_d56[9]
rx_dsensor1[10]=rx_d56[10]
rx_dsensor1[11]=rx_d56[11]
rx_dsensor1[12]=rx_d56[12]
rx_dsensor1[13]=rx_d56[13]
rx_dsensor1[14]=rx_d56[14]
rx_dsensor1[15]=rx_d56[15]
rx_dsensor1[16]=rx_d56[16]
rx_dsensor1[17]=rx_d56[17]
rx_dsensor1[18]=rx_d56[18]
rx_dsensor1[19]=rx_d56[19]
rx_dsensor1[20]=rx_d56[20]
rx_dsensor1[21]=rx_d56[21]
rx_dsensor1[22]=rx_d56[22]
rx_dsensor1[23]=rx_d56[49]
rx_dsensor1[24]=rx_d56[50]
rx_dsensor1[25]=rx_d56[43]
rx_dsensor1[26]=rx_d56[44]
rx_dsensor2[7]=rx_d56[25]
rx_dsensor2[8]=rx_d56[26]
rx_dsensor2[9]=rx_d56[27]
rx_dsensor2[10]=rx_d56[28]
rx_dsensor2[11]=rx_d56[29]
rx_dsensor2[12]=rx_d56[30]
rx_dsensor2[13]=rx_d56[31]
rx_dsensor2[14]=rx_d56[32]
rx_dsensor2[15]=rx_d56[33]
rx_dsensor2[16]=rx_d56[34]
rx_dsensor2[17]=rx_d56[35]
rx_dsensor2[18]=rx_d56[36]
rx_dsensor2[19]=rx_d56[45]
rx_dsensor2[20]=rx_d56[46]
rx_dsensor2[21]=rx_d56[23]
rx_dsensor2[22]=rx_d56[24]
rx_dsensor2[23]=rx_d56[47]
rx_dsensor2[24]=rx_d56[48]
rx_dsensor2[25]=rx_d56[51]
rx_dsensor2[26]=rx_d56[52]
rx_dsensor2[27]=rx_d56[37]
rx_dsensor2[28]=rx_d56[38]
rx_dsensor2[29]=rx_d56[39]
rx_dsensor2[30]=rx_d56[40]
rx_dsensor2[5]=rx_d56[41]
rx_dsensor2[6]=rx_d56[42]
def SensorFun(command) :
tx_d[4]=command
check()
rx_d56=ser.read(56)
rx_check_sum=0
if rx_d56[0]==2 and rx_d56[55] == 3 and rx_d56[1] == 56 :
rx_check_sum = rx_d56[0]
rx_check_sum = rx_check_sum + rx_d56[1]
rx_check_sum = rx_check_sum + rx_d56[3]
rx_check_sum = rx_check_sum + rx_d56[4]
rx_check_sum = rx_check_sum + rx_d56[5]
rx_check_sum = rx_check_sum + rx_d56[6]
rx_check_sum = rx_check_sum + rx_d56[7]
rx_check_sum = rx_check_sum + rx_d56[8]
rx_check_sum = rx_check_sum + rx_d56[9]
rx_check_sum = rx_check_sum + rx_d56[10]
rx_check_sum = rx_check_sum + rx_d56[11]
rx_check_sum = rx_check_sum + rx_d56[12]
rx_check_sum = rx_check_sum + rx_d56[13]
rx_check_sum = rx_check_sum + rx_d56[14]
rx_check_sum = rx_check_sum + rx_d56[15]
rx_check_sum = rx_check_sum + rx_d56[16]
rx_check_sum = rx_check_sum + rx_d56[17]
rx_check_sum = rx_check_sum + rx_d56[18]
rx_check_sum = rx_check_sum + rx_d56[19]
rx_check_sum = rx_check_sum + rx_d56[20]
rx_check_sum = rx_check_sum + rx_d56[21]
rx_check_sum = rx_check_sum + rx_d56[22]
rx_check_sum = rx_check_sum + rx_d56[23]
rx_check_sum = rx_check_sum + rx_d56[24]
rx_check_sum = rx_check_sum + rx_d56[25]
rx_check_sum = rx_check_sum + rx_d56[26]
rx_check_sum = rx_check_sum + rx_d56[27]
rx_check_sum = rx_check_sum + rx_d56[28]
rx_check_sum = rx_check_sum + rx_d56[29]
rx_check_sum = rx_check_sum + rx_d56[30]
rx_check_sum = rx_check_sum + rx_d56[31]
rx_check_sum = rx_check_sum + rx_d56[32]
rx_check_sum = rx_check_sum + rx_d56[33]
rx_check_sum = rx_check_sum + rx_d56[34]
rx_check_sum = rx_check_sum + rx_d56[35]
rx_check_sum = rx_check_sum + rx_d56[36]
rx_check_sum = rx_check_sum + rx_d56[37]
rx_check_sum = rx_check_sum + rx_d56[38]
rx_check_sum = rx_check_sum + rx_d56[39]
rx_check_sum = rx_check_sum + rx_d56[40]
rx_check_sum = rx_check_sum + rx_d56[41]
rx_check_sum = rx_check_sum + rx_d56[42]
rx_check_sum = rx_check_sum + rx_d56[43]
rx_check_sum = rx_check_sum + rx_d56[44]
rx_check_sum = rx_check_sum + rx_d56[45]
rx_check_sum = rx_check_sum + rx_d56[46]
rx_check_sum = rx_check_sum + rx_d56[47]
rx_check_sum = rx_check_sum + rx_d56[48]
rx_check_sum = rx_check_sum + rx_d56[49]
rx_check_sum = rx_check_sum + rx_d56[50]
rx_check_sum = rx_check_sum + rx_d56[51]
rx_check_sum = rx_check_sum + rx_d56[52]
rx_check_sum = rx_check_sum + rx_d56[53]
rx_check_sum = rx_check_sum + rx_d56[54]
rx_check_sum = rx_check_sum + rx_d56[55]
rx_check_sum = rx_check_sum % 256
if rx_check_sum == rx_d56[2]:
rx_dsensor1[7]=rx_d56[7]
rx_dsensor1[8]=rx_d56[8]
rx_dsensor1[9]=rx_d56[9]
rx_dsensor1[10]=rx_d56[10]
rx_dsensor1[11]=rx_d56[11]
rx_dsensor1[12]=rx_d56[12]
rx_dsensor1[13]=rx_d56[13]
rx_dsensor1[14]=rx_d56[14]
rx_dsensor1[15]=rx_d56[15]
rx_dsensor1[16]=rx_d56[16]
rx_dsensor1[17]=rx_d56[17]
rx_dsensor1[18]=rx_d56[18]
rx_dsensor1[19]=rx_d56[19]
rx_dsensor1[20]=rx_d56[20]
rx_dsensor1[21]=rx_d56[21]
rx_dsensor1[22]=rx_d56[22]
rx_dsensor1[23]=rx_d56[49]
rx_dsensor1[24]=rx_d56[50]
rx_dsensor1[25]=rx_d56[43]
rx_dsensor1[26]=rx_d56[44]
rx_dsensor2[7]=rx_d56[25]
rx_dsensor2[8]=rx_d56[26]
rx_dsensor2[9]=rx_d56[27]
rx_dsensor2[10]=rx_d56[28]
rx_dsensor2[11]=rx_d56[29]
rx_dsensor2[12]=rx_d56[30]
rx_dsensor2[13]=rx_d56[31]
rx_dsensor2[14]=rx_d56[32]
rx_dsensor2[15]=rx_d56[33]
rx_dsensor2[16]=rx_d56[34]
rx_dsensor2[17]=rx_d56[35]
rx_dsensor2[18]=rx_d56[36]
rx_dsensor2[19]=rx_d56[45]
rx_dsensor2[20]=rx_d56[46]
rx_dsensor2[21]=rx_d56[23]
rx_dsensor2[22]=rx_d56[24]
rx_dsensor2[23]=rx_d56[47]
rx_dsensor2[24]=rx_d56[48]
rx_dsensor2[25]=rx_d56[51]
rx_dsensor2[26]=rx_d56[52]
rx_dsensor2[27]=rx_d56[37]
rx_dsensor2[28]=rx_d56[38]
rx_dsensor2[29]=rx_d56[39]
rx_dsensor2[30]=rx_d56[40]
rx_dsensor2[5]=rx_d56[41]
rx_dsensor2[6]=rx_d56[42]
sensordata1 = SensorData
sensordata1.IR[0]=rx_dsensor1[7] * 256 + rx_dsensor1[8]
sensordata1.IR[1]=rx_dsensor1[9] * 256 + rx_dsensor1[10]
sensordata1.IR[2]=rx_dsensor1[11] * 256 + rx_dsensor1[12]
sensordata1.IR[3]=rx_dsensor1[13] * 256 + rx_dsensor1[14]
sensordata1.IR[4]=rx_dsensor1[15] * 256 + rx_dsensor1[16]
sensordata1.IR[5]=rx_dsensor1[17] * 256 + rx_dsensor1[18]
sensordata1.Tor[0]=rx_dsensor1[19] * 256 + rx_dsensor1[20]
sensordata1.Tor[1]=rx_dsensor1[21] * 256 + rx_dsensor1[22]
sensordata1.Temp = rx_dsensor1[23] * 256 + rx_dsensor1[24]
sensordata1.CDS = rx_dsensor1[25] * 256 + rx_dsensor1[26]
sensordata1.GSensor[0]=rx_dsensor2[7] * 256 + rx_dsensor2[8]
sensordata1.GSensor[1]=rx_dsensor2[9] * 256 + rx_dsensor2[10]
sensordata1.GSensor[2]=rx_dsensor2[11] * 256 + rx_dsensor2[12]
temp=0
temp=sensordata1.GSensor[0]//16
sensordata1.GSensor[0]=temp
temp=0
temp=sensordata1.GSensor[1]//16
sensordata1.GSensor[1]=temp
temp=0
temp=sensordata1.GSensor[2]//16
sensordata1.GSensor[2]=temp
if sensordata1.GSensor[0]>2048 :
sensordata1.GSensor[0]=sensordata1.GSensor[0]- 4096
if sensordata1.GSensor[1]>2048 :
sensordata1.GSensor[1]=sensordata1.GSensor[1]-4096
if sensordata1.GSensor[2]>2048 :
sensordata1.GSensor[2]=sensordata1.GSensor[2]-4096
sensordata1.MSensor[0]=rx_dsensor2[13] * 256 + rx_dsensor2[14]
sensordata1.MSensor[1]=rx_dsensor2[15] * 256 + rx_dsensor2[16]
sensordata1.MSensor[2]=rx_dsensor2[17] * 256 + rx_dsensor2[18]
sensordata1.StVar = rx_dsensor2[19] * 256 + rx_dsensor2[20]
sensordata1.StTor = rx_dsensor2[21] * 256 + rx_dsensor2[22]
sensordata1.Bat = rx_dsensor2[23] * 256 + rx_dsensor2[24]
sensordata1.Remote = rx_dsensor2[25]
sensordata1.MTemp = rx_dsensor2[26]
sensordata1.GySensor[0]=rx_dsensor2[27] * 256 + rx_dsensor2[28]
sensordata1.GySensor[1]=rx_dsensor2[29] * 256 + rx_dsensor2[30]
sensordata1.GySensor[2]=rx_dsensor2[5] * 256 + rx_dsensor2[6]
if sensordata1.MSensor[0]>32768 :
sensordata1.MSensor[0]=sensordata1.MSensor[0]-65535
if sensordata1.MSensor[1]>32768 :
sensordata1.MSensor[1]=sensordata1.MSensor[1]-65535
if sensordata1.MSensor[2]>32768 :
sensordata1.MSensor[2]=sensordata1.MSensor[2]-65535
if sensordata1.GySensor[0]>32768 :
sensordata1.GySensor[0]=sensordata1.GySensor[0]-65535
if sensordata1.GySensor[1]>32768 :
sensordata1.GySensor[1]=sensordata1.GySensor[1]-65535
if sensordata1.GySensor[2]>32768 :
sensordata1.GySensor[2]=sensordata1.GySensor[2]-65535
return sensordata1
sensor=SensorData
def startTimer():
global connectstate
global rxcnt
if connectstate == 1 :
try :
timer = threading.Timer(0.08, startTimer)
timer.start()
sensor = SensorFun(10)
except KeyboardInterrupt:
print("kkkkkkkkkkkkkkkk")
Close()
def Open(portName='') :
global connectstate
global ser
ser.port=portName #Bluetooth serial port
ser.open()
connectstate = 1
delay(1000)
startTimer()
print("Bluetooth connect")
def Close() :
global connectstate
delay(1000)
connectstate = 0
ser.close()
print("Bluetooth disconnect")
from Altino import *
Open('com7')
#핸들 왼쪽으로 최대
Steering3(-127)
Go(300,300)
delay(1000)
Go(0,0)
#핸들 중앙으로
Steering3(0)
Close()
from Altino import *
Open('com7')
while 1:
try :
#조도값
if sensor.CDS < 100 :
Sound(37)
else :
Sound(0)
except :
break
Close()
from Altino import *
Open('com7')
while 1:
try :
if sensor.IR[1] >300 :
Sound(37)
else :
Sound(0)
except :
break
Close()
1.1.1.1. AI 자율주행 자동차 로봇 2번 적외선 센서로 조향제어하기[훈련]
from keras.models import Sequential
from keras.layers import Dense, Activation
import numpy as np
x_train = [0, 3, 6, 10, 15, 25, 50, 90, 200, 700, 800, 900]
y_train = [1, 10, 20, 40, 60, 80, 100, 110, 120, 120, 120, 120]
print("x", x_train)
print("y", y_train)
model = Sequential()
model.add(Dense(2, activation='relu', input_dim=1))
model.add(Dense(100))
model.add(Dense(10))
model.add(Dense(1, activation='linear'))
model.compile(loss='mse', optimizer='Adam', metrics=['accuracy'])
model.summary()
model.fit(x_train, y_train, batch_size=10, epochs=1000)
loss_and_metrics = model.evaluate(x_train, y_train, batch_size=5)
print('## evaluation loss and_metrics ##')
print(loss_and_metrics)
print(model.predict(np.array([500])))
model.save('model3_2.h5')
AI 자율주행 자동차 로봇 2번 적외선 센서로 조향제어하기[실행]
from keras.models import load_model
from Altino import *
import numpy as np
model = load_model('model3_2.h5')
Open('com7')
result = 0
while 1:
try :
# 중앙 센서에 값이 커지면 핸들을 오른쪽으로 돌림
result = model.predict(np.array([sensor.IR[1]]))
Steering3(int(result))
except :
break
Close()
AI 자율주행 자동차 로봇 2번 적외선 센서로 조향제어하기[훈련]
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras import optimizers
import matplotlib.pyplot as plt
import numpy as np
data = np.loadtxt("STEERING44.csv", delimiter=",", dtype=np.int16)
x_train = data[:, 0:5]
y_train = data[:, 5:12]
print("x", x_train)
print("y", y_train)
model = Sequential()
model.add(Dense(2, activation='linear', input_dim=5))
model.add(Dense(4))
model.add(Dense(44))
model.add(Dense(4))
model.add(Dense(7))
model.add(Activation('sigmoid'))
sgd=optimizers.SGD(lr=0.00001)
model.compile(optimizer=sgd, loss='mean_squared_error', metrics=['accuracy'])
model.summary()
history = model.fit(x_train, y_train, epochs=200000, batch_size=10)
plt.plot(history.history['loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
loss_and_metrics = model.evaluate(x_train, y_train, batch_size=32)
print('## evaluation loss and_metrics ##')
print(loss_and_metrics)
y_predict = model.predict(x_train)
print(y_predict)
print(y_predict[0,0])
ret=model.predict(np.array([[0, 0, 50, 50, 0]]))
print(max(ret[0,0:6]))
model.save('model_2ch_14_3.h5')
AI 자율주행 자동차 로봇 2번 적외선 센서로 조향제어하기[실행]
from keras.models import load_model
from Altino import *
import numpy as np
model = load_model('model_2ch_14_3.h5')
Open('com3')
result = 0
while 1:
try :
result = model.predict(np.array([[sensor.IR[0], sensor.IR[1], sensor.IR[2], sensor.IR[3], sensor.IR[4]]]))
if result[0,0] == max(result[0,0:5]) :
Steering3(0)
elif result[0,1] == max(result[0,0:5]) :
Steering3(60)
elif result[0,2] == max(result[0,0:5]) :
Steering3(125)
elif result[0,3] == max(result[0,0:5]) :
Steering3(-60)
elif result[0,4] == max(result[0,0:5]) :
Steering3(-125)
if result[0,5]>result[0,6] :
Go(300,300)
else :
Go(-350,-350)
except :
Go(0,0)
Steering3(0)
break
Close()
AI 자율주행 자동차 로봇 2번 적외선 센서로 조향제어하기[훈련]
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras import optimizers
import matplotlib.pyplot as plt
import numpy as np
data = np.loadtxt("STEERING44.csv", delimiter=",", dtype=np.int16)
x_train = data[:, 0:5]
y_train = data[:, 5:12]
print("x", x_train)
print("y", y_train)
model = Sequential()
model.add(Dense(2, activation='linear', input_dim=5))
model.add(Dense(4))
model.add(Dense(44))
model.add(Dense(4))
model.add(Dense(7))
model.add(Activation('sigmoid'))
sgd=optimizers.SGD(lr=0.00001)
model.compile(optimizer=sgd, loss='mean_squared_error', metrics=['accuracy'])
model.summary()
history = model.fit(x_train, y_train, epochs=1000, batch_size=10)
plt.plot(history.history['loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
loss_and_metrics = model.evaluate(x_train, y_train, batch_size=32)
print('## evaluation loss and_metrics ##')
print(loss_and_metrics)
y_predict = model.predict(x_train)
print(y_predict)
print(y_predict[0,0])
ret=model.predict(np.array([[0, 0, 50, 50, 0]]))
print(max(ret[0,0:6]))
model.save('model_2ch_14_3.h5')
AI 자율주행 자동차 로봇 2번 적외선 센서로 조향제어하기[실행]
from keras.models import load_model
from Altino import *
import numpy as np
model = load_model('model_2ch_14_3.h5')
Open('com7')
result = 0
while 1:
try :
result = model.predict(np.array([[sensor.IR[0], sensor.IR[1], sensor.IR[2], sensor.IR[3], sensor.IR[4]]]))
if result[0,0] == max(result[0,0:5]) :
Steering3(0)
elif result[0,1] == max(result[0,0:5]) :
Steering3(60)
elif result[0,2] == max(result[0,0:5]) :
Steering3(125)
elif result[0,3] == max(result[0,0:5]) :
Steering3(-60)
elif result[0,4] == max(result[0,0:5]) :
Steering3(-125)
if result[0,5]>result[0,6] :
Go(300,300)
else :
Go(-350,-350)
except :
Go(0,0)
Steering3(0)
break
Close()
'푸닥거리' 카테고리의 다른 글
IIS on windows 10 (0) | 2020.12.26 |
---|---|
파이썬을 활용한 로봇제어 응용 (0) | 2020.12.06 |
자바스크립트 핵심과 소켓 프로그래밍 (0) | 2020.11.15 |
chrome 에서 css 비활성화하기 (0) | 2020.09.29 |
Please submit a new appeal and make sure that the link is publicly browsable (0) | 2020.09.15 |
댓글