Skip to content

Commit 8e5b82c

Browse files
committed
Add tests
1 parent a178235 commit 8e5b82c

11 files changed

Lines changed: 882 additions & 0 deletions

test/README.md

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# ST3215 Test Suite
2+
3+
This directory contains individual test scripts for testing ST3215 servo motor functionality. Each test focuses on specific functionality and can be run independently.
4+
5+
## Prerequisites
6+
7+
### Hardware Requirements
8+
- ST3215 servo motor with ID 1 connected to the system
9+
- USB-to-Serial adapter (RS-485 or TTL depending on your setup)
10+
- Proper power supply for the servo motor
11+
12+
### Software Requirements
13+
- Python 3.10+
14+
- st3215 library installed (`pip install -e .` from project root)
15+
- pyserial dependency
16+
17+
## Environment Setup
18+
19+
Set the USB device path in your environment:
20+
21+
```bash
22+
export ST3215_DEV="/dev/ttyUSB0"
23+
```
24+
25+
## Running Tests
26+
27+
Each test can be executed individually:
28+
29+
```bash
30+
python test_01_ping_servo.py
31+
python test_02_list_servos.py
32+
# ... etc
33+
```
34+
35+
Or run all tests in sequence:
36+
37+
```bash
38+
for test in test_*.py; do echo "Running $test"; python "$test"; echo; done
39+
```
40+
41+
## Test Descriptions
42+
43+
### Test 01: PingServo
44+
Tests basic communication with servo ID 1.
45+
- **File**: `test_01_ping_servo.py`
46+
- **Purpose**: Verify servo is connected and responding
47+
48+
### Test 02: ListServos
49+
Scans the bus for all connected servos.
50+
- **File**: `test_02_list_servos.py`
51+
- **Purpose**: Discover all servos on the bus
52+
53+
### Test 03: Read Load, Voltage & Current
54+
Tests telemetry reading functions.
55+
- **File**: `test_03_read_load_voltage_current.py`
56+
- **Purpose**: Read motor load, supply voltage, and current consumption
57+
- **⚠️ Special Instructions**: Apply physical force to the servo motor shaft, then press Enter before running this test
58+
59+
### Test 04: ReadTemperature
60+
Tests temperature sensor reading.
61+
- **File**: `test_04_read_temperature.py`
62+
- **Purpose**: Read internal servo temperature
63+
64+
### Test 05: ReadAcceleration
65+
Tests acceleration parameter reading.
66+
- **File**: `test_05_read_acceleration.py`
67+
- **Purpose**: Read current acceleration setting
68+
69+
### Test 06: ReadMode
70+
Tests operational mode reading.
71+
- **File**: `test_06_read_mode.py`
72+
- **Purpose**: Read current servo operation mode
73+
74+
### Test 07: ReadCorrection
75+
Tests position correction reading.
76+
- **File**: `test_07_read_correction.py`
77+
- **Purpose**: Read position correction value
78+
79+
### Test 08: ReadStatus
80+
Tests servo status reading.
81+
- **File**: `test_08_read_status.py`
82+
- **Purpose**: Read servo sensor status flags
83+
84+
### Test 09: IsMoving
85+
Tests motion detection.
86+
- **File**: `test_09_is_moving.py`
87+
- **Purpose**: Check if servo is currently moving
88+
89+
### Test 10: Complete Motion Control
90+
Comprehensive test of servo control functions.
91+
- **File**: `test_10_complete_motion_control.py`
92+
- **Purpose**: Test StartServo, SetAcceleration, SetSpeed, rotation mode, position mode, and StopServo
93+
- **⚠️ Important**: Ensure servo has enough physical clearance for movement
94+
95+
## Safety Notes
96+
97+
- Ensure proper power supply is connected before running tests
98+
- Verify servo has adequate clearance for movement in Test 10
99+
- Stop execution immediately if unusual noises or behaviors occur
100+
- Test 03 requires manual interaction (applying force)
101+
102+
## Troubleshooting
103+
104+
### Common Issues
105+
106+
1. **Permission denied on /dev/ttyUSB0**:
107+
```bash
108+
sudo chmod 666 /dev/ttyUSB0
109+
# or add user to dialout group
110+
sudo usermod -a -G dialout $USER
111+
```
112+
113+
2. **Servo not responding**:
114+
- Check power supply
115+
- Verify correct baud rate
116+
- Ensure proper wiring
117+
- Confirm servo ID is set to 1
118+
119+
3. **Import errors**:
120+
- Install library: `pip install -e .` from project root
121+
- Check Python path
122+
123+
## Expected Output
124+
125+
Each test will display:
126+
- Test description
127+
- Connection status
128+
- Test results with actual values
129+
- Success/failure indication
130+
131+
Example output:
132+
```
133+
=== ST3215 Ping Test ===
134+
Device: /dev/ttyUSB0
135+
Testing ping to servo ID 1...
136+
✓ Servo ID 1 responded successfully
137+
Test completed successfully!
138+
```

test/test_01_ping_servo.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Test 01: PingServo
4+
Tests basic communication with servo ID 1.
5+
"""
6+
7+
import os
8+
import sys
9+
from st3215 import ST3215
10+
11+
def main():
12+
print("=== ST3215 Ping Test ===")
13+
14+
# Get device from environment variable
15+
device = os.getenv('ST3215_DEV')
16+
if not device:
17+
print("❌ Error: ST3215_DEV environment variable not set")
18+
print(" Please set it to your serial device (e.g., /dev/ttyUSB0)")
19+
sys.exit(1)
20+
21+
print(f"Device: {device}")
22+
23+
try:
24+
# Initialize servo controller
25+
servo = ST3215(device)
26+
print("✓ Serial connection established")
27+
28+
# Test ping to servo ID 1
29+
servo_id = 1
30+
print(f"Testing ping to servo ID {servo_id}...")
31+
32+
result = servo.PingServo(servo_id)
33+
34+
if result:
35+
print(f"✓ Servo ID {servo_id} responded successfully")
36+
print("Test completed successfully!")
37+
else:
38+
print(f"❌ Servo ID {servo_id} did not respond")
39+
print(" Check connections, power supply, and servo ID")
40+
sys.exit(1)
41+
42+
except Exception as e:
43+
print(f"❌ Error during test: {e}")
44+
sys.exit(1)
45+
46+
if __name__ == "__main__":
47+
main()

test/test_02_list_servos.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Test 02: ListServos
4+
Scans the bus for all connected servos.
5+
"""
6+
7+
import os
8+
import sys
9+
from st3215 import ST3215
10+
11+
def main():
12+
print("=== ST3215 List Servos Test ===")
13+
14+
# Get device from environment variable
15+
device = os.getenv('ST3215_DEV')
16+
if not device:
17+
print("❌ Error: ST3215_DEV environment variable not set")
18+
print(" Please set it to your serial device (e.g., /dev/ttyUSB0)")
19+
sys.exit(1)
20+
21+
print(f"Device: {device}")
22+
23+
try:
24+
# Initialize servo controller
25+
servo = ST3215(device)
26+
print("✓ Serial connection established")
27+
28+
# Scan for all servos on the bus
29+
print("Scanning bus for connected servos...")
30+
31+
servo_list = servo.ListServos()
32+
33+
if servo_list is None:
34+
print("❌ Error occurred during servo scan")
35+
sys.exit(1)
36+
elif len(servo_list) == 0:
37+
print("❌ No servos found on the bus")
38+
print(" Check connections and power supply")
39+
sys.exit(1)
40+
else:
41+
print(f"✓ Found {len(servo_list)} servo(s):")
42+
for servo_id in servo_list:
43+
print(f" - Servo ID: {servo_id}")
44+
45+
if 1 in servo_list:
46+
print("✓ Required servo ID 1 is present")
47+
print("Test completed successfully!")
48+
else:
49+
print("⚠️ Warning: Servo ID 1 not found (required for other tests)")
50+
print(" Make sure servo ID 1 is connected and powered")
51+
52+
except Exception as e:
53+
print(f"❌ Error during test: {e}")
54+
sys.exit(1)
55+
56+
if __name__ == "__main__":
57+
main()
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Test 03: ReadLoad + ReadVoltage + ReadCurrent
4+
Tests telemetry reading functions.
5+
Special Instructions: Apply physical force to servo shaft before running.
6+
"""
7+
8+
import os
9+
import sys
10+
from st3215 import ST3215
11+
12+
def main():
13+
print("=== ST3215 Load, Voltage & Current Test ===")
14+
15+
# Get device from environment variable
16+
device = os.getenv('ST3215_DEV')
17+
if not device:
18+
print("❌ Error: ST3215_DEV environment variable not set")
19+
print(" Please set it to your serial device (e.g., /dev/ttyUSB0)")
20+
sys.exit(1)
21+
22+
print(f"Device: {device}")
23+
24+
try:
25+
# Initialize servo controller
26+
servo = ST3215(device)
27+
print("✓ Serial connection established")
28+
29+
servo_id = 1
30+
31+
# Special instructions for this test
32+
print("\n⚠️ SPECIAL INSTRUCTIONS:")
33+
print(" 1. Apply physical force to the servo motor shaft")
34+
print(" 2. Hold the force while the test is running")
35+
print(" 3. Press Enter when ready to start the test")
36+
37+
input("Press Enter to continue...")
38+
print("\nRunning telemetry tests...")
39+
40+
# Test ReadLoad
41+
print(f"Reading motor load from servo ID {servo_id}...")
42+
load = servo.ReadLoad(servo_id)
43+
44+
if load is not None:
45+
print(f"✓ Motor Load: {load:.2f}%")
46+
else:
47+
print("❌ Failed to read motor load")
48+
49+
# Test ReadVoltage
50+
print(f"Reading supply voltage from servo ID {servo_id}...")
51+
voltage = servo.ReadVoltage(servo_id)
52+
53+
if voltage is not None:
54+
print(f"✓ Supply Voltage: {voltage:.2f}V")
55+
else:
56+
print("❌ Failed to read supply voltage")
57+
58+
# Test ReadCurrent
59+
print(f"Reading current consumption from servo ID {servo_id}...")
60+
current = servo.ReadCurrent(servo_id)
61+
62+
if current is not None:
63+
print(f"✓ Current Consumption: {current:.2f}mA")
64+
else:
65+
print("❌ Failed to read current consumption")
66+
67+
# Summary
68+
print("\n=== Test Summary ===")
69+
if load is not None and voltage is not None and current is not None:
70+
print("✓ All telemetry readings successful!")
71+
print(f" Load: {load:.2f}%")
72+
print(f" Voltage: {voltage:.2f}V")
73+
print(f" Current: {current:.2f}mA")
74+
print("Test completed successfully!")
75+
else:
76+
print("❌ Some telemetry readings failed")
77+
print(" Check servo connection and power supply")
78+
sys.exit(1)
79+
80+
except Exception as e:
81+
print(f"❌ Error during test: {e}")
82+
sys.exit(1)
83+
84+
if __name__ == "__main__":
85+
main()

test/test_04_read_temperature.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Test 04: ReadTemperature
4+
Tests temperature sensor reading.
5+
"""
6+
7+
import os
8+
import sys
9+
from st3215 import ST3215
10+
11+
def main():
12+
print("=== ST3215 Temperature Test ===")
13+
14+
# Get device from environment variable
15+
device = os.getenv('ST3215_DEV')
16+
if not device:
17+
print("❌ Error: ST3215_DEV environment variable not set")
18+
print(" Please set it to your serial device (e.g., /dev/ttyUSB0)")
19+
sys.exit(1)
20+
21+
print(f"Device: {device}")
22+
23+
try:
24+
# Initialize servo controller
25+
servo = ST3215(device)
26+
print("✓ Serial connection established")
27+
28+
servo_id = 1
29+
30+
# Test ReadTemperature
31+
print(f"Reading internal temperature from servo ID {servo_id}...")
32+
33+
temperature = servo.ReadTemperature(servo_id)
34+
35+
if temperature is not None:
36+
print(f"✓ Internal Temperature: {temperature}°C")
37+
38+
# Provide some context about the temperature reading
39+
if temperature < 30:
40+
print(" Status: Normal (cool)")
41+
elif temperature < 50:
42+
print(" Status: Normal (warm)")
43+
elif temperature < 65:
44+
print(" Status: Getting hot")
45+
else:
46+
print(" Status: ⚠️ Hot - monitor closely")
47+
48+
print("Test completed successfully!")
49+
else:
50+
print("❌ Failed to read internal temperature")
51+
print(" Check servo connection and power supply")
52+
sys.exit(1)
53+
54+
except Exception as e:
55+
print(f"❌ Error during test: {e}")
56+
sys.exit(1)
57+
58+
if __name__ == "__main__":
59+
main()

0 commit comments

Comments
 (0)