forked from themoos/python-moos
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsimpleapp.py
More file actions
76 lines (59 loc) · 2.47 KB
/
simpleapp.py
File metadata and controls
76 lines (59 loc) · 2.47 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
import pymoos
import time
# A simple example using the CMOOSApp wrapper:
# a) we make an app object and set up callbacks for the application lifecycle
# b) we run the app which starts the main loop
# The app class provides a more structured approach than comms, with separate
# callbacks for startup, iteration, and mail handling
app = pymoos.app()
# OnStartUp is called when the app first starts
def on_startup():
print("App starting up...")
# Note: AppTick and CommsTick from the mission file are automatically
# handled by CMOOSApp - you don't need to read or set them manually.
# Read custom configuration parameters from the mission file
# The mission file should have a ProcessConfig block for this app
success, var_name = app.get_configuration_string('variable_name')
if success:
print(f"Will publish to variable: {var_name}")
else:
print("Using default variable name: simple_app_var")
success, max_iter = app.get_configuration_int('max_iterations')
if success:
print(f"Will run for {max_iter} iterations")
return True
# OnConnectToServer is called when connection to MOOSDB is established
def on_connect_to_server():
print("Connected to MOOSDB, registering for variables...")
return app.register('simple_app_var', 0)
# OnNewMail is called when new mail arrives
def on_new_mail(mail):
print(f"Received {len(mail)} messages:")
for msg in mail:
msg.trace()
return True
# Iterate is the main work loop, called at the frequency set by AppTick in the mission file
iteration_count = 0
def iterate():
global iteration_count
iteration_count += 1
print(f"Iterate #{iteration_count}")
# Publish a message
app.notify('simple_app_var', f'iteration {iteration_count}', pymoos.time())
# Run for 10 iterations then stop
if iteration_count >= 10:
return False
return True
def main():
# Set up the callbacks
app.set_on_start_up_callback(on_startup)
app.set_on_connect_to_server_callback(on_connect_to_server)
app.set_on_new_mail_callback(on_new_mail)
app.set_iterate_callback(iterate)
# Standard MOOS pattern: app_name and mission file
# The mission file contains ServerHost, ServerPort, AppTick, CommsTick, and Community
app.run('pymoos_simple_app', 'simpleapp.moos')
# Alternative: Run without mission file (uses default Mission.moos)
# app.run('pymoos_simple_app')
if __name__ == "__main__":
main()