Skip to content

Commit 3404306

Browse files
committed
Merge branch 'fast' of github.com:srobo/servo-v4-fw
2 parents 4dea6a5 + 59d80ab commit 3404306

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

src/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
## You should have received a copy of the GNU Lesser General Public License
1818
## along with this library. If not, see <http://www.gnu.org/licenses/>.
1919
##
20-
FW_VER ?= 4.4.0
20+
FW_VER ?= 4.4
2121

2222
# Name of C file with main function
2323
BINARY = main

src/servo.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212

1313
#define US_TO_TICK(x) ((uint16_t)x / 5)
1414
#define TICK_TO_US(x) (x * 5)
15-
#define TICKS_BETWEEN_EDGES US_TO_TICK(125)
15+
#define TICKS_BETWEEN_EDGES US_TO_TICK(US_BETWEEN_EDGES)
16+
#define NUM_SERVOS_EXTRA (NUM_SERVO_PHASES * SERVOS_PER_PHASE)
1617
static void calculate_phase_steps(uint8_t phase);
1718

1819
static const uint8_t servo_bit_mapping[] = {15, 14, 13, 12, 11, 10, 9, 8, 0, 1, 2, 3};
@@ -26,7 +27,7 @@ typedef struct {
2627
uint16_t pulse; // in timer ticks
2728
} servo_t;
2829

29-
static servo_t servo_state[NUM_SERVOS] = {};
30+
static servo_t servo_state[NUM_SERVOS_EXTRA] = {};
3031
static servo_step_t servo_steps[NUM_SERVO_PHASES][SERVO_STEPS_PER_PHASE] = { 0 };
3132

3233
// loaded from servo_steps at the start of each phase
@@ -56,7 +57,7 @@ static void init_timer(void) {
5657

5758
void servo_init(void) {
5859
// initialise servo state indexes
59-
for (uint8_t i = 0; i < NUM_SERVOS; i++) {
60+
for (uint8_t i = 0; i < NUM_SERVOS_EXTRA; i++) {
6061
servo_state[i].idx = i;
6162
servo_state[i].enabled = false;
6263
servo_state[i].pulse = US_TO_TICK(MIN_SERVO_PULSE);
@@ -184,7 +185,7 @@ static void calculate_phase_steps(uint8_t phase) {
184185
sorted_servo_steps[step].rising = true;
185186
if (step == (SERVOS_PER_PHASE - 1)) {
186187
// this gap is the remaining delay before the first falling edge
187-
sorted_servo_steps[step].next_steps = sorted_servo_states[0].pulse - (TICKS_BETWEEN_EDGES * 3);
188+
sorted_servo_steps[step].next_steps = sorted_servo_states[0].pulse - (TICKS_BETWEEN_EDGES * (SERVOS_PER_PHASE - 1));
188189
} else {
189190
sorted_servo_steps[step].next_steps = TICKS_BETWEEN_EDGES;
190191
}
@@ -222,9 +223,15 @@ static void set_expander_output(uint16_t val) {
222223

223224
// setup transaction to GPIO register
224225
i2c_start_message(I2C_EXPANDER_ADDR);
226+
#if NUM_SERVOS > 8
225227
i2c_send_byte(EXT_GPIOA);
226228
i2c_send_byte((uint8_t)(val & 0xff)); // A-reg first
227229
i2c_send_byte((uint8_t)((val >> 8) & 0xff));
230+
#else
231+
// The first 8 servos are all in the upper byte
232+
i2c_send_byte(EXT_GPIOA + 1);
233+
i2c_send_byte((uint8_t)((val >> 8) & 0xff)); // B-reg only
234+
#endif
228235
i2c_stop_message();
229236
}
230237

@@ -283,7 +290,7 @@ void tim1_cc_isr(void) {
283290
}
284291
}
285292

286-
if (current_servo_step == (SERVO_STEPS_PER_PHASE - 1)) {
293+
if (current_servo_step == (SERVO_STEPS_PER_PHASE - 1)) {
287294
// this phase is complete, stop interrupts until the next phase
288295
timer_disable_irq(TIM1, TIM_DIER_CC1IE);
289296
}

src/servo.h

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,26 @@
33
#include <stdint.h>
44
#include <stdbool.h>
55

6-
#define NUM_SERVOS 12
6+
#define NUM_SERVOS 8
77
#define NUM_SERVO_PHASES 3
8-
#define SERVOS_PER_PHASE (NUM_SERVOS / NUM_SERVO_PHASES)
8+
#define ROUND_UP_DIV(x, y) (uint8_t)(((x) + (y) - 1) / (y))
9+
#define SERVOS_PER_PHASE ROUND_UP_DIV(NUM_SERVOS, NUM_SERVO_PHASES)
910
#define SERVO_STEPS_PER_PHASE (SERVOS_PER_PHASE * 2)
1011

12+
#if NUM_SERVOS > 12
13+
#error "Board only supports 12 servos"
14+
#endif
15+
16+
#if NUM_SERVOS > 8
17+
// 16 bit transfer takes 95uS
18+
#define US_BETWEEN_EDGES 125
19+
#else
20+
// 8 bit transfer takes 72.5uS
21+
#define US_BETWEEN_EDGES 100
22+
#endif
23+
1124
// in uS
12-
#define MIN_SERVO_PULSE 500
25+
#define MIN_SERVO_PULSE (US_BETWEEN_EDGES * SERVOS_PER_PHASE)
1326
#define MAX_SERVO_PULSE 4000
1427

1528
typedef struct {

0 commit comments

Comments
 (0)