Commit 8a20000f authored by Werner Sembach's avatar Werner Sembach
Browse files

Remove warnings, calibrate servo, change servo api

parent f7825d71
......@@ -6,7 +6,7 @@
extern "C" {
#endif
void adc_init();
void adc_init(void);
uint16_t adc_get(uint8_t channel);
#ifdef __cplusplus
......
......@@ -9,7 +9,7 @@
extern "C" {
#endif
void dc_init();
void dc_init(void);
void dc_set(uint8_t direction, uint8_t speed);
......
......@@ -9,9 +9,9 @@
extern "C" {
#endif
void button_init();
void button_init(void);
int button_get();
int button_get(void);
#ifdef __cplusplus
}
......
......@@ -11,9 +11,11 @@
extern "C" {
#endif
void servo_init();
void servo_init(void);
void servo_set(uint8_t servo, uint8_t position);
void servo_set_percent(uint8_t servo, double position);
void servo_set_degree(uint8_t servo, double position);
#ifdef __cplusplus
}
......
......@@ -4,18 +4,18 @@
extern "C" {
#endif
void all_init();
void all_init(void);
void print_gyro();
void print_accelero();
void print_degree();
void print_gyro(void);
void print_accelero(void);
void print_degree(void);
double get_degree_short();
double get_degree_long();
double get_degree_max();
double get_degree_short(void);
double get_degree_long(void);
double get_degree_max(void);
bool simulate_spring();
void block_spring();
bool simulate_spring(void);
void block_spring(void);
#ifdef __cplusplus
}
......
......@@ -19,7 +19,34 @@ static cyg_thread threaddata;
void main_thread(cyg_addrword_t arg) {
all_init();
bool button_pressed = false;
unsigned int loop = 0;
while(1) {
printf("Loop: %8d\n", loop++);
servo_set_percent(SERVO_0, 0.0);
servo_set_percent(SERVO_1, 0.0);
servo_set_percent(SERVO_2, 0.0);
servo_set_percent(SERVO_3, 0.0);
cyg_thread_delay(5000);
servo_set_percent(SERVO_0, 0.5);
servo_set_percent(SERVO_1, 0.5);
servo_set_percent(SERVO_2, 0.5);
servo_set_percent(SERVO_3, 0.5);
cyg_thread_delay(5000);
servo_set_percent(SERVO_0, 1.0);
servo_set_percent(SERVO_1, 1.0);
servo_set_percent(SERVO_2, 1.0);
servo_set_percent(SERVO_3, 1.0);
cyg_thread_delay(5000);
}
/*bool button_pressed = false;
bool spring_broken = false;
bool upside_down = false;
......@@ -48,7 +75,7 @@ void main_thread(cyg_addrword_t arg) {
else {
block_spring();
}
}
}*/
}
void cyg_user_start(void) {
......
#include "ServoMotor.h"
#include <cstdio>
#include <cyg/hal/hal_arch.h>
#include <libopencm3/cm3/common.h>
......@@ -13,11 +15,15 @@
#define GPIO_D15 CYGHWR_HAL_STM32_PIN_ALTFN_OUT(D,15, 2, PUSHPULL,NONE,LOW)
// The timer ticks per microsecond. Can be adjusted by measuring PWM on an oscilloscope.
#define PWM_TIMER_TICKS_PER_MICROSECOND 86
#define PWM_TIMER_TICKS_PER_MICROSECOND 84
// PWM Period. Must be more then maximum high time.
#define PWM_PERIOD (3000 * PWM_TIMER_TICKS_PER_MICROSECOND)
// Use this to calibrate the minimum and maximum position of your servo. Default: 150 and 300
#define MIN_UPTIME 155
#define MAX_UPTIME 305
static void pwm_init(uint32_t timer, uint8_t channel, uint32_t period) {
// Function stolen from somewhere on stackoverflow
......@@ -60,7 +66,7 @@ static void pwm_init(uint32_t timer, uint8_t channel, uint32_t period) {
timer_enable_oc_output(timer, chan); // set CCxE bit in TIMx_CCER (enable output)
// Initialize all counters in the register
switch (timer) {
switch(timer) {
case TIM1:
TIM1_EGR |= TIM_EGR_UG;
break;
......@@ -85,10 +91,11 @@ static void pwm_init(uint32_t timer, uint8_t channel, uint32_t period) {
case TIM8:
TIM8_EGR |= TIM_EGR_UG;
break;
default:;
}
}
static void pwm_setup(void) {
static void pwm_setup() {
rcc_periph_clock_enable(RCC_TIM4);
pwm_init(TIM4, 1, PWM_PERIOD);
pwm_init(TIM4, 2, PWM_PERIOD);
......@@ -115,21 +122,33 @@ extern "C" void servo_init() {
CYGHWR_HAL_STM32_GPIO_SET(GPIO_D15);
}
extern "C" void servo_set(uint8_t servo, uint8_t position) {
extern "C" void servo_set_percent(uint8_t servo, double position) {
if (position > 1.0) {
printf("Error: servo_set_percent only accepts values between 0.0 and 1.0");
return;
};
switch (servo) {
case SERVO_0:
timer_set_oc_value(TIM4, TIM_OC1, 150 * PWM_TIMER_TICKS_PER_MICROSECOND + (position * 325 * PWM_TIMER_TICKS_PER_MICROSECOND / 255));
timer_set_oc_value(TIM4, TIM_OC1, (uint32_t)(MIN_UPTIME * PWM_TIMER_TICKS_PER_MICROSECOND + (position * MAX_UPTIME * PWM_TIMER_TICKS_PER_MICROSECOND)));
break;
case SERVO_1:
timer_set_oc_value(TIM4, TIM_OC2, 150 * PWM_TIMER_TICKS_PER_MICROSECOND + (position * 325 * PWM_TIMER_TICKS_PER_MICROSECOND / 255));
timer_set_oc_value(TIM4, TIM_OC2, (uint32_t)(MIN_UPTIME * PWM_TIMER_TICKS_PER_MICROSECOND + (position * MAX_UPTIME * PWM_TIMER_TICKS_PER_MICROSECOND)));
break;
case SERVO_2:
timer_set_oc_value(TIM4, TIM_OC3, 150 * PWM_TIMER_TICKS_PER_MICROSECOND + (position * 325 * PWM_TIMER_TICKS_PER_MICROSECOND / 255));
timer_set_oc_value(TIM4, TIM_OC3, (uint32_t)(MIN_UPTIME * PWM_TIMER_TICKS_PER_MICROSECOND + (position * MAX_UPTIME * PWM_TIMER_TICKS_PER_MICROSECOND)));
break;
case SERVO_3:
timer_set_oc_value(TIM4, TIM_OC4, 150 * PWM_TIMER_TICKS_PER_MICROSECOND + (position * 325 * PWM_TIMER_TICKS_PER_MICROSECOND / 255));
timer_set_oc_value(TIM4, TIM_OC4, (uint32_t)(MIN_UPTIME * PWM_TIMER_TICKS_PER_MICROSECOND + (position * MAX_UPTIME * PWM_TIMER_TICKS_PER_MICROSECOND)));
break;
default:
break;
}
}
extern "C" void servo_set_degree(uint8_t servo, double position) {
if (position > 180) {
printf("Error: servo_set_degree only accepts values between 0 and 180");
return;
};
servo_set_percent(servo, position / 180.0);
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment