Aruna
Stepper.cpp
Go to the documentation of this file.
1 //
2 // Created by noeel on 04-02-20.
3 //
4 
5 #include "aruna/driver/Stepper.h"
6 
7 using namespace aruna;
8 using namespace aruna::driver;
10 
11  set_speed(speed);
12  return err_t::OK;
13 }
14 
16 // TODO half stepping
17  err_t last_err_msg;
18  for (int i = 0; i < pins_count; ++i) {
19  if (i == active_pin_index)
20  continue;
21  last_err_msg = set_pin(pins[i], !active_high);
22  }
23  last_err_msg = set_pin(get_pin(direction), active_high);
24 
25  return last_err_msg;
26 }
27 
29  bool active_high)
30  : Actuator(axis), pins(pins), pins_count(pins_count),
31  active_high(active_high) {
32 // TODO creating two stepper object causes error on registering log
33 // TODO break_on_idle bool parameter. If true: lock motor when speed is zero; false: release motor is speed is zero.
34  log = new log::channel_t("stepper");
35 
36  asked_set_mutex = xSemaphoreCreateMutex();
37  if (asked_set_mutex == NULL) {
38  log->error("failed to create mutex");
39  }
40  if (xTaskCreate(_timer_task_wrapper, "stepper timer", 2048, this, 10, &timer_task_handle) != pdTRUE)
41  log->error("failed to create stepper timer task");
43 
44 }
45 
47  clear_pins();
48  vTaskDelete(timer_task_handle);
49 }
50 
51 uint8_t Stepper::get_pin(movement::axis_mask_t direction, uint8_t n) {
52  uint8_t ret_val = pins[active_pin_index + n];
53 // circular buffer
54  if (n == 0 and direction == movement::axis_mask_t::DIRECTION_PLUS) {
55 // move to next pin
57  } else if (n == 0 and direction == movement::axis_mask_t::DIRECTION_MIN) {
58 // move to prev pin
59  active_pin_index = ((int32_t) (active_pin_index - 1)) < 0 ? pins_count - 1 : active_pin_index - 1;
60  }
61  return ret_val;
62 }
63 
65  uint16_t speed = 0;
67  uint16_t delay_ms = 0;
68  bool first_time = true;
69  err_t err_msg;
70  while (true) {
71 
72  if (speed == 0)
73 // wait indefenitly if speed is zero
74  ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
75 
76  if (ulTaskNotifyTake(pdTRUE, 0) or first_time) {
77  first_time = false;
78 // update our variables
79  if (xSemaphoreTake(asked_set_mutex, portMAX_DELAY) != pdTRUE) {
80  log->error("failed to take mutex");
81  }
82  speed = asked_speed;
83  direction = asked_direction;
84  if (xSemaphoreGive(asked_set_mutex) != pdTRUE) {
85  log->error("failed to give mutex");
86  }
87 // TODO refine delay to more reasonable speeds
88  delay_ms = 65536 - speed;
89 
90  }
91 // loop the pins!
92  err_msg = do_step(direction);
93  if (err_msg != err_t::OK)
94  log->error("failed to step: %s", err_to_char.at(err_msg));
95  vTaskDelay(delay_ms / portTICK_PERIOD_MS);
96 
97  }
98 
99 }
100 
101 void Stepper::_timer_task_wrapper(void *_this) {
102  (static_cast<Stepper *>(_this))->timer_task();
103 }
104 
105 
107  err_t msg = err_t::OK;
108  for (int i = 0; i < pins_count; ++i) {
109  msg = init_pin(pins[i]);
110  if (msg != err_t::OK) {
111  log->error("failed to init pin %i: %s", pins[i], err_to_char.at(msg));
112  }
113  }
114  return msg;
115 }
116 
118  err_t msg = err_t::OK;
119  for (int i = 0; i < pins_count; ++i) {
120  msg = clear_pin(pins[i]);
121  if (msg != err_t::OK) {
122  log->error("failed to close pin %i: %s", pins[i], err_to_char.at(msg));
123  }
124  }
125  return msg;
126 }
127 
128 void Stepper::set_speed(int16_t speed) {
129  if (xSemaphoreTake(asked_set_mutex, portMAX_DELAY) != pdTRUE)
130  log->error("failed to take mutex");
131  asked_speed = speed;
133  if (xSemaphoreGive(asked_set_mutex) != pdTRUE)
134  log->error("failed to give mutex");
135  xTaskNotifyGive(timer_task_handle);
136 }
137 
138 void Stepper::do_steps(int32_t steps) {
139 // TODO
140 }
141 
Definition: comm.cpp:14
static void _timer_task_wrapper(void *_this)
wrapper for timer_task(), as FreeRTOS only wants to create tasks from static functions ...
Definition: Stepper.cpp:101
void timer_task()
task to set the next phase on time.
Definition: Stepper.cpp:64
const uint8_t * pins
Definition: Stepper.h:25
const std::map< err_t, char * > err_to_char
Definition: arunaTypes.h:54
Stepper(uint8_t *pins, size_t pins_count, movement::axis_mask_t axis, bool active_high)
Stepper motor.
Definition: Stepper.cpp:28
virtual err_t init_pin(uint8_t pin_nr)=0
init single pin as output
size_t active_pin_index
Definition: Stepper.h:23
TaskHandle_t timer_task_handle
Definition: Stepper.h:24
err_t clear_pins()
reset the pins back to initial state (run when program is finished)
Definition: Stepper.cpp:117
~Stepper()
destructor.
Definition: Stepper.cpp:46
movement::axis_mask_t asked_direction
Definition: Stepper.h:22
Actuator(axis_mask_t axis=axis_mask_t::NONE)
Actuator object, used by the movement module for vehicle movement.
Definition: Actuator.cpp:12
err_t init_pins()
initialize the pins for output
Definition: Stepper.cpp:106
void do_steps(int32_t steps)
Do an x number of steps.
Definition: Stepper.cpp:138
err_t do_step(movement::axis_mask_t direction)
Do a single step in a direction.
Definition: Stepper.cpp:15
const size_t pins_count
Definition: Stepper.h:26
err_t _set(movement::axis_mask_t axisMask, int16_t speed) override
Implementation of axis movement, this function is called from set(...).
Definition: Stepper.cpp:9
virtual err_t clear_pin(uint8_t pin_nr)
clear single pin after program is finished
Definition: Stepper.h:62
err_t startup_error
error when constructing gets put here, read before usage.
Definition: Actuator.h:45
const bool active_high
Definition: Stepper.h:27
int error(const char *format,...)
log error message
Definition: log.cpp:90
SemaphoreHandle_t asked_set_mutex
Definition: Stepper.h:20
uint16_t asked_speed
Definition: Stepper.h:21
void set_speed(int16_t speed)
Set speed of the stepper motor.
Definition: Stepper.cpp:128
virtual err_t set_pin(uint8_t pin_nr, bool value)=0
set pin level high or low
log::channel_t * log
Definition: Stepper.h:28
uint8_t get_pin(movement::axis_mask_t direction, uint8_t n=0)
pop pin from circular array buffer.
Definition: Stepper.cpp:51