-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest.py
More file actions
101 lines (83 loc) · 3.56 KB
/
Test.py
File metadata and controls
101 lines (83 loc) · 3.56 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
'''
*
* ===================================================
* CropDrop Bot (CB) Theme [eYRC 2025-26]
* ===================================================
*
* This script is intended to be an Boilerplate for
* Task 1B of CropDrop Bot (CB) Theme [eYRC 2025-26].
*
* Filename: Test.py
* Created: 24/08/2025
* Last Modified: 24/08/2025
* Author: e-Yantra Team
* Team ID: [ CB_XXXX ]
* This software is made available on an "AS IS WHERE IS BASIS".
* Licensee/end user indemnifies and will keep e-Yantra indemnified from
* any and all claim(s) that emanate from the use of the Software or
* breach of the terms of this agreement.
*
* e-Yantra - An MHRD project under National Mission on Education using ICT (NMEICT)
*
*****************************************************************************************
'''
'''You can Modify the this file,add more functions According to your usage.
You are not allowed to add any external packges,Beside the included Packages.You can use Built-in Python modules.'''
import time
import signal
import sys
# Import required classes for simulation and Q-learning
from Connector import CoppeliaClient
from Qlearning import QLearningController
# Global flag to handle safe interruption (Ctrl+C)
stop_requested = False
def signal_handler(sig, frame):
"""
Signal handler to catch keyboard interrupt (Ctrl+C).
It sets a flag to stop the main testing loop gracefully.
"""
global stop_requested
print("\n[TEST] Interrupt received. Stopping testing gracefully...")
stop_requested = True
# Register the signal handler to handle SIGINT (Ctrl+C)
signal.signal(signal.SIGINT, signal_handler)
def main():
global stop_requested
# === Initialize the Q-learning controller and load the Q-table ===
ql = QLearningController()
loaded = ql.load_q_table()
if not loaded:
print("[TEST] No Q-table found. Exiting.")
return
ql.epsilon = 0
# === Connect to the CoppeliaSim simulation environment ===
client = CoppeliaClient()
client.connect()
print("[TEST] Starting test loop...")
# === Start the testing loop ===
while not stop_requested:
# Step Receive current sensor data from simulator
sensor_data = client.receive_sensor_data()
if not sensor_data:
time.sleep(0.05) # Wait a short time before retrying if no data
continue
# Convert sensor data into a discrete state
state = ql.Get_state(sensor_data)
reward = 0
# Calculate reward just for logging/debug (not used in real Q-learning here)
reward = ql.Calculate_reward(state) # You can remove this line if reward is not needed in test
# Choose an action based on the current state (pure exploitation, not training)
action = ql.choose_action(state)
# Convert action into motor commands
left_speed, right_speed = ql.perform_action(action)
# Send the motor command to the simulator
client.send_motor_command(left_speed, right_speed, state=state, action=action,reward=reward)
# Wait for a short period before next iteration (to control speed of testing)
time.sleep(0.05)
# === Stop the robot and close the connection after exiting loop (Ctrl+C) ===
client.send_motor_command(0, 0) # Stop the robot
client.close() # Disconnect from simulator
print("[TEST] Testing stopped.")
# Entry point for the script
if __name__ == "__main__":
main()