Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cs437-lab2-code
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
dl35
cs437-lab2-code
Commits
dce8224d
Commit
dce8224d
authored
1 week ago
by
jinyuxu2
Browse files
Options
Downloads
Patches
Plain Diff
Edit wifi_server.py
parent
5ca20625
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
wifi_server.py
+8
-54
8 additions, 54 deletions
wifi_server.py
with
8 additions
and
54 deletions
wifi_server.py
+
8
−
54
View file @
dce8224d
...
...
@@ -7,14 +7,14 @@ import threading
import
numpy
as
np
HOST
=
"
192.168.31.81
"
# IP address of your Raspberry PI
PORT
=
65432
# Port to listen on (non-privileged ports are > 1023)
HOST
=
"
192.168.31.81
"
PORT
=
65432
SPEED
=
20
# default speed
cur_speed
=
SPEED
Forward_count
=
0
Backward_count
=
0
car_status
=
{
# "battery": 0.0,
"
direction
"
:
"
stop
"
,
"
temperature
"
:
0.0
,
"
speed
"
:
0.0
,
...
...
@@ -22,12 +22,10 @@ car_status = {
"
timestamp
"
:
0
}
# Flag to control the background thread
running
=
True
def
get_battery_level
():
voltage
=
fc
.
power_read
()
# Read voltage from picar_4wd
# Map voltage to percentage (adjust min/max values for your battery)
voltage
=
fc
.
power_read
()
min_voltage
=
6.0
max_voltage
=
8.4
percentage
=
((
voltage
-
min_voltage
)
/
(
max_voltage
-
min_voltage
))
*
100
...
...
@@ -35,20 +33,11 @@ def get_battery_level():
def
get_pi_temperature
():
# Read CPU temperature from system file
temp
=
os
.
popen
(
"
vcgencmd measure_temp
"
).
readline
()
temp
=
float
(
temp
.
replace
(
"
temp=
"
,
""
).
replace
(
"'
C
"
,
""
))
return
temp
# def estimate_speed():
# # This is just an approximation - ideally you'd use wheel encoders
# # You might need to adjust this based on your specific motor settings
# # This assumes fc.speed gives a value between -100 and 100
# motor_speed = fc.speed # You may need to adjust this based on your library
# # Convert motor power level to approximate cm/s
# # This conversion factor (0.5) needs calibration for your specific car
# speed_cms = np.absolute(motor_speed) * 0.5
# return round(speed_cms, 2)
def
update_car_status
():
global
car_status
...
...
@@ -57,27 +46,19 @@ def update_car_status():
while
running
:
current_time
=
time
.
time
()
# elapsed_time = current_time - last_update_time
# Update battery level
# car_status["battery"] = get_battery_level()
# Update temperature
car_status
[
"
temperature
"
]
=
get_pi_temperature
()
# Update speedw
# current_speed = estimate_speed()
car_status
[
"
speed
"
]
=
cur_speed
# Calculate distance traveled (speed * time)
distance_increment
=
(
Forward_count
+
Backward_count
)
*
0.03
car_status
[
"
distance_traveled
"
]
=
distance_increment
car_status
[
"
timestamp
"
]
=
int
(
current_time
)
last_update_time
=
current_time
time
.
sleep
(
0.1
)
# Update 10 times per second
time
.
sleep
(
0.1
)
# Start the background thread to update car status
status_thread
=
threading
.
Thread
(
target
=
update_car_status
)
status_thread
.
daemon
=
True
status_thread
.
start
()
...
...
@@ -120,31 +101,9 @@ def car_movement(direction):
print
(
"
Stopping car
"
)
fc
.
stop
()
# You might want to return some status information
return
direction
# with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
# s.bind((HOST, PORT))
# s.listen()
# # fc.cpu_temperature()
# try:
# while 1:
# client, clientInfo = s.accept()
# print("server recv from: ", clientInfo)
# data = client.recv(1024) # receive 1024 Bytes of message in binary format
# if data != b"":
# print(f"Received: {data}")
# # Convert bytes to integer, removing trailing \r\n
# direction = int(data.decode().strip())
# status = car_movement(direction)
# # Send back status information
# client.sendall(f"Received command: {direction}, {status}".encode())
# except:
# print("Closing socket")
# client.close()
# s.close()
with
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
as
s
:
s
.
bind
((
HOST
,
PORT
))
...
...
@@ -177,12 +136,7 @@ with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
"
status
"
:
car_status
})
client
.
sendall
(
response
.
encode
())
# except ValueError:
# print(f"Invalid command: {command}")
# client.sendall(b"Invalid command format")
# break
# finally:
# client.close()
else
:
fc
.
stop
()
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment