-
Notifications
You must be signed in to change notification settings - Fork 0
/
environment_01.py
122 lines (82 loc) · 2.97 KB
/
environment_01.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from pybrain.rl.environments.environment import Environment
from scipy import zeros
import random
#Rasberry Pi imports
import RPi.GPIO as GPIO
import time
#SETUP OUTPUT
GPIO.setmode(GPIO.BOARD)
# DC motors
GPIO.setup(15, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
def Forward():
GPIO.output(15, 0)
GPIO.output(13, 1)
def Backward():
GPIO.output(15, 1)
GPIO.output(13, 0)
def Snooze():
GPIO.output(15, 0)
GPIO.output(13, 0)
#SETUP INPUT
#define the sensor pins that goes to the circuit
photoresistor_sensor = 7
pir_sensor = 11
#analog sensor to digital input pin
def rc_time(photoresistor_sensor):
count = 0
#Output on the pin for
GPIO.setup(photoresistor_sensor, GPIO.OUT)
GPIO.output(photoresistor_sensor, GPIO.LOW)
time.sleep(0.1)
#Change the pin back to input
GPIO.setup(photoresistor_sensor, GPIO.IN)
#Count until the pin goes high and cap the number at 1023
while (GPIO.input(photoresistor_sensor) == GPIO.LOW and count<1023):
count += 1
return count
#digital sensor to digital input pin
def PIR_sensing (pir_sensor):
GPIO.setup(pir_sensor, GPIO.IN)
current_state = 0
time.sleep(0.1)
current_state = GPIO.input(pir_sensor)
#return 1 or 0
if current_state == 1:
return 1
elif current_state == 0:
return 0
class Env(Environment):
""" Environment for RL our agent will be able to observe"""
# the number of action values the environment accepts - Foreward, Backward and Snooze
indim = 3
# the number of sensor values the environment produces - analog in photoresistor_sensor
outdim = 1024
def getSensors(self):
""" the currently visible state of the world (the observation may be stochastic - repeated calls returning different values)
:rtype: by default, this is assumed to be a numpy array of doubles
"""
sensor_value = rc_time(photoresistor_sensor) #sensor messurment
# return needs to be formated in such way to be digestable by Pybrain library, I think... otherwise there are errors
return [float(sensor_value),]
def performAction(self, action):
""" perform an action on the world that changes it's internal state (maybe stochastically).
:key action: an action that should be executed in the Environment.
:type action: by default, this is assumed to be a numpy array of doubles
"""
print ("Action performed: ", action)
if action == 1:
print ("I Walk")
Forward()
time.sleep(1)
elif action == 2:
print ("I Retreat")
Backward()
time.sleep(1)
else:
Snooze()
print ("I don't walk")
time.sleep(1)
def reset(self):
""" Most environments will implement this optional method that allows for reinitialization.
"""