2018-08-23 20:21:58 +02:00
|
|
|
// Polar kinematics stepper pulse time generation
|
|
|
|
//
|
2019-06-20 21:44:45 +02:00
|
|
|
// Copyright (C) 2018-2019 Kevin O'Connor <kevin@koconnor.net>
|
2018-08-23 20:21:58 +02:00
|
|
|
//
|
|
|
|
// This file may be distributed under the terms of the GNU GPLv3 license.
|
|
|
|
|
|
|
|
#include <math.h> // sqrt
|
|
|
|
#include <stdlib.h> // malloc
|
|
|
|
#include <string.h> // memset
|
|
|
|
#include "compiler.h" // __visible
|
|
|
|
#include "itersolve.h" // struct stepper_kinematics
|
2019-10-27 18:23:03 +01:00
|
|
|
#include "trapq.h" // move_get_coord
|
2018-08-23 20:21:58 +02:00
|
|
|
|
|
|
|
static double
|
|
|
|
polar_stepper_radius_calc_position(struct stepper_kinematics *sk, struct move *m
|
|
|
|
, double move_time)
|
|
|
|
{
|
|
|
|
struct coord c = move_get_coord(m, move_time);
|
|
|
|
return sqrt(c.x*c.x + c.y*c.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
static double
|
|
|
|
polar_stepper_angle_calc_position(struct stepper_kinematics *sk, struct move *m
|
|
|
|
, double move_time)
|
|
|
|
{
|
|
|
|
struct coord c = move_get_coord(m, move_time);
|
|
|
|
// XXX - handle x==y==0
|
2019-06-20 21:44:45 +02:00
|
|
|
double angle = atan2(c.y, c.x);
|
|
|
|
if (angle - sk->commanded_pos > M_PI)
|
|
|
|
angle -= 2. * M_PI;
|
|
|
|
else if (angle - sk->commanded_pos < -M_PI)
|
|
|
|
angle += 2. * M_PI;
|
|
|
|
return angle;
|
2018-08-23 20:21:58 +02:00
|
|
|
}
|
|
|
|
|
2019-10-27 16:14:40 +01:00
|
|
|
static void
|
|
|
|
polar_stepper_angle_post_fixup(struct stepper_kinematics *sk)
|
|
|
|
{
|
|
|
|
// Normalize the stepper_bed angle
|
|
|
|
if (sk->commanded_pos < -M_PI)
|
|
|
|
sk->commanded_pos += 2 * M_PI;
|
|
|
|
else if (sk->commanded_pos > M_PI)
|
|
|
|
sk->commanded_pos -= 2 * M_PI;
|
|
|
|
}
|
|
|
|
|
2018-08-23 20:21:58 +02:00
|
|
|
struct stepper_kinematics * __visible
|
|
|
|
polar_stepper_alloc(char type)
|
|
|
|
{
|
|
|
|
struct stepper_kinematics *sk = malloc(sizeof(*sk));
|
|
|
|
memset(sk, 0, sizeof(*sk));
|
2019-10-27 16:14:40 +01:00
|
|
|
if (type == 'r') {
|
2019-10-27 16:06:46 +01:00
|
|
|
sk->calc_position_cb = polar_stepper_radius_calc_position;
|
2019-10-27 16:14:40 +01:00
|
|
|
} else if (type == 'a') {
|
2019-10-27 16:06:46 +01:00
|
|
|
sk->calc_position_cb = polar_stepper_angle_calc_position;
|
2019-10-27 16:14:40 +01:00
|
|
|
sk->post_cb = polar_stepper_angle_post_fixup;
|
|
|
|
}
|
2019-10-29 04:13:58 +01:00
|
|
|
sk->active_flags = AF_X | AF_Y;
|
2018-08-23 20:21:58 +02:00
|
|
|
return sk;
|
|
|
|
}
|