-
Notifications
You must be signed in to change notification settings - Fork 921
Add example for servo motor via PWM #140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Skipper7718
wants to merge
3
commits into
raspberrypi:develop
Choose a base branch
from
Skipper7718:feature
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
add_executable(servo main.c servo.c) | ||
|
||
target_link_libraries(servo pico_stdlib hardware_pwm hardware_clocks) | ||
|
||
pico_add_extra_outputs(servo) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#include "pico/stdlib.h" | ||
#include "servo.h" | ||
#include <stdbool.h> | ||
|
||
int main() | ||
{ | ||
servo_init(1, 50); | ||
for(;;) | ||
{ | ||
servo_put(1, 0, true); | ||
sleep_ms(1000); | ||
|
||
servo_put(1, 90, true); | ||
sleep_ms(1000); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#include "servo.h" | ||
#include "pico/stdlib.h" | ||
#include "hardware/pwm.h" | ||
#include "hardware/clocks.h" | ||
#include <stdbool.h> | ||
|
||
#define DUTY_MIN 2400 | ||
#define DUTY_MAX 8000 | ||
#define TOP_MAX 65534 | ||
|
||
long map(long x, long in_min, long in_max, long out_min, long out_max) { | ||
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; | ||
} | ||
|
||
void servo_put(int gpio, int angle, bool wait_write) | ||
{ | ||
uint slice = pwm_gpio_to_slice_num(gpio); | ||
uint channel = pwm_gpio_to_channel(gpio); | ||
uint32_t top = pwm_hw->slice[slice].top; | ||
int duty_u16 = map(angle, 0, 180, DUTY_MIN, DUTY_MAX); | ||
uint32_t cc = duty_u16 * (top + 1) / 65535; | ||
pwm_set_chan_level(slice, channel, cc); | ||
pwm_set_enabled(slice, true); | ||
|
||
if(wait_write) | ||
sleep_ms(1000); | ||
} | ||
|
||
void servo_init(int gpio, int base_frequency) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
{ | ||
gpio_set_function(gpio, GPIO_FUNC_PWM); | ||
uint slice = pwm_gpio_to_slice_num(gpio); | ||
uint channel = pwm_gpio_to_channel(gpio); | ||
|
||
|
||
uint32_t source_hz = clock_get_hz(clk_sys); | ||
uint freq = 50; | ||
Comment on lines
+36
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inconsistent indentation. |
||
uint32_t div16_top = 16 * source_hz / freq; | ||
uint32_t top = 1; | ||
for (;;) { | ||
if (div16_top >= 16 * 5 && div16_top % 5 == 0 && top * 5 <= TOP_MAX) { | ||
div16_top /= 5; | ||
top *= 5; | ||
} else if (div16_top >= 16 * 3 && div16_top % 3 == 0 && top * 3 <= TOP_MAX) { | ||
div16_top /= 3; | ||
top *= 3; | ||
} else if (div16_top >= 16 * 2 && top * 2 <= TOP_MAX) { | ||
div16_top /= 2; | ||
top *= 2; | ||
} else { | ||
break; | ||
} | ||
} | ||
pwm_hw->slice[slice].div = div16_top; | ||
pwm_hw->slice[slice].top = top; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include <stdbool.h> | ||
/** | ||
* @brief initialize PWM pins and frequency clock | ||
* | ||
* @param gpio PWM Pin | ||
* @param base_frequency set Servo Frequency - 50Hz is common | ||
*/ | ||
void servo_init(int gpio, int base_frequency); | ||
|
||
|
||
/** | ||
* @brief Map a range of values to another range | ||
* @return long | ||
*/ | ||
long map(long x, long in_min, long in_max, long out_min, long out_max); | ||
|
||
/** | ||
* @brief Write an angle to the Servo | ||
* | ||
* @param gpio PWM pin attached to the servo | ||
* @param angle Angle from 1-180 | ||
* @param wait_write Wait for write to finish | ||
*/ | ||
void servo_put(int gpio, int angle, bool wait_write); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is documented as "@param wait_write Wait for write to finish", in which case 1000ms seems like an excessively long time? 🤷
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My motors are slow and with a smaller delay my motor do not finish the turn properly