hi every one i am strucked while thinking of this i am working on LPC2148 RTOS , i have to run 3 stepper motors X axix,y axis and Z axis and control their directions using using computer hyperterminal (i am planning to use hyperterminal as a KEYBOAD for controlling the stepper motor directions, to START and to STOP )... please can anyone tell me how to implement this using RTOS
i have 3 stepper motors,each SM has 2 external pins(CLK & DIRECTION) so i have used 3 pins for clk generation 3 pins for direction(-ve or -ve ) so all the above pins are connected to each switches to control their clock and direction
i have created 8 tasks tsk1- Criate all tsks and delete itself tsk2 - check all the SM key press, if any key is pressed execute that particular tsk only if key is released stop executing that tsk tsk3 - rotate SM1(start clk) and set +ve direction tsk4 - rotate SM1(start clk) and set -ve direction
tsk5 - rotate SM2(start clk) and set +ve direction tsk6 - rotate SM2(start clk) and set -ve direction
tsk7 - rotate SM3(start clk) and set +ve direction tsk8 - rotate SM3(start clk) and set -ve direction
please tell me how to switch to perticular tsk if that tsk key is pressed? i hv attached the code here
#include<lpc214x.h> #include <RTX_Config.h> #include"lcd.h" /* Include type and function declarations for RTX */ #include <RTL.h> #include"hdr_files.h" #include "drill_machine.h" OS_TID tsk1; OS_TID tsk2; OS_TID tsk3; OS_TID tsk4; OS_TID tsk5; OS_TID tsk6; OS_TID tsk7; OS_TID tsk8; // Forward reference. void main_tsk (void) __task; void check_key (void) __task; void check_XP (void) __task; void check_XN (void) __task; void check_YP (void) __task; void check_YN (void) __task; void check_ZP (void) __task; void check_ZN (void) __task; unsigned char xp,xn,yp,yn,zp,zn; /******************************************************************************* Criate all tsks and delete itself ********************************************************************************/ void main_tsk (void) __task { os_tsk_prio_self (1); tsk1 = os_tsk_self (); // get own task identification number tsk2 = os_tsk_create (check_key,1); // start task and assign ID tsk3 = os_tsk_create (check_XP,1); // start task and assign ID tsk4 = os_tsk_create (check_XN,1); // start task and assign ID tsk5 = os_tsk_create (check_YP,1); // start task and assign ID tsk6 = os_tsk_create (check_YN,1); // start task and assign ID tsk7 = os_tsk_create (check_ZP,1); // start task and assign ID tsk8 = os_tsk_create (check_ZN,1); // start task and assign ID os_tsk_delete_self(); //deleting the criated tsk because that is not required }
/******************************************************************************* check all the SM key press, if any key is pressed execute that particular tsk only if key is released stop executing that tsk ********************************************************************************/ void check_key (void) __task { while(1) { if(READ_XP==0) { //os_tsk_prio(tsk3, 2); os_evt_set (0x0003, tsk3);//if key pressed is X+ve or SM1 +ve key pressed } //run this tsk else os_evt_clr (0x0003, tsk3); //else stop running this tsk if(READ_XN==0) os_evt_set (0x0004, tsk4);//if key pressed is X+ve or SM1 +ve key pressed else os_evt_clr (0x0004, tsk4); //else stop running this tsk if(READ_YP==0) os_evt_set (0x0005, tsk5);//if key pressed is X+ve or SM1 +ve key pressed else os_evt_clr (0x0005, tsk5);//else stop running this tsk if(READ_YN==0) os_evt_set (0x0006, tsk6);//if key pressed is X+ve or SM1 +ve key pressed else os_evt_clr (0x0006, tsk6);//else stop running this tsk if(READ_ZP==0) os_evt_set (0x0007, tsk7);//if key pressed is X+ve or SM1 +ve key pressed else os_evt_clr (0x0007, tsk7);//else stop running this tsk if(READ_ZN==0) os_evt_set (0x0009, tsk8);//if key pressed is X+ve or SM1 +ve key pressed else os_evt_clr (0x0009, tsk8);//else stop running this tsk } } /******************************************************************************* start tsk if key pressed is x+ve ********************************************************************************/ void check_XP (void) __task { while (1) { //os_evt_wait_and (0x0003, 5000); DIR_X_SET //set X +ve direction run_xclock(); //run clk for the SM } } /******************************************************************************* start tsk if key pressed is x-ve ********************************************************************************/ void check_XN (void) __task { while (1) { DIR_X_CLEAR //set X -ve direction //lcd_print_string(2,2,"XN"); run_xclock(); //run clk for the SM } } /******************************************************************************* start tsk if key pressed is y+ve ********************************************************************************/ void check_YP (void) __task { while (1) { DIR_Y_SET //set y+ve direction //lcd_print_string(2,4,"YP"); run_yclock(); //run clk for the SM } } /******************************************************************************* start tsk if key pressed is y-ve ********************************************************************************/ void check_YN (void) __task { while (1) { DIR_Y_CLEAR //set y-ve direction //lcd_print_string(2,6,"YN"); run_yclock(); //run clk for the SM } } /******************************************************************************* start tsk if key pressed is Z+ve ********************************************************************************/ void check_ZP (void) __task { while (1) { DIR_Z_SET //set z +ve direction //lcd_print_string(2,8,"ZP"); run_zclock(); //run clk for the SM } } /******************************************************************************* start tsk if key pressed is Z-ve ********************************************************************************/ void check_ZN (void) __task { while (1) { DIR_Z_CLEAR //set z-ve direction run_zclock(); //run clk for the SM // os_tsk_pass(); } } /******************************************************************************* initialises the all the ports ********************************************************************************/ void init_SM_ports() { port_pin_dir(1,18,INPUT); //X +VE made as input port_pin_dir(0,21,INPUT); //X -VE made as input port_pin_dir(1,19,INPUT); //Y +VE made as input port_pin_dir(0,20,INPUT); //Y-VE made as input port_pin_dir(0,22,INPUT); //Z +VE made as input port_pin_dir(1,17,INPUT); //Z -VE made as input port_pin_dir(0,4,OUTPUT); //Direction 0 made as output port_pin_dir(0,5,OUTPUT); //clock 0 made as output port_pin_dir(0,3,OUTPUT); //Direction 1 made as output port_pin_dir(0,6,OUTPUT); //clock 1 made as output port_pin_dir(0,2,OUTPUT); //Direction 2 made as output port_pin_dir(0,7,OUTPUT); //clock 2 made as output } int main (void) { init_SM_ports(); lcd_init(); lcd_print_string(1,0,"test"); os_sys_init (main_tsk); //return 0; }
So you are not even going to let the stepper motors run, but only allow them to tick one step up or down depending on detection of key presses?
Then you really, really have no reason for having all these tasks, since you don't seem to intend to run multiple motors concurrently.
For "normal" stepper motor electronics, you normally have a set of goals that you try to fulfill concurrently.
So stepper motor 1 may have a goal to run counter-clockwise 1000 pulses within 15 seconds. Stepper motor 2 may have a goal to run clockwise 700 pulses within 15 seconds.
The above may move a plotter pen in a diagonal over a plotter table, while drawing a straight line by having the two motors select different speeds to get the required angle.
So unless the processor have hardware acceleration for the motors, it's common to have the control program run a single loop, stepping a time counter at a high speed. And for each iteration (time step), the code figures out if one or more of the connected motors should take a full, half or micro step forward or backward or stay still.
No reason for multiple threads. If it's important that multiple motors takes the steps at exactly the same time, the code can precompute all signals and then just have to emit the clock pulses for the motor(s) that should step.
But often, it doesn't matter if a 45 degree diagonal is performed as step x, step y, step x, step y, i.e. the two motors are ticked one at a time. With small enough steps, the end result will look the same. Especially since different motors may have different strength and/or have different amount of mass, making it impossible for two motors to step concurrently and get them to perform the one-step movement so precisely that both motors have done 50% of the step at the same time.