Skip to content

Commit c898dd6

Browse files
committed
fix: basic comm and data structure alignment with latest V2 board
1 parent ee049a5 commit c898dd6

6 files changed

Lines changed: 784 additions & 517 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ dependencies = [
1414
"importlib>=1.0.4",
1515
"keke>=0.1.4",
1616
"kivy>=2.3.0",
17+
"loguru>=0.7.3",
1718
"minimalmodbus>=2.1.1",
1819
"nmcli>=1.5.0",
1920
"pydantic>=2.10.3",

rcp/components/home/coordbar.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def init_connection(self, *args, **kv):
9393
"""
9494
This method is called when the connection is established
9595
"""
96-
self.syncEnable = self.device['scales'][self.inputIndex]['syncEnable']
96+
self.syncEnable = self.device['servo']['syncEnable']
9797
self.set_sync_ratio()
9898

9999
def update_tick(self, *args, **kv):
@@ -110,8 +110,14 @@ def update_tick(self, *args, **kv):
110110
def toggle_sync(self):
111111
if not self.app.connected:
112112
return
113-
self.syncEnable = not self.device['scales'][self.inputIndex]['syncEnable']
114-
self.device['scales'][self.inputIndex]['syncEnable'] = self.syncEnable
113+
if self.syncEnable:
114+
self.device['servo']['syncEnable'] = 0
115+
else:
116+
self.device['servo']['syncScaleIndex'] = self.inputIndex
117+
self.device['servo']['syncEnable'] = 1
118+
119+
# self.syncEnable = not self.device['scales'][self.inputIndex]['syncEnable']
120+
# self.device['scales'][self.inputIndex]['syncEnable'] = self.syncEnable
115121

116122
def set_sync_ratio(self, *args, **kv):
117123
if not self.app.connected:
@@ -136,8 +142,8 @@ def set_sync_ratio(self, *args, **kv):
136142
sync_ratio = Fraction(self.syncRatioNum, self.syncRatioDen)
137143

138144
final_ratio = scale_ratio * sync_ratio / servo_ratio
139-
self.device['scales'][self.inputIndex]['syncRatioNum'] = final_ratio.numerator
140-
self.device['scales'][self.inputIndex]['syncRatioDen'] = final_ratio.denominator
145+
self.device['servo']['syncRatioNum'] = final_ratio.numerator
146+
self.device['servo']['syncRatioDen'] = final_ratio.denominator
141147

142148
def on_syncRatioNum(self, instance, value):
143149
if self.app.home is None:

rcp/components/home/servobar.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ def on_index(self, instance, value):
212212
delta = (delta + steps_per_turn)
213213

214214
if delta != 0:
215-
self.app.device['servo']['direction'] = delta
215+
self.app.device['servo']['stepsToGo'] = delta
216216
self.disableControls = True
217217
self.previousIndex = self.index
218218

@@ -221,7 +221,7 @@ def on_offset(self, instance, value):
221221
delta = value - self.oldOffset
222222
delta_steps = int(delta / ratio)
223223
if delta_steps != 0:
224-
self.app.device['servo']['direction'] = delta_steps
224+
self.app.device['servo']['stepsToGo'] = delta_steps
225225
self.disableControls = True
226226
self.oldOffset = value
227227

rcp/utils/base_device.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
from keke import ktrace, kev
55
from rcp.utils import communication
66

7-
import logging
7+
from loguru import logger as log
88
from pydantic import BaseModel
99

10-
log = logging.getLogger(__name__)
11-
1210

1311
class TypeDefinition(BaseModel):
1412
name: str
@@ -26,6 +24,20 @@ class VariableDefinition(BaseModel):
2624

2725

2826
variable_definitions = [
27+
TypeDefinition(
28+
name="UART_HandleTypeDef",
29+
length=2,
30+
struct_unpack_string="L",
31+
read_function=communication.read_long,
32+
write_function=communication.write_long
33+
),
34+
TypeDefinition(
35+
name="GPIO_TypeDef",
36+
length=2,
37+
struct_unpack_string="L",
38+
read_function=communication.read_long,
39+
write_function=communication.write_long
40+
),
2941
TypeDefinition(
3042
name="TIM_HandleTypeDef",
3143
length=2,

rcp/utils/devices.py

Lines changed: 89 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import copy
22
import inspect
3+
import logging
34

45
import sys
56
import time
7+
from pprint import pprint
68

79
from rcp.utils.base_device import BaseDevice
810
from rcp.utils.communication import ConnectionManager
@@ -11,60 +13,94 @@
1113
SCALES_COUNT = 4
1214

1315

16+
class DeltaPosError(BaseDevice):
17+
definition = """
18+
typedef struct {
19+
int32_t delta;
20+
uint32_t oldPosition;
21+
uint32_t position;
22+
int32_t scaledDelta;
23+
int32_t error;
24+
} deltaPosError_t;
25+
"""
26+
27+
1428
class Servo(BaseDevice):
1529
definition = """
1630
typedef struct {
17-
float maxSpeed;
18-
float currentSpeed;
19-
float jogSpeed;
20-
float acceleration;
21-
int32_t direction;
22-
uint32_t destinationSteps;
23-
uint32_t currentSteps;
24-
uint32_t desiredSteps;
31+
float maxSpeed;
32+
float currentSpeed;
33+
float jogSpeed;
34+
float acceleration;
35+
int32_t stepsToGo;
36+
uint32_t destinationSteps;
37+
uint32_t previousSteps;
38+
uint32_t currentSteps;
39+
uint32_t desiredSteps;
40+
int32_t currentDirection;
41+
int32_t previousDirection;
42+
GPIO_TypeDef *stepPort;
43+
GPIO_TypeDef *dirPort;
44+
uint16_t stepPin;
45+
uint16_t dirPin;
46+
bool syncEnable;
47+
uint16_t unused;
48+
uint32_t syncScaleIndex;
49+
int32_t syncRatioNum;
50+
int32_t syncRatioDen;
51+
deltaPosError_t syncDeltaPos;
2552
} servo_t;
2653
"""
2754

2855

2956
class Global(BaseDevice):
3057
definition = """
3158
typedef struct {
32-
uint32_t executionInterval;
33-
uint32_t executionIntervalPrevious;
34-
uint32_t executionIntervalCurrent;
35-
uint32_t executionCycles;
36-
servo_t servo;
37-
input_t scales[4];
38-
fastData_t fastData;
39-
} rampsSharedData_t;
59+
uint32_t executionInterval;
60+
uint32_t executionIntervalPrevious;
61+
uint32_t executionIntervalCurrent;
62+
uint32_t executionCycles;
63+
GPIO_TypeDef *enaPort;
64+
GPIO_TypeDef *usrLedPort;
65+
uint16_t usrLedPin;
66+
uint16_t enaPin;
67+
servo_t servo[1];
68+
input_t scales[5];
69+
fastData_t fastData;
70+
TIM_HandleTypeDef *synchroRefreshTimer;
71+
UART_HandleTypeDef *modbusUart;
72+
deltaPosError_t rampsDeltaPos;
73+
uint16_t servoCycles;
74+
uint16_t servoCyclesCounter;
75+
} rampsHandler_t;
4076
"""
4177

4278

4379
class Scale(BaseDevice):
4480
definition = """
4581
typedef struct {
46-
TIM_HandleTypeDef *timerHandle;
47-
int32_t position;
48-
int32_t speed;
49-
int32_t syncRatioNum, syncRatioDen;
50-
uint16_t syncEnable;
51-
uint16_t spare;
82+
TIM_HandleTypeDef *timerHandle;
83+
int32_t position;
84+
int32_t speed;
85+
deltaPosError_t scalesDeltaPos;
86+
deltaPosError_t scalesSpeed;
5287
} input_t;
5388
"""
5489

5590

5691
class FastData(BaseDevice):
5792
definition = """
5893
typedef struct {
59-
uint32_t servoCurrent;
60-
uint32_t servoDesired;
61-
uint32_t stepsToGo;
62-
float servoSpeed;
63-
int32_t scaleCurrent[4];
64-
int32_t scaleSpeed[4];
65-
uint32_t cycles;
66-
uint32_t executionInterval;
67-
uint16_t servoEnable;
94+
uint32_t servoCurrent[1];
95+
uint32_t servoDesired[1];
96+
uint32_t stepsToGo[1];
97+
float servoSpeed[1];
98+
int32_t scaleCurrent[5];
99+
int32_t scaleSpeed[5];
100+
uint32_t cycles;
101+
uint32_t executionInterval;
102+
uint16_t servoMode;
103+
uint16_t servoEnable;
68104
} fastData_t;
69105
"""
70106

@@ -86,33 +122,34 @@ class FastData(BaseDevice):
86122
definition = my_class[1].register_type()
87123
variable_definitions.append(definition)
88124
except Exception as e:
125+
logging.error(e.__str__())
89126
failure_list.append(my_class)
90127
unloaded_list = copy.deepcopy(failure_list)
91128
iterations_limit -= 1
92129

93130

94131
if __name__ == "__main__":
95132
from rcp.utils import communication
133+
from loguru import logger as log
96134

97-
connection_manager = communication.ConnectionManager()
135+
connection_manager = communication.ConnectionManager(serial_device="/dev/ttyUSB1", baudrate=115200, address=17, debug=False)
98136
device = Global(connection_manager=connection_manager, base_address=0)
99-
100-
while True:
101-
time.sleep(0.5)
102-
# values = device['fastData'].refresh()
103-
# print(
104-
# device['executionInterval'],
105-
# device['executionIntervalPrevious'],
106-
# device['executionIntervalCurrent'],
107-
# device['executionCycles']
108-
# )
109-
values = device['servo'].refresh()
110-
print(values)
111-
112-
# float maxSpeed;
113-
# float currentSpeed;
114-
# float acceleration;
115-
# int32_t direction;
116-
# uint32_t destinationSteps;
117-
# uint32_t currentSteps;
118-
# uint32_t desiredSteps;
137+
log.info("Starting test routine for serial modbus communication")
138+
# while True:
139+
time.sleep(0.5)
140+
print(f"Ena Port(0x40020800): {device['enaPort']:x}")
141+
print(f"Ena Pin(1024): {device['enaPin']}")
142+
print(f"User Led Port(0x40020000): {device['usrLedPort']:x}")
143+
print(f"User Led Pin(1024): {device['usrLedPin']}")
144+
145+
print(f"Scale 0 Timer Handle(0x200019d4): {device['scales'][0]['timerHandle']:x}")
146+
print(f"Scale 1 Timer Handle(0x2000198c): {device['scales'][1]['timerHandle']:x}")
147+
print(f"Scale 2 Timer Handle(0x20001944): {device['scales'][2]['timerHandle']:x}")
148+
print(f"Scale 3 Timer Handle(0x200018fc): {device['scales'][3]['timerHandle']:x}")
149+
print(f"Scale 4 Timer Handle(0x200018b4): {device['scales'][4]['timerHandle']:x}")
150+
151+
device['servoCycles'] = 0
152+
count = 10
153+
while count > 0:
154+
fastData = device['fastData'].refresh()
155+
pprint(fastData)

0 commit comments

Comments
 (0)