Carousel application

Carousel control application

More...



#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

Carousel control application


Define Documentation

#define CAN_ISR   0

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

#define CAN_SPEED   1000000

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 FULLSPEED   50

the speed of motors while turn at FULLSPEED

#define GLOBALTIMEOUT   6000

the timeout for carousel to reach requested position

#define INIT_POSITION   0

the initial position of carousel, could be 0..3

#define LOWSPEED   10

the speed of motors while turn a LOWSPEED - used to accurate move to possition

#define MIDSPEED   20

the speed of motors while turn at MIDSPEED

#define SERVO_ISR   5

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


Typedef Documentation

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


Enumeration Type Documentation

enum Direction

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

Enumerator:
FORWARD 
BACKWARD 
enum ErrorState

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

Enumerator:
NO_ERROR 
ERROR_SENSOR 
ERROR_POSITION 
ERROR_TIMEOUT 
NOTICE 
enum Event

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

Enumerator:
EVENT_ENTRY 
EVENT_DO 
EVENT_EXIT 

Function Documentation

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.

Here is the call graph for this function:

Here is the caller graph for this function:

void can_rx ( can_msg_t *  msg  ) 

handels messages recieved by the CAN controller

Parameters:
msg the recieved CAN message pointer

Here is the call graph for this function:

Here is the caller graph for this function:

void delay ( int  interval  ) 

wait time specified in the interaval int interval - interval in ms

‘Ò‹@

need to implement overflow when needed durations in days..

Here is the caller graph for this function:

unsigned int distanceToPos ( unsigned int  position,
signed int  distance 
)

Count new position.

Parameters:
position 0..4, the current position
distance -4..+4, distance from the current position, + forward, - backward from current position
Returns:
0..4, new position

Here is the caller graph for this function:

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[]

Here is the call graph for this function:

Here is the caller graph for this function:

unsigned int getCurrentPosition ( void   ) 

reads the sensors !caution! read position can be errorneous

Parameters:
@return position 0..4 if sense sone data, 10 means error, 11 does'n sense any position

Here is the caller graph for this function:

unsigned int getTime ( void   ) 

used as timer

Parameters:
@return returns the number in timer T0TC

Here is the caller graph for this function:

void go_to_sleep (  ) 

set carousel to sleep caused by CAN_PWR_ALERT CAN message

Here is the caller graph for this function:

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.

CAN id is CAN_CAROUSEL

  • if there is error in moving carousel send message with CAN_ERROR id.

recieve message

  • if new position is recieved and is in state_stop, request new position
    • if positioning of the carousel is in progress and different position is requested make next state state_stop and set new requested position -if debuging is set, the position of carousel is controlled by the the keyboard or by array of positions

Here is the call graph for this function:

Here is the caller graph for this function:

void init_carousel ( void   ) 

Initialize constants for positioning carousel.

Move n times by set speed and measure the time interval Not very accurate, it is better to set constants manualy

Here is the call graph for this function:

Here is the caller graph for this function:

void init_perip ( void   ) 

initialisation of ARM periphery

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

Here is the call graph for this function:

Here is the caller graph for this function:

int main ( void   ) 

Main loop.

In main loop

  • At the beginning
    • ARM is initialised
    • Carousel is initialised and moved to INIT_POSITION
    • The first state is state_stop
  • Then in infinet loop
    1. The state space machine runs
    2. Check for carouselTimeOut
    3. Handle CAN
    4. Blink LEDs
    5. Update positions
    6. Check for state change

Here is the call graph for this function:

signed int positionToDistance ( unsigned int  pos1,
unsigned int  pos2 
)

Count the distance between 2 position.

Parameters:
pos1 0..4, new position
pos2 0..4, old position
Returns:
-2..+2 the distance, positive distace mean that pos 1 is forward to pos 2

Here is the caller graph for this function:

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.

  • on EVENT_DO:
    • When the carousel overshoot the right position - stop, the next state is state_returnToPosition.
  • on EVENT_EXIT:
    • stop motors

Here is the call graph for this function:

Here is the caller graph for this function:

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.

Here is the call graph for this function:

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.

Here is the call graph for this function:

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 .

If requested position is read on sensors - wait for new request. If requeset position is valid position (last requested) but not read from the sensors, new state is state_returnToPosition. If the requested position is different from the last one. then find out the direction to move and change state to state_moveFast.

  • on EVENT_EXIT:
    • nothing

Here is the call graph for this function:

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.

It also set the value readPositon= n, where n(0,4) is the last read valid position

In this function it is also checked for an errors in reading signal

! this is crucial, read error of requested position

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

unsigned int carouselTimeOut = 0

global timeout, the requested position is not reachable

current state of carousel, stop, fast_move, slow_move, returnToPosition

const unsigned int doInitCarousel = 0

debuging - if set, program will count constants to move carousel, not really the best ones..

unsigned int errLEDFlash = 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 errorNumber = 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

enum ErrorState errorState = NO_ERROR

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 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 initPos = 0

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

const unsigned int intToSignal[5] = {1,4,5,2,3}

used to get signals from sensors for the position (0..4)

enum ErrorState lastErrorState = NO_ERROR

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

unsigned int lastSignaltime = 0

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

volatile unsigned int moveCarousel = 0

request to move the carousel

enum Direction moveDirection = FORWARD

the direction carousel should move

const unsigned int moveSlowTimeOut = 2000

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

can_msg_t msg

pointer to the recieved can message

unsigned int okLEDstate = 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

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

unsigned int prevMoveDirection = FORWARD

init direction should be forward!!

unsigned int prevRequestedPos = INIT_POSITION +1

at start robot should move therefore the initial position has to be different than this position

unsigned int prevValidPosition = 0

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

unsigned int reading = 0

signal that is read

unsigned int readyToGo = 0

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

volatile unsigned int recieveCANdata = 0

recieved CAN data

volatile unsigned int recieveCANmessage = 0

flag indicated CAN data was recieved

unsigned int repeatErrorCAN = 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 repeatTXCANtime = 0

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

volatile unsigned int requestedPosition = 0

the positon the carousel should move to

unsigned int returnStopTime

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 sendCANERRdata = 0

flag data to send - ERROR

unsigned int sendCANmessage = 0

flag to sent CAN data

unsigned int sendCANOKdata = 0

flag data to send - OK

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 setConstants = 1

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

unsigned int signalTimeoutOn = 0

flag that show if carousel is moving and is checked if new signal comes before timeout

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

unsigned int stateReading = 0

0- no signal, 1- some signal

unsigned int stepsToPos = 1

distance to reguested position

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

unsigned int systemInit = 0

flag that show that carousel is just initialising

unsigned int timeNext2Pos = 424

default time to move to next postion

unsigned int timeNextPos = 232

default time to move to next postion

unsigned int timeToGo = 0

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

const unsigned int useArray = 0

debuging - next position is generated automaticly using the array of possition

const unsigned int useKeyboard = 0

debuging - the next position of carousel is set by terminal

volatile unsigned int validPosition = 0

0..4 read position from carusel

volatile unsigned int validReading = 0

0 - carusel position signal 0- not valid, 1- valid

unsigned int wasError = 0

flag that says, that there were an error, this flag is cancelled while new position is received

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