//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-#define END_SWITCH_UP 9 //pin 4, exp. port on board
-#define END_SWITCH_DOWN 8 //pin 3, exp. port on board
-#define SWITCH_HOME 19 //pin 6, exp. port on board
+#define END_SWITCH_UP_PIN 9 //pin 4, exp. port on board
+#define END_SWITCH_DOWN_PIN 8 //pin 3, exp. port on board
+#define SWITCH_HOME_PIN 19 //pin 6, exp. port on board
+#define START_PIN 15 //pin 7, exp. port on board
+#define COLOR_PIN 18 //pin 5, exp. port on board
+
+#define START_SEND_PRIOD_FAST 50 /* [miliseconds] */
+#define START_SEND_PRIOD_SLOW 300 /* [miliseconds] */
+#define START_SEND_FAST_COUNT 10 /* How many times to send start with small period (after a change) */
+
+#define LIFT_IRC_VAL_MAX 0x19C
+#define LIFT_IRC_VAL_MIN 0x0
+
+#define IRC_A_PIN 2 //pin 1, exp. port on board
+#define IRC_B_PIN 3 //pin 2, exp. port on board
+
+#define IRC_A_MASK 0x04 //(1<<IRC_A)
+#define IRC_B_MASK 0x08 //(1<<IRC_B)
+#define IRC_AB_MASK 0x0C //((1<<IRC_A)&(1<<IRC_B))
void lift_switches_handler(void);
void lift_switches_handler()
{
- if (IO0PIN & (1<<END_SWITCH_UP)){
- fsm_lift.flags |= CAN_LIFT_SWITCH_UP;
- deb_led_on(LEDR);
- } else {
+ if (IO0PIN & (1<<END_SWITCH_UP_PIN)){
fsm_lift.flags &= ~CAN_LIFT_SWITCH_UP;
deb_led_off(LEDR);
+ } else {
+ fsm_lift.flags |= CAN_LIFT_SWITCH_UP;
+ deb_led_on(LEDR);
}
- if (IO0PIN & (1<<END_SWITCH_DOWN)){
- fsm_lift.flags |= CAN_LIFT_SWITCH_DOWN;
- deb_led_on(LEDR);
- } else {
+ if (IO0PIN & (1<<END_SWITCH_DOWN_PIN)){
fsm_lift.flags &= ~CAN_LIFT_SWITCH_DOWN;
deb_led_off(LEDR);
+ } else {
+ fsm_lift.flags |= CAN_LIFT_SWITCH_DOWN;
+ deb_led_on(LEDR);
}
- if (IO0PIN & (1<<SWITCH_HOME)){
- fsm_lift.flags |= CAN_LIFT_SWITCH_HOME;
- deb_led_on(LEDR);
- } else {
+ if (IO0PIN & (1<<SWITCH_HOME_PIN)){
fsm_lift.flags &= ~CAN_LIFT_SWITCH_HOME;
deb_led_off(LEDR);
+ } else {
+ fsm_lift.flags |= CAN_LIFT_SWITCH_HOME;
+ deb_led_on(LEDR);
}
-}
-
-#define LIFT_IRC_VAL_MAX 0x19C
-#define LIFT_IRC_VAL_MIN 0x0
-
-#define IRC_A 2 //pin 1, exp. port on board
-#define IRC_B 3 //pin 2, exp. port on board
-
-#define IRC_A_MASK 0x04 //(1<<IRC_A)
-#define IRC_B_MASK 0x08 //(1<<IRC_B)
-#define IRC_AB_MASK 0x0C //((1<<IRC_A)&(1<<IRC_B))
-
-int irc_read_tick(uint16_t *last_irc);
-void irc_init(void);
-
-void irc_init(void){
- SET_PIN(PINSEL0, IRC_A, PINSEL_0);
- SET_PIN(PINSEL0, IRC_B, PINSEL_0);
-
- SET_PIN(PINSEL0, END_SWITCH_UP, PINSEL_0);
- SET_PIN(PINSEL0, END_SWITCH_DOWN, PINSEL_0);
+// if (IO0PIN & (1<<IRC_A_PIN)){
+// deb_led_off(LEDR);
+// } else {
+// deb_led_on(LEDR);
+// }
+// if (IO0PIN & (1<<IRC_B_PIN)){
+// deb_led_off(LEDR);
+// } else {
+// deb_led_on(LEDR);
+// }
}
//source code from http://www.vosrk.cz/robotika/Stavba/Enkoder.pdf
-int irc_read_tick(uint16_t *last_irc){
+int32_t irc_read_tick(){
- static char cnt = 0;
+ static uint16_t cnt_up = 0;
+ static uint16_t cnt_down = 0;
+ static uint16_t last_irc = 0;
+ static int32_t position = 0;
uint16_t irc, temp;
irc = IO0PIN & IRC_AB_MASK;
irc ^= IRC_A_MASK;
}
- temp = (irc - *last_irc) & IRC_AB_MASK;
+ temp = (irc - last_irc) & IRC_AB_MASK;
- *last_irc = irc;
+ last_irc = irc;
if (temp == IRC_A_MASK){
/* count 100times slower - we do not need 250 ticks per milimeter*/
- if (--cnt < 100) {
- cnt = 0;
- return -1;
+ if (++cnt_down >= 100) {
+ cnt_down = 0;
+ cnt_up = 0;
+ deb_led_change(LEDB);
+ return --position;
}
} else if (temp == IRC_AB_MASK){
/* count 100times slower - we do not need 250 ticks per milimeter*/
- if (++cnt > 100) {
- cnt = 0;
- return 1;
+ if (++cnt_up >= 100) {
+ cnt_up = 0;
+ cnt_down = 0;
+ deb_led_change(LEDB);
+ return ++position;
}
}
- return 0;
+ return position;
}
void init_motors(void){
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
-#define START_PIN 15 //pin 7, exp. port on board
-#define START_SEND_PRIOD_FAST 50 /* [miliseconds] */
-#define START_SEND_PRIOD_SLOW 300 /* [miliseconds] */
-#define START_SEND_FAST_COUNT 10 /* How many times to send start with small period (after a change) */
-
-
void start_button(void)
{
can_msg_t msg;
init_uart();
// init_adc(ADC_ISR);
- irc_init();
-
-
}
/*********************************************************/
void can_send_status(void)
}
}
-
-
-#define COLOR_PIN 18 //pin 5, exp. port on board
-
void color_handler()
{
static uint32_t color_time = 0;
{
uint32_t main_time = timer_usec;
uint32_t status_time = timer_usec;
-
- static uint16_t last_irc=0;
-
+
//lift motor is motor A, MOTA connctor on board
- init_periphery();
-
- SET_PIN(PINSEL0, START_PIN, PINSEL_0); //init of start pin
- SET_PIN(PINSEL0, 3, PINSEL_0); //init of color pin
- SET_PIN(PINSEL0, 4, PINSEL_0); //init of home pin
-
+ init_periphery();
+
+ SET_PIN(PINSEL0, IRC_A_PIN, PINSEL_0);
+ SET_PIN(PINSEL0, IRC_B_PIN, PINSEL_0);
+
+ SET_PIN(PINSEL0, END_SWITCH_UP_PIN, PINSEL_0);
+ SET_PIN(PINSEL0, END_SWITCH_DOWN_PIN, PINSEL_0);
+
+ SET_PIN(PINSEL0, START_PIN, PINSEL_0); //init of start pin
+ SET_PIN(PINSEL1, (COLOR_PIN - 16), PINSEL_0); //init of color pin
+ SET_PIN(PINSEL1, (SWITCH_HOME_PIN - 16), PINSEL_0); //init of home pin
+
+ IO0DIR &= ~((1<<START_PIN) | (1<<SWITCH_HOME_PIN) | (1 << COLOR_PIN));
+ IO0DIR &= ~((1<<END_SWITCH_UP_PIN) | (1<<END_SWITCH_DOWN_PIN));
+ IO0DIR &= ~((1<<IRC_A_PIN) | (1<<IRC_B_PIN));
send_rs_str("Lift started\n");
fsm_lift.act_pos = 0;
can_send_status();
}
- fsm_lift.act_pos = fsm_lift.act_pos + irc_read_tick(&last_irc);
+ fsm_lift.act_pos = irc_read_tick();
start_button();
color_handler();