/* * Copyright (c) 2012-2014 Wind River Systems, Inc. * * SPDX-License-Identifier: Apache-2.0 */ #include <zephyr/zephyr.h> #include <sys/printk.h> #include <drivers/uart.h> #include <drivers/gpio.h> #include <cJSON.h> #define SLEEP_TIME_MS 30000 #define RECEIVE_BUFF_SIZE 10 const float displacement_speed = 127.3867; /* 1mm displacement time in millisecond */ cJSON *root; static struct gpio_callback gpio_cb1; static struct gpio_callback gpio_cb2; static const struct gpio_dt_spec led0 = GPIO_DT_SPEC_GET(DT_ALIAS(led0), gpios); static const struct gpio_dt_spec led1 = GPIO_DT_SPEC_GET(DT_ALIAS(led1), gpios); static const struct gpio_dt_spec led2 = GPIO_DT_SPEC_GET(DT_ALIAS(led2), gpios); static const struct gpio_dt_spec led3 = GPIO_DT_SPEC_GET(DT_ALIAS(led3), gpios); const struct device *gpio= DEVICE_DT_GET(DT_NODELABEL(gpio0)); int motor_pos = 0; /*Reference position of the motor*/ int rotation_check = 0; /* 0 represent motor is steady, +1 means clockwise rotation and -1 means counter-clockwise rotation*/ static int motor_controller(bool enable, bool direction, const struct gpio_dt_spec *pin1, const struct gpio_dt_spec *pin2) { if(!enable) { gpio_pin_configure_dt(pin1, GPIO_OUTPUT_INACTIVE); gpio_pin_configure_dt(pin2, GPIO_OUTPUT_INACTIVE); rotation_check = 0; } else { gpio_pin_configure_dt(pin1, GPIO_OUTPUT_INACTIVE); gpio_pin_configure_dt(pin2, GPIO_OUTPUT_INACTIVE); if(direction) { /* Counter Clockewise direction Movement */ gpio_pin_configure_dt(pin1, GPIO_OUTPUT_ACTIVE); } else { /* Clockwise direction Movement */ gpio_pin_configure_dt(pin2, GPIO_OUTPUT_ACTIVE); } } return 1; } static void motor_up(const struct device *dev, struct gpio_callback *cb, uint32_t pins) { (void) dev; (void) cb; (void) pins; if(rotation_check == 0) { rotation_check = 1; } if(rotation_check > 0 ) { motor_pos++; } printk("Motor_up: cw rotation, Motor Pos:%d\n\r",motor_pos); } static void motor_down(const struct device *dev, struct gpio_callback *cb, uint32_t pins) { (void) dev; (void) cb; (void) pins; if(rotation_check == 0) { rotation_check = -1; } if(rotation_check < 0) { motor_pos--; } printk("Motor_down: Rotation ccw, Motor Pos:%d\n\r",motor_pos); } int main(void) { int ret = 0; int num = 1; (void) ret; (void) num; printk("1. Welcome to the motor controller\n\r"); /* Verify that the UART device is ready */ if (!device_is_ready(gpio)){ printk("UART device not ready\r\n"); return 1 ; } /* Verify that the controller devices are ready */ if (!device_is_ready(led0.port)){ printk("GPIO device is not ready\r\n"); return 1; } num = 2; /*configure pins as an input pin to */ gpio_pin_configure_dt(&led2, GPIO_INPUT); gpio_pin_configure_dt(&led3, GPIO_INPUT); gpio_pin_interrupt_configure_dt(&led2, GPIO_INT_EDGE_RISING); gpio_pin_interrupt_configure_dt(&led3, GPIO_INT_EDGE_RISING); gpio_init_callback(&gpio_cb1, motor_up, BIT(led2.pin)); gpio_init_callback(&gpio_cb2, motor_down, BIT(led3.pin)); gpio_add_callback(led3.port, &gpio_cb2); gpio_add_callback(led2.port, &gpio_cb1); while(num) { /*k_msleep(300); motor_controller(true, false, &led0, &led1); k_msleep(1350); motor_controller(false, false, &led0, &led1); k_msleep(3000);*/ motor_controller(true, true, &led0, &led1); k_msleep(2700); motor_controller(false, true, &led0, &led1); k_msleep(500); num--; } while (1) { k_msleep(SLEEP_TIME_MS); } }
I am trying to write the code for motor controller. I am need to move motor to certain number of ticks based on the given input.
Currently I am using n20 motor for testing.
Along with motor there is one encoder is also added in the top of motor. Encoder is having two GPIO output pin (c1, and c2) using those I am able to get to know whether clockwise or anticlockwise movement is happening. Using motor encoder I want to control movement to certain number of ticks based on given input.