Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
B
bldc_uart_comm_stm32f4_discovery
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
João Lino
bldc_uart_comm_stm32f4_discovery
Commits
85914615
Commit
85914615
authored
Jan 27, 2017
by
service-config
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Multiple fixes to stability. Added debug.
parent
56afc6c3
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
171 additions
and
46 deletions
+171
-46
bldc_interface.cpp
bldc_interface.cpp
+87
-19
comm_uart_arduino.cpp
comm_uart_arduino.cpp
+39
-26
packet.cpp
packet.cpp
+44
-0
packet.h
packet.h
+1
-1
No files found.
bldc_interface.cpp
View file @
85914615
...
@@ -127,6 +127,9 @@ void bldc_interface_send_packet(unsigned char *data, int len) {
...
@@ -127,6 +127,9 @@ void bldc_interface_send_packet(unsigned char *data, int len) {
* The length of the buffer.
* The length of the buffer.
*/
*/
void
bldc_interface_process_packet
(
unsigned
char
*
data
,
int
len
)
{
void
bldc_interface_process_packet
(
unsigned
char
*
data
,
int
len
)
{
#ifdef DEBUG_BLDC
Serial
.
println
(
F
(
"[ bldc ] New packet to process!"
));
#endif
if
(
!
len
)
{
if
(
!
len
)
{
return
;
return
;
}
}
...
@@ -144,6 +147,9 @@ void bldc_interface_process_packet(unsigned char *data, int len) {
...
@@ -144,6 +147,9 @@ void bldc_interface_process_packet(unsigned char *data, int len) {
switch
(
id
)
{
switch
(
id
)
{
case
COMM_FW_VERSION
:
case
COMM_FW_VERSION
:
#ifdef DEBUG_BLDC
Serial
.
println
(
F
(
"[ bldc ] COMM_FW_VERSION = ?"
));
#endif
if
(
len
==
2
)
{
if
(
len
==
2
)
{
ind
=
0
;
ind
=
0
;
fw_major
=
data
[
ind
++
];
fw_major
=
data
[
ind
++
];
...
@@ -155,7 +161,15 @@ void bldc_interface_process_packet(unsigned char *data, int len) {
...
@@ -155,7 +161,15 @@ void bldc_interface_process_packet(unsigned char *data, int len) {
break
;
break
;
case
COMM_ERASE_NEW_APP
:
case
COMM_ERASE_NEW_APP
:
#ifdef DEBUG_BLDC
Serial
.
println
(
F
(
"[ bldc ] COMM_ERASE_NEW_APP = ?"
));
#endif
// TODO
break
;
case
COMM_WRITE_NEW_APP_DATA
:
case
COMM_WRITE_NEW_APP_DATA
:
#ifdef DEBUG_BLDC
Serial
.
println
(
F
(
"[ bldc ] COMM_WRITE_NEW_APP_DATA = ?"
));
#endif
// TODO
// TODO
break
;
break
;
...
@@ -202,6 +216,9 @@ void bldc_interface_process_packet(unsigned char *data, int len) {
...
@@ -202,6 +216,9 @@ void bldc_interface_process_packet(unsigned char *data, int len) {
break
;
break
;
case
COMM_PRINT
:
case
COMM_PRINT
:
#ifdef DEBUG_BLDC
Serial
.
println
(
F
(
"[ bldc ] COMM_PRINT = ?"
));
#endif
if
(
rx_printf_func
)
{
if
(
rx_printf_func
)
{
data
[
len
]
=
'\0'
;
data
[
len
]
=
'\0'
;
rx_printf_func
((
char
*
)
data
);
rx_printf_func
((
char
*
)
data
);
...
@@ -209,10 +226,16 @@ void bldc_interface_process_packet(unsigned char *data, int len) {
...
@@ -209,10 +226,16 @@ void bldc_interface_process_packet(unsigned char *data, int len) {
break
;
break
;
case
COMM_SAMPLE_PRINT
:
case
COMM_SAMPLE_PRINT
:
#ifdef DEBUG_BLDC
Serial
.
println
(
F
(
"[ bldc ] COMM_SAMPLE_PRINT = ?"
));
#endif
// TODO
// TODO
break
;
break
;
case
COMM_ROTOR_POSITION
:
case
COMM_ROTOR_POSITION
:
#ifdef DEBUG_BLDC
Serial
.
println
(
F
(
"[ bldc ] COMM_ROTOR_POSITION = ?"
));
#endif
ind
=
0
;
ind
=
0
;
rotor_pos
=
buffer_get_float32
(
data
,
100000.0
,
&
ind
);
rotor_pos
=
buffer_get_float32
(
data
,
100000.0
,
&
ind
);
...
@@ -222,11 +245,22 @@ void bldc_interface_process_packet(unsigned char *data, int len) {
...
@@ -222,11 +245,22 @@ void bldc_interface_process_packet(unsigned char *data, int len) {
break
;
break
;
case
COMM_EXPERIMENT_SAMPLE
:
case
COMM_EXPERIMENT_SAMPLE
:
#ifdef DEBUG_BLDC
Serial
.
println
(
F
(
"[ bldc ] COMM_EXPERIMENT_SAMPLE = ?"
));
#endif
// TODO
// TODO
break
;
break
;
case
COMM_GET_MCCONF
:
case
COMM_GET_MCCONF
:
#ifdef DEBUG_BLDC
Serial
.
println
(
F
(
"[ bldc ] COMM_GET_MCCONF = ?"
));
#endif
//TODO
break
;
case
COMM_GET_MCCONF_DEFAULT
:
case
COMM_GET_MCCONF_DEFAULT
:
#ifdef DEBUG_BLDC
Serial
.
println
(
F
(
"[ bldc ] COMM_GET_MCCONF_DEFAULT = ?"
));
#endif
ind
=
0
;
ind
=
0
;
mcconf
.
pwm_mode
=
(
mc_pwm_mode
)
data
[
ind
++
];
mcconf
.
pwm_mode
=
(
mc_pwm_mode
)
data
[
ind
++
];
mcconf
.
comm_mode
=
(
mc_comm_mode
)
data
[
ind
++
];
mcconf
.
comm_mode
=
(
mc_comm_mode
)
data
[
ind
++
];
...
@@ -319,7 +353,15 @@ void bldc_interface_process_packet(unsigned char *data, int len) {
...
@@ -319,7 +353,15 @@ void bldc_interface_process_packet(unsigned char *data, int len) {
break
;
break
;
case
COMM_GET_APPCONF
:
case
COMM_GET_APPCONF
:
#ifdef DEBUG_BLDC
Serial
.
println
(
F
(
"[ bldc ] COMM_GET_APPCONF = ?"
));
#endif
// TODO
break
;
case
COMM_GET_APPCONF_DEFAULT
:
case
COMM_GET_APPCONF_DEFAULT
:
#ifdef DEBUG_BLDC
Serial
.
println
(
F
(
"[ bldc ] COMM_GET_APPCONF_DEFAULT = ?"
));
#endif
ind
=
0
;
ind
=
0
;
appconf
.
controller_id
=
data
[
ind
++
];
appconf
.
controller_id
=
data
[
ind
++
];
appconf
.
timeout_msec
=
buffer_get_uint32
(
data
,
&
ind
);
appconf
.
timeout_msec
=
buffer_get_uint32
(
data
,
&
ind
);
...
@@ -387,6 +429,9 @@ void bldc_interface_process_packet(unsigned char *data, int len) {
...
@@ -387,6 +429,9 @@ void bldc_interface_process_packet(unsigned char *data, int len) {
break
;
break
;
case
COMM_DETECT_MOTOR_PARAM
:
case
COMM_DETECT_MOTOR_PARAM
:
#ifdef DEBUG_BLDC
Serial
.
println
(
F
(
"[ bldc ] COMM_DETECT_MOTOR_PARAM = ?"
));
#endif
ind
=
0
;
ind
=
0
;
detect_cycle_int_limit
=
buffer_get_float32
(
data
,
1000.0
,
&
ind
);
detect_cycle_int_limit
=
buffer_get_float32
(
data
,
1000.0
,
&
ind
);
detect_coupling_k
=
buffer_get_float32
(
data
,
1000.0
,
&
ind
);
detect_coupling_k
=
buffer_get_float32
(
data
,
1000.0
,
&
ind
);
...
@@ -402,22 +447,37 @@ void bldc_interface_process_packet(unsigned char *data, int len) {
...
@@ -402,22 +447,37 @@ void bldc_interface_process_packet(unsigned char *data, int len) {
break
;
break
;
case
COMM_DETECT_MOTOR_R_L
:
{
case
COMM_DETECT_MOTOR_R_L
:
{
#ifdef DEBUG_BLDC
Serial
.
println
(
F
(
"[ bldc ] COMM_DETECT_MOTOR_R_L = ?"
));
#endif
// TODO!
// TODO!
}
break
;
}
break
;
case
COMM_DETECT_MOTOR_FLUX_LINKAGE
:
{
case
COMM_DETECT_MOTOR_FLUX_LINKAGE
:
{
#ifdef DEBUG_BLDC
Serial
.
println
(
F
(
"[ bldc ] COMM_DETECT_MOTOR_FLUX_LINKAGE = ?"
));
#endif
// TODO!
// TODO!
}
break
;
}
break
;
case
COMM_DETECT_ENCODER
:
{
case
COMM_DETECT_ENCODER
:
{
#ifdef DEBUG_BLDC
Serial
.
println
(
F
(
"[ bldc ] COMM_DETECT_ENCODER = ?"
));
#endif
// TODO!
// TODO!
}
break
;
}
break
;
case
COMM_DETECT_HALL_FOC
:
{
case
COMM_DETECT_HALL_FOC
:
{
#ifdef DEBUG_BLDC
Serial
.
println
(
F
(
"[ bldc ] COMM_DETECT_HALL_FOC = ?"
));
#endif
// TODO!
// TODO!
}
break
;
}
break
;
case
COMM_GET_DECODED_PPM
:
case
COMM_GET_DECODED_PPM
:
#ifdef DEBUG_BLDC
Serial
.
println
(
F
(
"[ bldc ] COMM_GET_DECODED_PPM = ?"
));
#endif
ind
=
0
;
ind
=
0
;
dec_ppm
=
buffer_get_float32
(
data
,
1000000.0
,
&
ind
);
dec_ppm
=
buffer_get_float32
(
data
,
1000000.0
,
&
ind
);
dec_ppm_len
=
buffer_get_float32
(
data
,
1000000.0
,
&
ind
);
dec_ppm_len
=
buffer_get_float32
(
data
,
1000000.0
,
&
ind
);
...
@@ -428,6 +488,9 @@ void bldc_interface_process_packet(unsigned char *data, int len) {
...
@@ -428,6 +488,9 @@ void bldc_interface_process_packet(unsigned char *data, int len) {
break
;
break
;
case
COMM_GET_DECODED_ADC
:
case
COMM_GET_DECODED_ADC
:
#ifdef DEBUG_BLDC
Serial
.
println
(
F
(
"[ bldc ] COMM_GET_DECODED_ADC = ?"
));
#endif
ind
=
0
;
ind
=
0
;
dec_adc
=
buffer_get_float32
(
data
,
1000000.0
,
&
ind
);
dec_adc
=
buffer_get_float32
(
data
,
1000000.0
,
&
ind
);
dec_adc_voltage
=
buffer_get_float32
(
data
,
1000000.0
,
&
ind
);
dec_adc_voltage
=
buffer_get_float32
(
data
,
1000000.0
,
&
ind
);
...
@@ -439,6 +502,9 @@ void bldc_interface_process_packet(unsigned char *data, int len) {
...
@@ -439,6 +502,9 @@ void bldc_interface_process_packet(unsigned char *data, int len) {
break
;
break
;
case
COMM_GET_DECODED_CHUK
:
case
COMM_GET_DECODED_CHUK
:
#ifdef DEBUG_BLDC
Serial
.
println
(
F
(
"[ bldc ] COMM_GET_DECODED_CHUK = ?"
));
#endif
ind
=
0
;
ind
=
0
;
dec_chuk
=
buffer_get_float32
(
data
,
1000000.0
,
&
ind
);
dec_chuk
=
buffer_get_float32
(
data
,
1000000.0
,
&
ind
);
...
@@ -448,6 +514,9 @@ void bldc_interface_process_packet(unsigned char *data, int len) {
...
@@ -448,6 +514,9 @@ void bldc_interface_process_packet(unsigned char *data, int len) {
break
;
break
;
case
COMM_SET_MCCONF
:
case
COMM_SET_MCCONF
:
#ifdef DEBUG_BLDC
Serial
.
println
(
F
(
"[ bldc ] COMM_SET_MCCONF = ?"
));
#endif
// This is a confirmation that the new mcconf is received.
// This is a confirmation that the new mcconf is received.
if
(
rx_mcconf_received_func
)
{
if
(
rx_mcconf_received_func
)
{
rx_mcconf_received_func
();
rx_mcconf_received_func
();
...
@@ -455,6 +524,9 @@ void bldc_interface_process_packet(unsigned char *data, int len) {
...
@@ -455,6 +524,9 @@ void bldc_interface_process_packet(unsigned char *data, int len) {
break
;
break
;
case
COMM_SET_APPCONF
:
case
COMM_SET_APPCONF
:
#ifdef DEBUG_BLDC
Serial
.
println
(
F
(
"[ bldc ] COMM_SET_APPCONF = ?"
));
#endif
// This is a confirmation that the new appconf is received.
// This is a confirmation that the new appconf is received.
if
(
rx_appconf_received_func
)
{
if
(
rx_appconf_received_func
)
{
rx_appconf_received_func
();
rx_appconf_received_func
();
...
@@ -462,6 +534,9 @@ void bldc_interface_process_packet(unsigned char *data, int len) {
...
@@ -462,6 +534,9 @@ void bldc_interface_process_packet(unsigned char *data, int len) {
break
;
break
;
default:
default:
#ifdef DEBUG_BLDC
Serial
.
println
(
F
(
"[ bldc ] default = ?"
));
#endif
break
;
break
;
}
}
}
}
...
@@ -528,8 +603,7 @@ void bldc_interface_set_rx_appconf_received_func(void(*func)(void)) {
...
@@ -528,8 +603,7 @@ void bldc_interface_set_rx_appconf_received_func(void(*func)(void)) {
void bldc_interface_terminal_cmd(char* cmd) {
void bldc_interface_terminal_cmd(char* cmd) {
int send_index = 0;
int send_index = 0;
int len = strlen(cmd);
int len = strlen(cmd);
//fwd_can_append(send_buffer, &send_index);
fwd_can_append(send_buffer, &send_index);
fwd_can_append();
send_buffer[send_index++] = COMM_TERMINAL_CMD;
send_buffer[send_index++] = COMM_TERMINAL_CMD;
memcpy(send_buffer + send_index, cmd, len);
memcpy(send_buffer + send_index, cmd, len);
send_index += len;
send_index += len;
...
@@ -538,8 +612,7 @@ void bldc_interface_terminal_cmd(char* cmd) {
...
@@ -538,8 +612,7 @@ void bldc_interface_terminal_cmd(char* cmd) {
void bldc_interface_set_duty_cycle(float dutyCycle) {
void bldc_interface_set_duty_cycle(float dutyCycle) {
int send_index = 0;
int send_index = 0;
//fwd_can_append(send_buffer, &send_index);
fwd_can_append(send_buffer, &send_index);
fwd_can_append();
send_buffer[send_index++] = COMM_SET_DUTY;
send_buffer[send_index++] = COMM_SET_DUTY;
buffer_append_float32(send_buffer, dutyCycle, 100000.0, &send_index);
buffer_append_float32(send_buffer, dutyCycle, 100000.0, &send_index);
send_packet_no_fwd(send_buffer, send_index);
send_packet_no_fwd(send_buffer, send_index);
...
@@ -548,26 +621,24 @@ void bldc_interface_set_duty_cycle(float dutyCycle) {
...
@@ -548,26 +621,24 @@ void bldc_interface_set_duty_cycle(float dutyCycle) {
void
bldc_interface_set_current
(
float
current
)
{
void
bldc_interface_set_current
(
float
current
)
{
uint8_t
send_buffer
[
5
];
uint8_t
send_buffer
[
5
];
int
send_index
=
0
;
int
send_index
=
0
;
//
fwd_can_append(send_buffer, &send_index);
fwd_can_append
(
send_buffer
,
&
send_index
);
send_buffer
[
send_index
++
]
=
COMM_SET_CURRENT
;
send_buffer
[
send_index
++
]
=
COMM_SET_CURRENT
;
buffer_append_float32
(
send_buffer
,
current
,
1000.0
,
&
send_index
);
buffer_append_float32
(
send_buffer
,
current
,
1000.0
,
&
send_index
);
//Serial.write(send_buffer, send_index); Serial.println("");
send_packet_no_fwd
(
send_buffer
,
send_index
);
send_packet_no_fwd
(
send_buffer
,
send_index
);
}
}
/*void bldc_interface_set_current_brake(float current) {
void
bldc_interface_set_current_brake
(
float
current
)
{
uint8_t
send_buffer
[
5
];
int
send_index
=
0
;
int
send_index
=
0
;
//fwd_can_append(send_buffer, &send_index);
fwd_can_append
(
send_buffer
,
&
send_index
);
fwd_can_append();
send_buffer
[
send_index
++
]
=
COMM_SET_CURRENT_BRAKE
;
send_buffer
[
send_index
++
]
=
COMM_SET_CURRENT_BRAKE
;
buffer_append_float32
(
send_buffer
,
current
,
1000.0
,
&
send_index
);
buffer_append_float32
(
send_buffer
,
current
,
1000.0
,
&
send_index
);
send_packet_no_fwd
(
send_buffer
,
send_index
);
send_packet_no_fwd
(
send_buffer
,
send_index
);
}
}
void bldc_interface_set_rpm(int rpm) {
/*
void bldc_interface_set_rpm(int rpm) {
int send_index = 0;
int send_index = 0;
//fwd_can_append(send_buffer, &send_index);
fwd_can_append(send_buffer, &send_index);
fwd_can_append();
send_buffer[send_index++] = COMM_SET_RPM;
send_buffer[send_index++] = COMM_SET_RPM;
buffer_append_int32(send_buffer, rpm, &send_index);
buffer_append_int32(send_buffer, rpm, &send_index);
send_packet_no_fwd(send_buffer, send_index);
send_packet_no_fwd(send_buffer, send_index);
...
@@ -575,8 +646,7 @@ void bldc_interface_set_rpm(int rpm) {
...
@@ -575,8 +646,7 @@ void bldc_interface_set_rpm(int rpm) {
void bldc_interface_set_pos(float pos) {
void bldc_interface_set_pos(float pos) {
int send_index = 0;
int send_index = 0;
//fwd_can_append(send_buffer, &send_index);
fwd_can_append(send_buffer, &send_index);
fwd_can_append();
send_buffer[send_index++] = COMM_SET_POS;
send_buffer[send_index++] = COMM_SET_POS;
buffer_append_float32(send_buffer, pos, 1000000.0, &send_index);
buffer_append_float32(send_buffer, pos, 1000000.0, &send_index);
send_packet_no_fwd(send_buffer, send_index);
send_packet_no_fwd(send_buffer, send_index);
...
@@ -584,8 +654,7 @@ void bldc_interface_set_pos(float pos) {
...
@@ -584,8 +654,7 @@ void bldc_interface_set_pos(float pos) {
void bldc_interface_set_servo_pos(float pos) {
void bldc_interface_set_servo_pos(float pos) {
int send_index = 0;
int send_index = 0;
//fwd_can_append(send_buffer, &send_index);
fwd_can_append(send_buffer, &send_index);
fwd_can_append();
send_buffer[send_index++] = COMM_SET_SERVO_POS;
send_buffer[send_index++] = COMM_SET_SERVO_POS;
buffer_append_float16(send_buffer, pos, 1000.0, &send_index);
buffer_append_float16(send_buffer, pos, 1000.0, &send_index);
send_packet_no_fwd(send_buffer, send_index);
send_packet_no_fwd(send_buffer, send_index);
...
@@ -593,8 +662,7 @@ void bldc_interface_set_servo_pos(float pos) {
...
@@ -593,8 +662,7 @@ void bldc_interface_set_servo_pos(float pos) {
void bldc_interface_set_mcconf(const mc_configuration *mcconf) {
void bldc_interface_set_mcconf(const mc_configuration *mcconf) {
int send_index = 0;
int send_index = 0;
//fwd_can_append(send_buffer, &send_index);
fwd_can_append(send_buffer, &send_index);
fwd_can_append();
send_buffer[send_index++] = COMM_SET_MCCONF;
send_buffer[send_index++] = COMM_SET_MCCONF;
send_buffer[send_index++] = mcconf->pwm_mode;
send_buffer[send_index++] = mcconf->pwm_mode;
...
@@ -764,7 +832,7 @@ void bldc_interface_get_fw_version(void) {
...
@@ -764,7 +832,7 @@ void bldc_interface_get_fw_version(void) {
}
}
*/
*/
void
bldc_interface_get_values
(
void
)
{
void
bldc_interface_get_values
(
void
)
{
u
int8_t
send_buffer
[
3
];
u
nsigned
char
send_buffer
[
3
];
int
send_index
=
0
;
int
send_index
=
0
;
fwd_can_append
(
send_buffer
,
&
send_index
);
fwd_can_append
(
send_buffer
,
&
send_index
);
send_buffer
[
send_index
++
]
=
COMM_GET_VALUES
;
send_buffer
[
send_index
++
]
=
COMM_GET_VALUES
;
...
...
comm_uart_arduino.cpp
View file @
85914615
...
@@ -27,7 +27,7 @@
...
@@ -27,7 +27,7 @@
// Settings
// Settings
#define __CUA_UART_BAUDRATE 9600
#define __CUA_UART_BAUDRATE 9600
//115200
// Private functions
// Private functions
static
void
send_packet
(
unsigned
char
*
data
,
int
len
);
static
void
send_packet
(
unsigned
char
*
data
,
int
len
);
...
@@ -58,40 +58,49 @@ void softwareSerialEvent() {
...
@@ -58,40 +58,49 @@ void softwareSerialEvent() {
bool
isWork
=
false
;
bool
isWork
=
false
;
#ifdef DEBUG_BLDC
#ifdef DEBUG_BLDC
int
len
=
0
;
int
len
=
0
;
uint8_t
rbyte
;
//
uint8_t rbyte;
#endif
#endif
if
(
mySerial
.
available
())
{
if
(
mySerial
.
available
())
{
isWork
=
true
;
//
isWork = true;
#ifdef DEBUG_BLDC
#ifdef DEBUG_BLDC
Serial
.
print
(
F
(
"[ bldc ] softwareSerialEvent = read[
"
));
Serial
.
print
ln
(
F
(
"[ bldc ] softwareSerialEvent: Reading from serial...
"
));
#endif
#endif
}
while
(
mySerial
.
available
())
{
while
(
mySerial
.
available
())
{
//#ifndef DEBUG_BLDC
bldc_interface_uart_process_byte
(
mySerial
.
read
());
#ifdef DEBUG_BLDC
len
+=
sizeof
(
uint8_t
);
#endif
/*#else
rbyte = mySerial.read();
bldc_interface_uart_process_byte(rbyte);
Serial.write(rbyte);
len += sizeof(uint8_t);
#endif*/
//delay(2);
}
#ifndef DEBUG_BLDC
bldc_interface_uart_process_byte
(
mySerial
.
read
());
#else
rbyte
=
mySerial
.
read
();
bldc_interface_uart_process_byte
(
rbyte
);
Serial
.
write
(
rbyte
);
len
+=
sizeof
(
uint8_t
);
#endif
}
if
(
isWork
)
{
#ifdef DEBUG_BLDC
#ifdef DEBUG_BLDC
Serial
.
print
(
F
(
"
] len[
"
));
Serial
.
print
(
F
(
"
[ bldc ] softwareSerialEvent: Just read
"
));
Serial
.
print
(
len
);
Serial
.
print
(
len
);
Serial
.
println
(
F
(
"]"
));
Serial
.
println
(
F
(
" bytes."
));
#endif
#endif
is_data_ready
=
true
;
is_data_ready
=
true
;
}
}
else
{
#ifdef DEBUG_BLDC
Serial
.
println
(
F
(
"[ bldc ] softwareSerialEvent: nothing to do... "
));
#endif
}
}
}
/**
/**
...
@@ -102,20 +111,24 @@ void softwareSerialEvent() {
...
@@ -102,20 +111,24 @@ void softwareSerialEvent() {
* @param len
* @param len
* Data array length
* Data array length
*/
*/
static
void
send_packet
(
unsigned
char
*
data
,
int
len
)
{
void
send_packet
(
unsigned
char
*
data
,
int
len
)
{
/*if (len > (PACKET_MAX_PL_LEN + 5)) {
/*if (len > (PACKET_MAX_PL_LEN + 5)) {
return;
return;
}*/
}*/
//data[0] = 4;
// Send the data over UART
// Send the data over UART
#ifdef DEBUG_BLDC
#ifdef DEBUG_BLDC
Serial
.
print
(
F
(
"[ bldc ] send_packet = write["
));
Serial
.
print
(
F
(
"[ bldc ] send_packet = len["
));
Serial
.
write
(
data
,
len
);
Serial
.
print
(
F
(
"] len["
));
Serial
.
print
(
len
);
Serial
.
print
(
len
);
Serial
.
print
(
F
(
"] write["
));
for
(
int
i
=
0
;
i
<
len
;
i
++
){
Serial
.
print
(
data
[
i
]);
Serial
.
print
(
F
(
":"
));
}
//Serial.write(data, len);
Serial
.
println
(
F
(
"]"
));
Serial
.
println
(
F
(
"]"
));
#endif
#endif
mySerial
.
write
(
data
,
len
);
mySerial
.
write
(
data
,
len
);
}
}
...
...
packet.cpp
View file @
85914615
...
@@ -86,20 +86,38 @@ void packet_timerfunc(void) {
...
@@ -86,20 +86,38 @@ void packet_timerfunc(void) {
handler_states
[
i
].
rx_timeout
--
;
handler_states
[
i
].
rx_timeout
--
;
}
else
{
}
else
{
handler_states
[
i
].
rx_state
=
0
;
handler_states
[
i
].
rx_state
=
0
;
/*#ifdef DEBUG_BLDC_P
Serial.println(F("[ bldc ] packet_timerfunc: Timeout!"));
#endif*/
}
}
}
}
}
}
void
packet_process_byte
(
uint8_t
rx_data
,
int
handler_num
)
{
void
packet_process_byte
(
uint8_t
rx_data
,
int
handler_num
)
{
#ifdef DEBUG_BLDC_P
Serial
.
print
(
F
(
"[ bldc ] packet_process_byte = rx_data[ "
));
Serial
.
print
(
rx_data
);
Serial
.
print
(
F
(
" ] handler_states.rx_data[ "
));
Serial
.
print
(
handler_states
[
handler_num
].
rx_state
);
Serial
.
println
(
F
(
" ]"
));
#endif
switch
(
handler_states
[
handler_num
].
rx_state
)
{
switch
(
handler_states
[
handler_num
].
rx_state
)
{
case
0
:
case
0
:
if
(
rx_data
==
2
)
{
if
(
rx_data
==
2
)
{
#ifdef DEBUG_BLDC_P
Serial
.
println
(
F
(
"[ bldc ] packet_process_byte: 1 byte PL len"
));
#endif
// 1 byte PL len
// 1 byte PL len
handler_states
[
handler_num
].
rx_state
+=
2
;
handler_states
[
handler_num
].
rx_state
+=
2
;
handler_states
[
handler_num
].
rx_timeout
=
PACKET_RX_TIMEOUT
;
handler_states
[
handler_num
].
rx_timeout
=
PACKET_RX_TIMEOUT
;
handler_states
[
handler_num
].
rx_data_ptr
=
0
;
handler_states
[
handler_num
].
rx_data_ptr
=
0
;
handler_states
[
handler_num
].
payload_length
=
0
;
handler_states
[
handler_num
].
payload_length
=
0
;
}
else
if
(
rx_data
==
3
)
{
}
else
if
(
rx_data
==
3
)
{
#ifdef DEBUG_BLDC_P
Serial
.
println
(
F
(
"[ bldc ] packet_process_byte: 2 byte PL len"
));
#endif
// 2 byte PL len
// 2 byte PL len
handler_states
[
handler_num
].
rx_state
++
;
handler_states
[
handler_num
].
rx_state
++
;
handler_states
[
handler_num
].
rx_timeout
=
PACKET_RX_TIMEOUT
;
handler_states
[
handler_num
].
rx_timeout
=
PACKET_RX_TIMEOUT
;
...
@@ -112,12 +130,25 @@ void packet_process_byte(uint8_t rx_data, int handler_num) {
...
@@ -112,12 +130,25 @@ void packet_process_byte(uint8_t rx_data, int handler_num) {
case
1
:
case
1
:
handler_states
[
handler_num
].
payload_length
=
(
unsigned
int
)
rx_data
<<
8
;
handler_states
[
handler_num
].
payload_length
=
(
unsigned
int
)
rx_data
<<
8
;
#ifdef DEBUG_BLDC_P
Serial
.
print
(
F
(
"[ bldc ] packet_process_byte = payload_length[ "
));
Serial
.
print
(
handler_states
[
handler_num
].
payload_length
);
Serial
.
println
(
F
(
" ]"
));
#endif
handler_states
[
handler_num
].
rx_state
++
;
handler_states
[
handler_num
].
rx_state
++
;
handler_states
[
handler_num
].
rx_timeout
=
PACKET_RX_TIMEOUT
;
handler_states
[
handler_num
].
rx_timeout
=
PACKET_RX_TIMEOUT
;
break
;
break
;
case
2
:
case
2
:
handler_states
[
handler_num
].
payload_length
|=
(
unsigned
int
)
rx_data
;
handler_states
[
handler_num
].
payload_length
|=
(
unsigned
int
)
rx_data
;
#ifdef DEBUG_BLDC_P
Serial
.
print
(
F
(
"[ bldc ] packet_process_byte = payload_length[ "
));
Serial
.
print
(
handler_states
[
handler_num
].
payload_length
);
Serial
.
println
(
F
(
" ]"
));
#endif
if
(
handler_states
[
handler_num
].
payload_length
>
0
&&
if
(
handler_states
[
handler_num
].
payload_length
>
0
&&
handler_states
[
handler_num
].
payload_length
<=
PACKET_MAX_PL_LEN
)
{
handler_states
[
handler_num
].
payload_length
<=
PACKET_MAX_PL_LEN
)
{
handler_states
[
handler_num
].
rx_state
++
;
handler_states
[
handler_num
].
rx_state
++
;
...
@@ -132,18 +163,31 @@ void packet_process_byte(uint8_t rx_data, int handler_num) {
...
@@ -132,18 +163,31 @@ void packet_process_byte(uint8_t rx_data, int handler_num) {
if
(
handler_states
[
handler_num
].
rx_data_ptr
==
handler_states
[
handler_num
].
payload_length
)
{
if
(
handler_states
[
handler_num
].
rx_data_ptr
==
handler_states
[
handler_num
].
payload_length
)
{
handler_states
[
handler_num
].
rx_state
++
;
handler_states
[
handler_num
].
rx_state
++
;
}
}
#ifdef DEBUG_BLDC_P
Serial
.
print
(
F
(
"[ bldc ] packet_process_byte = Received "
));
Serial
.
print
(
handler_states
[
handler_num
].
rx_data_ptr
);
Serial
.
print
(
F
(
" bytes of "
));
Serial
.
print
(
handler_states
[
handler_num
].
payload_length
);
Serial
.
println
(
F
(
"."
));
#endif
handler_states
[
handler_num
].
rx_timeout
=
PACKET_RX_TIMEOUT
;
handler_states
[
handler_num
].
rx_timeout
=
PACKET_RX_TIMEOUT
;
break
;
break
;
case
4
:
case
4
:
handler_states
[
handler_num
].
crc_high
=
rx_data
;
handler_states
[
handler_num
].
crc_high
=
rx_data
;
handler_states
[
handler_num
].
rx_state
++
;
handler_states
[
handler_num
].
rx_state
++
;
#ifdef DEBUG_BLDC_P
Serial
.
println
(
F
(
"[ bldc ] packet_process_byte = Received crc_high byte."
));
#endif
handler_states
[
handler_num
].
rx_timeout
=
PACKET_RX_TIMEOUT
;
handler_states
[
handler_num
].
rx_timeout
=
PACKET_RX_TIMEOUT
;
break
;
break
;
case
5
:
case
5
:
handler_states
[
handler_num
].
crc_low
=
rx_data
;
handler_states
[
handler_num
].
crc_low
=
rx_data
;
handler_states
[
handler_num
].
rx_state
++
;
handler_states
[
handler_num
].
rx_state
++
;
#ifdef DEBUG_BLDC_P
Serial
.
println
(
F
(
"[ bldc ] packet_process_byte = Received crc_low byte."
));
#endif
handler_states
[
handler_num
].
rx_timeout
=
PACKET_RX_TIMEOUT
;
handler_states
[
handler_num
].
rx_timeout
=
PACKET_RX_TIMEOUT
;
break
;
break
;
...
...
packet.h
View file @
85914615
...
@@ -31,7 +31,7 @@
...
@@ -31,7 +31,7 @@
//#define DEBUG_BLDC_P
//#define DEBUG_BLDC_P
// Settings
// Settings
#define PACKET_RX_TIMEOUT
2
#define PACKET_RX_TIMEOUT
100
#define PACKET_HANDLERS 1
#define PACKET_HANDLERS 1
#define PACKET_MAX_PL_LEN 128 //256
#define PACKET_MAX_PL_LEN 128 //256
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment