-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpersistent_param.py
More file actions
142 lines (111 loc) · 4.47 KB
/
persistent_param.py
File metadata and controls
142 lines (111 loc) · 4.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
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
# ,---------, ____ _ __
# | ,-^-, | / __ )(_) /_______________ _____ ___
# | ( O ) | / __ / / __/ ___/ ___/ __ `/_ / / _ \
# | / ,--' | / /_/ / / /_/ /__/ / / /_/ / / /_/ __/
# +------` /_____/_/\__/\___/_/ \__,_/ /___/\___/
#
# Copyright (C) 2025 Bitcraze AB
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Demonstrate persistent parameter storage on the Crazyflie.
Persistent parameters retain their values across reboots by storing them
in EEPROM. This example shows how to:
- List all persistent parameters
- Get default values
- Query persistent storage state
- Store a parameter value to EEPROM
- Clear a stored value from EEPROM
Example usage:
python persistent_param.py # Use default URI
python persistent_param.py --uri radio://0/80/2M/E7E7E7E701 # Custom URI
"""
import asyncio
from dataclasses import dataclass
import tyro
from cflib2 import Crazyflie, LinkContext
@dataclass
class Args:
uri: str = "radio://0/80/2M/E7E7E7E7E7"
"""Crazyflie URI"""
async def main() -> None:
args = tyro.cli(Args)
print(f"Connecting to {args.uri}...")
context = LinkContext()
cf = await Crazyflie.connect_from_uri(context, args.uri)
print("Connected!\n")
param = cf.param()
try:
# Step 1: List persistent parameters
print("=== Persistent Parameters ===")
persistent_params = []
for name in param.names():
if await param.is_persistent(name):
persistent_params.append(name)
print(f"Found {len(persistent_params)} persistent parameters\n")
# Step 2: Get default values
print("=== Default Values ===\n")
test_params = ["ring.effect", "activeMarker.back", "pm.lowVoltage"]
for name in test_params:
value = await param.get_default_value(name)
print(f"{name}: {value}")
# Step 3: Get persistent state
print("\n=== Persistent Parameter States ===\n")
for name in test_params:
state = await param.persistent_get_state(name)
print(f"{name}:")
print(f" Default value: {state.default_value}")
if state.is_stored:
print(f" Stored value: {state.stored_value}")
else:
print(" Stored: No (using default)")
print()
# Step 4: Store a value to EEPROM
print("=== Storing a Parameter ===\n")
test_param = "ring.effect"
current_value = await param.get(test_param)
print(f"Current value of {test_param}: {current_value}")
new_value = 10
print(f"Setting {test_param} to {new_value}")
await param.set(test_param, new_value)
print("Storing to EEPROM...")
await param.persistent_store(test_param)
print("Stored successfully!\n")
# Verify it's now marked as stored
state = await param.persistent_get_state(test_param)
print("Verification:")
print(f" Default value: {state.default_value}")
if state.is_stored:
print(f" Stored value: {state.stored_value}")
else:
print(" Stored: No (using default)")
# Step 5: Clear a stored value from EEPROM
print("\n=== Clearing a Stored Parameter ===\n")
print("Clearing stored value from EEPROM...")
await param.persistent_clear(test_param)
print("Cleared successfully!\n")
# Verify it's now using the default again
state = await param.persistent_get_state(test_param)
print("Verification:")
print(f" Default value: {state.default_value}")
if state.is_stored:
print(f" Stored value: {state.stored_value}")
else:
print(" Stored: No (using default)")
finally:
print("\nDisconnecting...")
await cf.disconnect()
print("Done!")
if __name__ == "__main__":
asyncio.run(main())