]> rtime.felk.cvut.cz Git - eurobot/public.git/commitdiff
zakladni fce, pridani serva
authorJarda Sach <jarin@work.(none)>
Wed, 25 Mar 2009 18:53:18 +0000 (18:53 +0000)
committerJarda Sach <jarin@work.(none)>
Wed, 25 Mar 2009 18:53:18 +0000 (18:53 +0000)
src/eb_vytah_09/Makefile.omk
src/eb_vytah_09/lift.c [new file with mode: 0644]
src/eb_vytah_09/lift.h [new file with mode: 0644]
src/eb_vytah_09/mydefs.h [new file with mode: 0644]
src/eb_vytah_09/serv.c [new file with mode: 0644]
src/eb_vytah_09/serv.h [new file with mode: 0644]

index 1ba64df611c9ab3e647376302031f3e880d89b32..0d8e49bc25c6c74f4590ab42b9ca274c021aaeac 100644 (file)
@@ -2,6 +2,6 @@
 
 bin_PROGRAMS = eb_vytah_09
 
-eb_vytah_09_SOURCES = main.c 
+eb_vytah_09_SOURCES = lift.c serv.c
 eb_vytah_09_LIBS = can ebb
 
diff --git a/src/eb_vytah_09/lift.c b/src/eb_vytah_09/lift.c
new file mode 100644 (file)
index 0000000..31dd6c4
--- /dev/null
@@ -0,0 +1,200 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+//                 Eurobot BLINK TEST  (with LPC2129)
+//
+// Description
+// -----------
+// This software control mechanisms in eurobot. Use it with lpceurobot board
+// Author : Jarda Sach DCE CVUT
+//
+//
+////////////////////////////////////////////////////////////////////////////////
+
+#include <lpc21xx.h>                            /* LPC21xx definitions */
+#include <deb_led.h>
+#include <system_def.h>        
+#include <can_ids.h>
+#include <periph/can.h>
+#include <string.h>
+#include <deb_led.h>
+#include "serv.h"
+#include "engine.h"    
+#include "lift.h"
+
+
+
+unsigned int flags=0x00; // flags bit
+error_messages error_state= NO_ERROR; //public variable content error massage for major unit
+state_fcn current_state; //pointer to current state function,( stop, fast_move, slow_move, returnToPosition)
+state_fcn last_state;//pointer to last state function
+can_msg_t msg;                 // CAN messge
+
+
+void dummy_wait(unsigned int wait) 
+{
+       //unsigned int wait = 50000000;
+       while(--wait)
+         ;
+}
+
+
+//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+void init_ports(void){
+}
+void init_periphery(void){
+       init_engine_A();// initialization of PWM unit
+       init_engine_B();
+       //can_init_baudrate(CAN_SPEED, CAN_ISR, can_rx);//initialization of CAN bus     
+} 
+
+//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+void init_motors(void){
+       engine_A_en(ENGINE_EN_ON);//enable motor A
+       engine_B_en(ENGINE_EN_ON);// ----//----- B
+       engine_A_dir(ENGINE_DWN);//set direction 
+       engine_B_dir(ENGINE_UP);
+       engine_A_pwm(100);      // pwm is in percent, range 0~100~200
+       engine_B_pwm(100);      // pwm is in percent, range 0~100~200
+}
+
+//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+error_messages init_engine(units eng){
+       error_messages err=NO_ERROR;
+       if(tbit(flags,PUCK_LOADED))
+               return(ERROR_PUCK_LOADED);// no initialization is possible if puck is loaded in holder
+       if(!(tbit(flags,DOORS_OPEN)))
+               return(ERROR_DOORS_OPEN);// no initialization is possible if puck is loaded in holder
+       //open the doors
+       //set flag DOORS_OPEN
+       //check puck
+       if(eng==LIFT_ENGINE){
+               move_pusher(ENGINE_DWN,100);// start moving to back bound width max velocity
+               while(tbit(IO1PIN,DOWN_SW))//move pusher until bit is one/ bit is switch input
+                       ; //check timeout 
+               //go up
+       }
+       else{
+               if(eng==PULL_ENGINE){
+                       move_pusher(ENGINE_BW,100);// start moving to back bound width max velocity
+                       while(tbit(IO1PIN,BACK_SW))//move pusher until bit is one/ bit is switch input
+                               ; //check timeout 
+                       //clr pulses,go forward
+               }
+       }
+       return(err);
+}
+
+//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+void move_lift(unsigned char direct,unsigned char velocity){//how many pulzes 
+       engine_A_dir(direct);//set direction for lift
+       engine_A_pwm(velocity); // set velocity, range 0~100~200
+       engine_A_en(ENGINE_EN_ON);//enable motor of lift
+}
+
+//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+void move_pusher(unsigned char direct,unsigned char velocity){//how many pulzes 
+       engine_B_dir(direct);//set direction for lift
+       engine_B_pwm(velocity); // set velocity, range 0~100~200
+       engine_B_en(ENGINE_EN_ON);//enable motor of pusher
+}
+
+void open_doors(void){
+       set_servo(100);
+}
+//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+static void Init_timer(void)
+{
+//             VICVectAddr0 = (unsigned)timer0_isr;        /* Set the ISR vector address  vector0 = highest priority */
+//             VICVectCntl0 = 0x20|4;                      /* Enable this vector and assign Timer IRQ to it */
+//             VICIntEnable = 0x00000010;                  /* Enable the interrupt of timer0*/
+       T0MR0= 0x0ffffffff; // interrupt interval when timer0 overload
+       T0PR = CPU_APB_HZ/1000; // Load prescaler for 1 Msec tick
+       T0MCR = 0x07; //resets timer0, stop timer 0 and generates interrupt when timer0 counter match MR0 register
+       T0TCR = 0x02; //Reset timer 0
+       T0TCR = 0x01; // start timer 0
+}
+
+
+
+//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+//~~~~~~~~~~~~~~ Possible states definition ~~~~~~~~~~~~~~
+//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+void stop(events my_event){
+       switch(my_event){
+               case EVENT_ENTRY:
+                       //stop booth of motors
+                       
+               break;
+               case EVENT_DO:
+                       //wait for commands
+               break;
+               case EVENT_EXIT:
+       
+               break;
+               default:break;
+       }
+}
+
+void move_lift_home(events my_event){
+}
+
+void move_lift_floor1(events my_event){
+}
+
+void move_lift_floor2(events my_event){
+}
+
+void move_lift_floor3(events my_event){
+}
+
+void move_lift_floor4(events my_event){
+}
+
+//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+int main(void){
+       unsigned char last_position;
+// ~~~~~ initialization of peryphery ~~~~~
+       init_periphery();
+       init_motors();//initialization of direction and velocity
+       init_servo(SERVO_PRIORITY);
+       if((init_engine(LIFT_ENGINE)!=NO_ERROR)||(init_engine(PULL_ENGINE)!=NO_ERROR))
+               ;//current_state=state_err_init(EVEN_ENTRY);//goto error state and send message to major unit
+
+// ~~~~~ no error >> initialization successfull completed
+       last_position=HOME;
+       current_state=&stop;
+       current_state(EVENT_ENTRY);// go stop state and wait for any commands from major unit
+// ~~~~~ state machine ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+       for(;;){
+       //      last_state=current_state; // in every expiry trough loop refresh last state, on start position >> booth states are the same
+
+               if(last_state!=current_state)
+               {       // in case of changing state
+                       last_state(EVENT_EXIT); // finish the old state
+                       current_state(EVENT_ENTRY); // initialize the new state
+               }
+
+
+
+
+       }
+
+
+       while(1){       
+               deb_led_change(LEDR);
+               dummy_wait(5000000);    
+               deb_led_change(LEDG);
+               dummy_wait(5000000);
+               deb_led_change(LEDB);
+               dummy_wait(5000000);
+       } 
+}
+
+
+
diff --git a/src/eb_vytah_09/lift.h b/src/eb_vytah_09/lift.h
new file mode 100644 (file)
index 0000000..1a7c90d
--- /dev/null
@@ -0,0 +1,109 @@
+#ifndef DEFH
+#define DEFH
+       //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+       //~~~~~~~~~~ definition of interrupt priority ~~~~~~~~~~~
+       //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+       #define SERVO_PRIORITY 7 //0 is the biggest priority
+       
+       //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+       //~~~~~~~ definition of macros for bit operations ~~~~~~~
+       //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+       #define         sbit(WORD,BIT)  (WORD|=(1<<BIT))        //set bit\r
+       #define         cbit(WORD,BIT)  (WORD&=~(1<<BIT))//clear bit\r
+       #define         tbit(WORD,BIT)  (WORD&(1<<BIT)) //test bit\r
+
+       //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+       //~~~~~~~~~~~ definition of moving directions ~~~~~~~~~~~
+       //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+       #define ENGINE_BW       1       ///< Backward direction 
+       #define ENGINE_FW       0       ///< Forward direction
+       #define ENGINE_UP       1       ///< Up direction       
+       #define ENGINE_DWN      0       ///< Down direction
+
+       //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+       //~~~~~~~~~~~ definition of terminal switches ~~~~~~~~~~~
+       //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+       #define BACK_SW         16
+       #define FORWARD_SW      17
+       #define DOWN_SW         18
+       #define UP_SW           19
+
+       //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+       //~~~~~~~~~~~~ definition of lift positions ~~~~~~~~~~~~~
+       //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+       #define HOME    0
+       #define FLOOR1  1
+       #define FLOOR2  2
+       #define FLOOR3  3
+       #define FLOOR4  4
+
+       //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+       //~~~~~~~~~~ definition of flag register bits ~~~~~~~~~~~
+       //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+       #define DOORS_OPEN 0
+       #define PUCK_LOADED 1   
+
+       //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+       //~~~~~~~~~ definition of CAN bus cominication ~~~~~~~~~~
+       //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+       #define CAN_SPEED       1000000
+       #define CAN_ISR         0
+
+
+       typedef enum {//possible errors 
+               NO_ERROR, 
+               ERROR_LIFT_IRC, 
+               ERROR_LIFT_POSITION, 
+               ERROR_LIFT_TIMEOUT,
+               ERROR_PUSHER_IRC, 
+               ERROR_PUSHER_POSITION, 
+               ERROR_PUSHER_TIMEOUT,
+               ERROR_PUCK_LOADED,
+               ERROR_DOORS_OPEN
+       } error_messages;
+
+
+       typedef enum {//mark which unit we want controlled
+               LIFT_ENGINE, 
+               PULL_ENGINE
+       } units;
+
+       typedef enum {//mark which unit we want controlled
+               DOWN, 
+               UP,
+               BACK,
+               FORWARD
+       } direction;
+
+       typedef enum {// events of each state of state machine
+               EVENT_ENTRY,
+               EVENT_DO,
+               EVENT_EXIT
+       } events; 
+
+
+       typedef void (*state_fcn)(events my_event);//pointer to function returning void and one input parametr
+
+//************************************************************************************
+       void move_lift(unsigned char direct,unsigned char velocity);
+       void move_pusher(unsigned char direct,unsigned char velocity);
+       void open_doors(void);  
+       error_messages init_engine(units eng);// calibration and move to start position
+       void init_motors(void);//initialization PWM duty, direction
+       void init_ports(void);
+       static void Init_timer(void);
+       unsigned int getTime(void);
+       void can_rx(can_msg_t *msg);//CAN bus receive rutine
+       
+//************************************************************************************
+//****************** declaration of possible states **********************************
+//************************************************************************************
+       void stop(events my_event);
+       void move_lift_home(events my_event);
+       void move_lift_floor1(events my_event);
+       void move_lift_floor2(events my_event);
+       void move_lift_floor3(events my_event);
+       void move_lift_floor4(events my_event);
+//     void state_move_pusher(events my_event);
+       
+#endif
diff --git a/src/eb_vytah_09/mydefs.h b/src/eb_vytah_09/mydefs.h
new file mode 100644 (file)
index 0000000..b004c8d
--- /dev/null
@@ -0,0 +1,9 @@
+#ifndef DEFH
+#define DEFH
+
+#define sbit(BYTE,BIT) BYTE|(0x01<<BIT)
+#define cbit(BYTE,BIT) BYTE&(0xfe<<BIT)
+//#define tbit(BYTE,BIT)
+
+
+#endif
diff --git a/src/eb_vytah_09/serv.c b/src/eb_vytah_09/serv.c
new file mode 100644 (file)
index 0000000..efac35d
--- /dev/null
@@ -0,0 +1,87 @@
+
+
+#include <lpc21xx.h>                          // LPC21XX Peripheral Registers
+#include <deb_led.h>
+#include <system_def.h>
+#include "serv.h"
+
+
+#define SERVO2 (1<<10)
+#define SERVO0 (1<<12)
+#define SERVO1 (1<<13)
+
+
+
+#define TIM_EMR_NOTHING 0
+#define TIM_EMR_CLEAR  1
+#define TIM_EMR_SET    2
+#define TIM_EMR_TOGLE  3
+
+#define TIM_EMR_PIN_ON 1
+#define TIM_EMR_PIN_OFF 0
+
+#define TIME20MS       ((CPU_APB_HZ) / 50)
+#define SERVOTICK      (((CPU_APB_HZ / 50) / 20) / 256)
+
+
+unsigned char servo[3];
+
+
+
+// ---------------- SERVO PART -------------------------------
+
+
+void tc1 (void) __attribute__ ((interrupt));
+
+void tc1 (void)   {
+       
+       time_ms +=20;
+       T1EMR |= (TIM_EMR_PIN_ON<<0) | (TIM_EMR_PIN_ON<<1) | (TIM_EMR_PIN_ON<<3);
+       T1MR3 = (servo[2] + 256) * SERVOTICK;
+       if (T1IR != 4)
+       {                       
+           __deb_led_on(LEDR); 
+       }  
+       T1IR        = 4;                            // Vynulovani priznaku preruseni
+       VICVectAddr = 0;                            // Potvrzeni o obsluze preruseni
+}
+
+void set_servo(char position)
+{
+       servo[2] = position;
+}
+
+/* Setup the Timer Counter 1 Interrupt */
+void init_servo (unsigned rx_isr_vect)
+{
+
+       IO0DIR|= SERVO2;                        // enables servo output
+       IO0SET|= SERVO2;                        // sets to High level 
+
+       PINSEL0 &= ~((PINSEL_3 << 24)   | (PINSEL_3 << 26));
+       PINSEL0 |= (PINSEL_2 << 24) | (PINSEL_2 << 26);
+       PINSEL1 &= ~(PINSEL_3 << 8);
+       PINSEL1 |= (PINSEL_1 << 8);
+       
+       servo[2] = 0xFF;
+
+       T1PR = 0;
+       T1MR2 = TIME20MS;
+       T1MR0 = (servo[2] + 256) * SERVOTICK;   
+       T1MCR = (3<<6);                 // interrupt on MR1
+
+       T1EMR = (TIM_EMR_PIN_ON<<0) | (TIM_EMR_PIN_ON<<1) | (TIM_EMR_PIN_ON<<3) \
+                               | (TIM_EMR_CLEAR << 4) | (TIM_EMR_CLEAR << 6) | (TIM_EMR_CLEAR << 10);
+
+
+       T1TCR = 1;                                  // Starts Timer 1 
+
+
+       ((uint32_t*)&VICVectAddr0)[rx_isr_vect] = (unsigned long)tc1;          // Nastaveni adresy vektotu preruseni
+       ((uint32_t*)&VICVectCntl0)[rx_isr_vect] = 0x20 | 0x5;                    // vyber casovece pro preruseni
+       VICIntEnable = (1<<5);                  // Povoli obsluhu preruseni
+}
+
+
+// ---------------- powSitch PART -------------------------------
+
diff --git a/src/eb_vytah_09/serv.h b/src/eb_vytah_09/serv.h
new file mode 100644 (file)
index 0000000..964f712
--- /dev/null
@@ -0,0 +1,40 @@
+/**
+ * @file   servo.h
+ * 
+ * @brief  FIXME
+ *     This file provides simply how-to use eb_ebb library.
+ *     From main function is called init_perip function 
+ *     where is initialized servos, engines, power switch,
+ *     CAN,ADC and serial port. After this initialization is shown 
+ *  how to control each devices. This sample also include simply 
+ *  sample of sending and receiving CAN message.
+ * 
+ */
+
+
+
+#ifndef SERVO_H
+#define SERVO_H
+
+
+
+/** gobal time
+ *  @note incremented twenty 20ms, overrun every 1194hours
+ */
+volatile unsigned int time_ms;
+
+/** Initialize servos
+ *  @note All three servos - should be fixed FIXME
+ */
+void init_servo(unsigned rx_isr_vect);
+
+
+/** Sets serv position
+ *  @return 0 
+ *  @note VPB = APB   - name conflict FIXME
+ *  @param     servo   define servo
+ *  @param     position        new position for servo
+ */
+void set_servo(char position);
+
+#endif