-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
203 lines (158 loc) · 7.25 KB
/
main.py
File metadata and controls
203 lines (158 loc) · 7.25 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# Description: Program written by Team LionTech from
# Colegiul National "Mihai Eminescu" Oradea, Romania for
# Phase 2 of AstroPi Mission Space Lab competition 2021/2022
# Team Members: Elisa Erzse, Vlad Marian, Mihnea Popovici,
# Mădălin Toma, Andrei Droj
# Teacher: Amelia Stoian
# Libraries needed for experiment
from sense_hat import SenseHat
import datetime as dt
from time import sleep
from picamera import PiCamera
from ephem import readtle, degree
import os, csv
# SenseHat Initialisation
sense = SenseHat()
sense.clear()
# Starting the camera
cam = PiCamera()
cam.resolution = (1920, 1080)
# Mission parameters
missiontime = 175 # Running time in minutes
team = 'LionTech' # AstroPi team name
photo_counter = 1
photo_delay = 7.5
# Working path definition
dir_path = os.path.dirname(os.path.realpath(__file__))
# Datetime variables to store the start time and the current time
start_time = dt.datetime.now()
now_time = dt.datetime.now()
last_photo_time = dt.datetime.now()
file_time_stamp = start_time.strftime('%Y%b%d_%Hh%Mm%Ss')
# Create folders for images and logs
ltdata = dir_path + '/LTdata'
if (not os.path.exists(ltdata)):
os.mkdir(ltdata)
if (not os.path.exists(ltdata + "/logs/")):
os.mkdir(ltdata + "/logs/")
if (not os.path.exists(ltdata + "/images/")):
os.mkdir(ltdata + "/images/")
if (not os.path.exists(ltdata + '/images/{}'.format(file_time_stamp))):
os.mkdir(ltdata + '/images/{}'.format(file_time_stamp))
logfilename = ltdata + '/logs/LionTech_Log_{}.csv'.format(file_time_stamp)
errorfilename = ltdata + '/logs/Errors{}.txt'.format(file_time_stamp)
# Latest TLE data for ISS location
name = "ISS (ZARYA)"
line1 = "1 25544U 98067A 21044.74781537 .00000127 00000-0 10467-4 0 9995"
line2 = "2 25544 51.6433 233.2678 0002756 10.4033 94.8094 15.48960066269462"
# Retrieve ISS position
iss = readtle(name, line1, line2)
iss.compute()
def readAccelerations():
""" Function for reading the accelerations on the three axes.
The reading contains the acceleration on x, y and z-axis, in
variables rawAccelX, rawAccelY and rawAccelZ.
"""
sense.set_imu_config(False, False, True)
rawAccel = sense.get_accelerometer_raw()
rawAccelX = round(rawAccel['x'],3)
rawAccelY = round(rawAccel['y'],3)
rawAccelZ = round(rawAccel['z'],3)
return rawAccelX, rawAccelY, rawAccelZ
def readCompass():
""" Function for reading the data from the compass. The readings
contains both the direction of the North, in variable comp, and
the magnetic intensity on x, y and z-axis, in variables rawCompX,
rawCompY and rawCompZ.
"""
sense.set_imu_config(True, False, False)
comp = round(sense.get_compass(),3)
rawComp = sense.get_compass_raw()
rawCompX = round(rawComp['x'],3)
rawCompY = round(rawComp['y'],3)
rawCompZ = round(rawComp['z'],3)
return comp, rawCompX, rawCompY, rawCompZ
def readOrientation():
""" Function for reading the SenseHat orientation data. The readings
contain the pitch, roll and yaw as separate values.
"""
sense.set_imu_config(False, True, True)
rawAccel = sense.get_orientation()
pitch = round(rawAccel['pitch'],2)
roll = round(rawAccel['roll'],2)
yaw = round(rawAccel['yaw'],2)
return pitch, roll, yaw
def convert(angle):
""" Convert an ephem angle (degrees:minutes:seconds) to an
EXIF-appropriate representation (rationals). Return a tuple
containing a boolean and the converted angle, with the
boolean indicating if the angle is negative.
"""
degrees, minutes, seconds = (float(field) for field in str(angle).split(':'))
exif_angle = '{:.0f}/1,{:.0f}/1,{:.0f}/10'.format(abs(degrees), minutes, seconds*10)
return degrees < 0, exif_angle
def create_csv(data_file):
""" Creates the CSV file and the header of the log file.
"""
with open(data_file, 'w') as f:
writer = csv.writer(f)
header = ("Team","Timestamp","Longitude", "Latitude","Height","Temperature","Temp_from_pressure","Humidity","Pressure","AccelX","AccelY","AccelZ","CompassMag","CompassX","CompassY","CompassZ","Pitch","Roll","Yaw")
writer.writerow(header)
def add_csv_data(data_file, data):
""" Function to add data to the CSV log file.
"""
with open(data_file, 'a') as f:
writer = csv.writer(f)
writer.writerow(data)
# main programm
print('LionTech Mission Space Lab')
print('- Systems checked at: {}'.format(now_time.strftime('%Y-%b-%d %Hh%Mm%Ss')))
print('- Data collection started at: {}'.format(now_time.strftime('%Y-%b-%d %Hh%Mm%Ss')))
print('- Collecting data .................................')
create_csv(logfilename)
#print("Longitude, Latitude, Height,Temperature,temp_p,humidity,pressure,accel, compass, orient")
while (now_time < start_time + dt.timedelta(minutes=missiontime)):
try:
# Update the current time
sleep(0.25)
now_time = dt.datetime.now()
# Compute ISS location
iss.compute()
# Convert the latitude and longitude to EXIF-appropriate representations
south, exif_latitude = convert(iss.sublat)
west, exif_longitude = convert(iss.sublong)
height = round(iss.elevation,2)
# Read the temperature, humidity, pressure and other data from the SenseHat
temperature = round(sense.get_temperature(), 3)
temp_p = round(sense.get_temperature_from_pressure(), 3)
humidity = round(sense.get_humidity(), 3)
pressure = round(sense.get_pressure(), 3)
accelX, accelY, accelZ = readAccelerations()
compassMag, compassX, compassY, compassZ = readCompass()
pitch, roll, yaw = readOrientation()
lat = round(iss.sublat/degree, 5)
lon = round(iss.sublong/degree, 5)
# Zip readings in a single variable and write them to the csv
rowdata = (team, now_time,lon, lat, height, temperature,temp_p,humidity,pressure,accelX,accelY, accelZ, compassMag, compassX, compassY, compassZ, pitch, roll, yaw)
add_csv_data(logfilename, rowdata)
# print(team,now_time,lon,lat,temperature,temp_p,humidity,pressure,accelX,accelY, accelZ, compassMag, compassX, compassY, compassZ, pitch, roll, yaw)
# Convert the latitude and longitude to EXIF-appropriate representations
south, exif_latitude = convert(iss.sublat)
west, exif_longitude = convert(iss.sublong)
# Set the EXIF tags specifying the current location
cam.exif_tags['GPS.GPSLatitude'] = exif_latitude
cam.exif_tags['GPS.GPSLatitudeRef'] = 'S' if south else 'N'
cam.exif_tags['GPS.GPSLongitude'] = exif_longitude
cam.exif_tags['GPS.GPSLongitudeRef'] = 'W' if west else 'E'
# Image capture
if now_time > last_photo_time + dt.timedelta(seconds = photo_delay):
cam.capture(ltdata + '/images/{}/LTech_{:04d}.jpg'.format(file_time_stamp, photo_counter))
photo_counter +=1
last_photo_time = now_time
except Exception as e:
with open(errorfilename, 'w') as f:
f.write('Mission error: ' + str(e))
print('- Mission ended at: {}'.format(now_time.strftime('%Y-%b-%d %Hh%Mm%Ss')))
print('- Total runtime: {}'.format((now_time - start_time)))
print('Mission accomplished - Team LionTech')
sense.clear()