Skip to content

Commit fbb96b6

Browse files
committed
adding option to change the angle of multiple servomotors
1 parent edc313f commit fbb96b6

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

examples/servo/main.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,32 @@ int main(void) {
1010
printf("There is no available servo\n");
1111
return -1;
1212
}
13-
13+
returncode_t result = RETURNCODE_EOFF;
1414
uint16_t angle = 0;
15+
uint16_t index = 0; // the first index available.
16+
17+
if (libtock_current_servo_angle(index, &angle) == RETURNCODE_ENODEVICE) {
18+
printf("\n The index number is bigger than the available servomotors\n");
19+
return -1;
20+
}
1521

1622
// Changes the angle of the servomotor from 0 to 180 degrees (waiting 0.1 ms between every change).
1723
for (int i = 0; i <= 180; i++) {
18-
if (libtock_servo_angle(i) == RETURNCODE_SUCCESS) {
24+
// `result` stores the returned code received from the driver.
25+
result = libtock_servo_angle(index, i);
26+
if (result == RETURNCODE_SUCCESS) {
1927
libtocksync_alarm_delay_ms(100);
2028
// Verifies if the function successfully returned the current angle.
21-
if (libtock_current_servo_angle(&angle) == RETURNCODE_SUCCESS) {
22-
printf("The current angle is: %d\n", angle);
29+
if (libtock_current_servo_angle(index, &angle) == RETURNCODE_SUCCESS) {
30+
printf("\nThe current angle is: %d", angle);
2331
} else {
24-
printf("\nThe servomotor is OFF\n");
32+
printf("\n This servo cannot return it's angle.\n");
2533
}
26-
} else {
27-
printf("\nThe angle could not be changed\n");
34+
} else if (result == RETURNCODE_EINVAL){
35+
printf("\nThe angle you provided exceeds 360 degrees\n");
36+
return -1;
37+
} else if (result == RETURNCODE_FAIL){
38+
printf("\nThe angle could not be changed. The provided angle exceeds the servo's limit\n");
2839
return -1;
2940
}
3041
}

libtock/interface/syscalls/servo_syscalls.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ bool libtock_servo_exists(void) {
55
return driver_exists(DRIVER_NUM_SERVO);
66
}
77

8-
returncode_t libtock_servo_angle(uint32_t angle) {
9-
syscall_return_t cval = command(DRIVER_NUM_SERVO, 1, angle, 0);
8+
returncode_t libtock_servo_angle(uint16_t index, uint16_t angle) {
9+
syscall_return_t cval = command(DRIVER_NUM_SERVO, 1, index, angle);
1010
return tock_command_return_novalue_to_returncode(cval);
1111
}
1212

13-
returncode_t libtock_current_servo_angle(uint16_t* angle) {
13+
returncode_t libtock_current_servo_angle(uint16_t index, uint16_t* angle) {
1414
uint32_t value = 0;
15-
syscall_return_t r = command(DRIVER_NUM_SERVO, 2, 0, 0);
15+
syscall_return_t r = command(DRIVER_NUM_SERVO, 2, index, 0);
1616
// Converts the value returned by the servo, stored in `r`,
1717
// and assigns it to `ret`.
1818
returncode_t ret = tock_command_return_u32_to_returncode(r, &value);

libtock/interface/syscalls/servo_syscalls.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ extern "C" {
1212
bool libtock_servo_exists(void);
1313

1414
// Change the angle.
15-
returncode_t libtock_servo_angle(uint32_t angle);
15+
returncode_t libtock_servo_angle(uint16_t index, uint16_t angle );
1616
// Requests the current angle from the servo.
17-
returncode_t libtock_current_servo_angle(uint16_t* angle);
17+
returncode_t libtock_current_servo_angle(uint16_t index, uint16_t* angle);
1818

1919
#ifdef __cplusplus
2020
}
21-
#endif
21+
#endif

0 commit comments

Comments
 (0)