Skip to content

Commit 7b9dcdf

Browse files
committed
Add persistent parameter example
1 parent abbffe7 commit 7b9dcdf

1 file changed

Lines changed: 142 additions & 0 deletions

File tree

examples/persistent_param.py

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# ,---------, ____ _ __
2+
# | ,-^-, | / __ )(_) /_______________ _____ ___
3+
# | ( O ) | / __ / / __/ ___/ ___/ __ `/_ / / _ \
4+
# | / ,--' | / /_/ / / /_/ /__/ / / /_/ / / /_/ __/
5+
# +------` /_____/_/\__/\___/_/ \__,_/ /___/\___/
6+
#
7+
# Copyright (C) 2025 Bitcraze AB
8+
#
9+
# This program is free software: you can redistribute it and/or modify
10+
# it under the terms of the GNU General Public License as published by
11+
# the Free Software Foundation, either version 3 of the License, or
12+
# (at your option) any later version.
13+
#
14+
# This program is distributed in the hope that it will be useful,
15+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
# GNU General Public License for more details.
18+
#
19+
# You should have received a copy of the GNU General Public License
20+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
"""
22+
Demonstrate persistent parameter storage on the Crazyflie.
23+
24+
Persistent parameters retain their values across reboots by storing them
25+
in EEPROM. This example shows how to:
26+
- List all persistent parameters
27+
- Get default values
28+
- Query persistent storage state
29+
- Store a parameter value to EEPROM
30+
- Clear a stored value from EEPROM
31+
32+
Example usage:
33+
python persistent_param.py # Use default URI
34+
python persistent_param.py --uri radio://0/80/2M/E7E7E7E701 # Custom URI
35+
"""
36+
37+
import asyncio
38+
from dataclasses import dataclass
39+
40+
import tyro
41+
42+
from cflib2 import Crazyflie, LinkContext
43+
44+
45+
@dataclass
46+
class Args:
47+
uri: str = "radio://0/80/2M/E7E7E7E7E7"
48+
"""Crazyflie URI"""
49+
50+
51+
async def main() -> None:
52+
args = tyro.cli(Args)
53+
54+
print(f"Connecting to {args.uri}...")
55+
context = LinkContext()
56+
cf = await Crazyflie.connect_from_uri(context, args.uri)
57+
print("Connected!\n")
58+
59+
param = cf.param()
60+
61+
try:
62+
# Step 1: List persistent parameters
63+
print("=== Persistent Parameters ===")
64+
persistent_params = []
65+
66+
for name in param.names():
67+
if await param.is_persistent(name):
68+
persistent_params.append(name)
69+
70+
print(f"Found {len(persistent_params)} persistent parameters\n")
71+
72+
# Step 2: Get default values
73+
print("=== Default Values ===\n")
74+
75+
test_params = ["ring.effect", "activeMarker.back", "pm.lowVoltage"]
76+
77+
for name in test_params:
78+
value = await param.get_default_value(name)
79+
print(f"{name}: {value}")
80+
81+
# Step 3: Get persistent state
82+
print("\n=== Persistent Parameter States ===\n")
83+
84+
for name in test_params:
85+
state = await param.persistent_get_state(name)
86+
print(f"{name}:")
87+
print(f" Default value: {state.default_value}")
88+
if state.is_stored:
89+
print(f" Stored value: {state.stored_value}")
90+
else:
91+
print(" Stored: No (using default)")
92+
print()
93+
94+
# Step 4: Store a value to EEPROM
95+
print("=== Storing a Parameter ===\n")
96+
97+
test_param = "ring.effect"
98+
99+
current_value = await param.get(test_param)
100+
print(f"Current value of {test_param}: {current_value}")
101+
102+
new_value = 10
103+
print(f"Setting {test_param} to {new_value}")
104+
await param.set(test_param, new_value)
105+
106+
print("Storing to EEPROM...")
107+
await param.persistent_store(test_param)
108+
print("Stored successfully!\n")
109+
110+
# Verify it's now marked as stored
111+
state = await param.persistent_get_state(test_param)
112+
print("Verification:")
113+
print(f" Default value: {state.default_value}")
114+
if state.is_stored:
115+
print(f" Stored value: {state.stored_value}")
116+
else:
117+
print(" Stored: No (using default)")
118+
119+
# Step 5: Clear a stored value from EEPROM
120+
print("\n=== Clearing a Stored Parameter ===\n")
121+
122+
print("Clearing stored value from EEPROM...")
123+
await param.persistent_clear(test_param)
124+
print("Cleared successfully!\n")
125+
126+
# Verify it's now using the default again
127+
state = await param.persistent_get_state(test_param)
128+
print("Verification:")
129+
print(f" Default value: {state.default_value}")
130+
if state.is_stored:
131+
print(f" Stored value: {state.stored_value}")
132+
else:
133+
print(" Stored: No (using default)")
134+
135+
finally:
136+
print("\nDisconnecting...")
137+
await cf.disconnect()
138+
print("Done!")
139+
140+
141+
if __name__ == "__main__":
142+
asyncio.run(main())

0 commit comments

Comments
 (0)