carousel.c File Reference

#include <deb_led.h>
#include <uart.h>
#include <engine.h>
#include <powswitch.h>
#include <system_def.h>
#include <servo.h>
#include <can_ids.h>
#include <periph/can.h>
#include <string.h>
Include dependency graph for carousel.c:



#define CAN_SPEED   1000000
 the interrupt for the timer is somehow set in this class perhaps 4
#define CAN_ISR   0
 the interrupt for the timer is somehow set in this class perhaps 4
#define ENG_ISR   1
 the interrupt for the timer is somehow set in this class perhaps 4
#define SERVO_ISR   5
 the interrupt for the timer is somehow set in this class perhaps 4
#define FULLSPEED   50
 the speed of motors while turn at FULLSPEED
#define MIDSPEED   20
 the speed of motors while turn at MIDSPEED
#define LOWSPEED   10
 the speed of motors while turn a LOWSPEED - used to accurate move to possition
#define INIT_POSITION   0
 the initial position of carousel, could be 0..3
#define GLOBALTIMEOUT   6000
 the timeout for carousel to reach requested position
enum  Event { EVENT_ENTRY, EVENT_DO, EVENT_EXIT }
 

the interrupt for the timer is somehow set in this class perhaps 4

More...
enum  Direction { FORWARD, BACKWARD }
 

the interrupt for the timer is somehow set in this class perhaps 4

More...
enum  ErrorState {
  NO_ERROR = 0, ERROR_SENSOR = 1, ERROR_POSITION = 2, ERROR_TIMEOUT = 3,
  NOTICE = 5
}
 

information about error states

  • ERROR_SENSOR - motor should move, so sensor should sense somethign, but doesn't sense anything
  • ERROR_POSITION - the position isn't read correctly while rotating carousel
  • ERROR_TIMEOUT - the requested position wasn't reached in timeout = carouselTimeout
  • NOTICE - some small error
More...
typedef void(* state_fcn )(enum Event my_event)
 state space automat

  • used to change states
  • each state has three "events"
    1. EVENT_ENTRY
    2. EVENT_DO
    3. EVENT_EXIT
  • states are
    1. state_stop
    2. state_moveSlow
    3. state_moveFast
    4. state_returnToPosition

volatile unsigned int validReading = 0
 0 - carusel position signal 0- not valid, 1- valid
volatile unsigned int validPosition = 0
 0..4 read position from carusel
unsigned int timeNextPos = 232
 default time to move to next postion
unsigned int timeNext2Pos = 424
 default time to move to next postion
volatile unsigned int requestedPosition = 0
 the positon the carousel should move to
volatile unsigned int moveCarousel = 0
 request to move the carousel
enum Direction moveDirection = FORWARD
 the direction carousel should move
unsigned int stepsToPos = 1
 distance to reguested position
unsigned int flashLEDB = 0
 when 1, blue led will flash when the sensors sense new valid position (overshoot position)
unsigned int flashLEDY = 0
 when 1 Yellow led will flash when the requested position was set
unsigned int sendCANmessage = 0
 flag to sent CAN data
unsigned int sendCANOKdata = 0
 flag data to send - OK
unsigned int sendCANERRdata = 0
 flag data to send - ERROR
volatile unsigned int recieveCANmessage = 0
 flag indicated CAN data was recieved
volatile unsigned int recieveCANdata = 0
 recieved CAN data
unsigned int systemInit = 0
 flag that show that carousel is just initialising
const unsigned int useKeyboard = 0
 debuging - the next position of carousel is set by terminal
const unsigned int useArray = 0
 debuging - next position is generated automaticly using the array of possition
const unsigned int doInitCarousel = 0
 debuging - if set, program will count constants to move carousel, not really the best ones..
unsigned int carouselTimeOut = 0
 global timeout, the requested position is not reachable
enum ErrorState errorState = NO_ERROR
 the interrupt for the timer is somehow set in this class perhaps 4
unsigned int errorNumber = 0
 the interrupt for the timer is somehow set in this class perhaps 4
unsigned int wasError = 0
 flag that says, that there were an error, this flag is cancelled while new position is received
can_msg_t msg
 pointer to the recieved can message
state_fcn current_state
 current state of carousel, stop, fast_move, slow_move, returnToPosition
unsigned int repeatTXCANtime = 0
 the interrupt for the timer is somehow set in this class perhaps 4
unsigned int repeatErrorCANtime = 0
 the interrupt for the timer is somehow set in this class perhaps 4
unsigned int repeatErrorCAN = 0
 the interrupt for the timer is somehow set in this class perhaps 4
unsigned int prevRequestedPos = INIT_POSITION +1
 at start robot should move therefore the initial position has to be different than this position
int prevMoveCarousel = 0
 the interrupt for the timer is somehow set in this class perhaps 4
const unsigned int sequens [14] = {0,1,2,3,4,3,2,1,0,2,4,3,1,4}
 sequence of carousel positions, used for debuging
unsigned int sequensX = 0
 the interrupt for the timer is somehow set in this class perhaps 4
unsigned int readyToGo = 0
 the interrupt for the timer is somehow set in this class perhaps 4
unsigned int timeToGo = 0
 the interrupt for the timer is somehow set in this class perhaps 4
unsigned int okTimeToBlink = 0
 the interrupt for the timer is somehow set in this class perhaps 4
unsigned int okLEDstate = 0
 the interrupt for the timer is somehow set in this class perhaps 4
unsigned int errTimeToBlink = 0
 the interrupt for the timer is somehow set in this class perhaps 4
unsigned int errLEDstate = 0
 the interrupt for the timer is somehow set in this class perhaps 4
unsigned int errLEDFlash = 0
 the interrupt for the timer is somehow set in this class perhaps 4
unsigned int yBlinkTime = 0
 the interrupt for the timer is somehow set in this class perhaps 4
unsigned int yLEDstate = 0
 the interrupt for the timer is somehow set in this class perhaps 4
enum ErrorState lastErrorState = NO_ERROR
 the interrupt for the timer is somehow set in this class perhaps 4
unsigned int stopFaMoveTime = 0
 the interrupt for the timer is somehow set in this class perhaps 4
unsigned int stopSlMoveTime = 0
 the interrupt for the timer is somehow set in this class perhaps 4
const unsigned int moveSlowTimeOut = 2000
 the interrupt for the timer is somehow set in this class perhaps 4
const unsigned int returnTimeOut = 2000
 the interrupt for the timer is somehow set in this class perhaps 4
unsigned int returnStopTime
 the interrupt for the timer is somehow set in this class perhaps 4
const unsigned int signalToInt [7] = {11,0,3,4,1,2,10}
 used to decode signal from sensors to integer; 0..4 is position, 10 means error, 11 does'n sense any position
const unsigned int intToSignal [5] = {1,4,5,2,3}
 used to get signals from sensors for the position (0..4)
unsigned int stateReading = 0
 0- no signal, 1- some signal
unsigned int reading = 0
 signal that is read
unsigned int prevValidPosition = 0
 the interrupt for the timer is somehow set in this class perhaps 4
unsigned int prevMoveDirection = FORWARD
 init direction should be forward!!
unsigned int signalTimeoutOn = 0
 flag that show if carousel is moving and is checked if new signal comes before timeout
unsigned int lastSignaltime = 0
 the interrupt for the timer is somehow set in this class perhaps 4
unsigned int errorPosRead [5] = {0,0,0,0,0}
 how many times positon was read wrong. once read right, reset this number
unsigned int initPos = 0
 the interrupt for the timer is somehow set in this class perhaps 4
unsigned int setConstants = 1
 the interrupt for the timer is somehow set in this class perhaps 4
void init_perip (void)
 initialisation of ARM periphery

  • servo
  • power switch
  • motor that moves carousel
  • timer
  • UART0
  • CAN

void delay (int interval)
 wait time specified in the interaval int interval - interval in ms
void init_carousel (void)
 Initialize constants for positioning carousel.
unsigned int getCurrentPosition (void)
 reads the sensors !caution! read position can be errorneous
void generatePositions (void)
 used for debuging to generate new positions for the carousel new position could be set by keyboard or read from array sequens[]
void go_to_sleep ()
 set carousel to sleep caused by CAN_PWR_ALERT CAN message
unsigned int getTime (void)
 used as timer
void can_rx (can_msg_t *msg)
 handels messages recieved by the CAN controller
void handle_CAN (void)
 handle CAN bus

  • send messages
    • if carousel position was reached - send the position on CAN and repeat to send the position till the time new position is requested.

void blink_LED (void)
 Blink with the LEDs located on eb_board

  • green LED blink constantly
  • red LED blinks when errorState is ERROR for 5 seconds, when erroeState is NOTICE, LED blinks for 1 sec
  • yellow LED blink when the requested position was reached.

void updatePosition (void)
 Check the sensors Has three states

  • waiting for signal
  • signal was read
  • signal is valid If signal is valid set the value int validPos=1, in the other code it has to be set to back 0.

void state_stop (enum Event my_event)
 State space machine state: state_stop The initial state where the motor is stoped and Carousel is waiting for commands to move to requested postion

  • on EVENT_ENTRY:
    • Stop the motors
  • on EVENT_DO:
    • Wait for moveCarousel==1 .

void state_moveSlow (enum Event my_event)
 State space machine state: state_moveSlow

  • on EVENT_ENTRY:
    • moves the carusel slowly towards the righ position
  • on EVENT_DO:
    • when when the right position is passed go to state_returnToPosition
    • when the position isn't reached in moveSlowTimeOut go to state state_moveFast
  • on EVENT_EXIT:
    • stop motors.

void state_moveFast (enum Event my_event)
 State space machine state: state_moveFast

  • on EVENT_ENTRY:
    • Moving the carousel fast forward towards the right position - turn on the motors fast.

void state_returnToPosition (enum Event my_event)
 State space machine state: state_returnToPosition

  • on EVENT_ENTRY:
    • moves the carusel slowly backwards to the righ position
  • on EVENT_DO:
    • when when the right position is reached go to state_stop and send the CAN message
    • when the position isn't reached in returnTimeOut go to state state_moveFast
  • on EVENT_EXIT:
    • stop motors.

int main (void)
 Main loop.
signed int positionToDistance (unsigned int pos1, unsigned int pos2)
 Count the distance between 2 position.
unsigned int distanceToPos (unsigned int position, signed int distance)
 Count new position.

Detailed Description

Author:
Ondrej Vrzal, ondra.vrzal@gmail.com