Overview | Main | State Machine | Events | IR | Tape | Drivetrain | PWM | Flash | Two Minute Timer | Bump

events.c
events.h

char getIRstate(char sensor): get the events-based state of a given IR sensor

char checkEvents(void): check for events to pass on to the state machine

The events module polls the low-level modules and converts changes in states of sensors or the robot itself into events that are useful to the state machine. Since it contains the definitions for the interesting ranges of duty cycle values for IR sensing, it also provides a public function to get the state, as opposed to the current value, of any IR sensor.

PSEUDOCODE

(Download Pseudocode .doc 39kb)

char getIRstate(char sensor)
    Return the current state of the sensor that was passed in

char checkEvents(void)
    Get the current state of the timer
    If the timer is not expired and the previous state was not not expired
        Set the timer’s state to not expired
        Return “no event”
    Else if the timer is expired and the previous state was not expired
        Set the timer’s state to expired
        Return “two-minute timer expired”
    Get the current state of the front bump sensor
    If the front bump sensor is on and the previous state was not on
        Set the front bump sensor’s state to on
        Return “front bump sensor now on”
    Else if the front bump sensor is off and the previous state was not off
        Set the front bump sensor’s state to off
        Return “front bump sensor now off”
    Get the current state of the back bump sensor
    If the back bump sensor is on and the previous state was not on
        Set the back bump sensor’s state to on
        Return “back bump sensor now on”
    Else if the back bump sensor is off and the previous state was not off
        Set the back bump sensor’s state to off
        Return “back bump sensor now off”
    Get the current state of the center tape sensor
    If the center tape sensor is black and the previous state was not black
        Set the center tape sensor’s state to black
        Return “center tape sensor now black”
    Else if the center tape sensor is green and the previous state was not green
        Set the center tape sensor’s state to green
        Return “center tape sensor now green”
    Else if the center tape sensor is white and the previous state was not white
        Set the center tape sensor’s state to white
        Return “center tape sensor now white”
    Get the current state of the front left tape sensor
    If the front left tape sensor is black and the previous state was not black
        Set the front left tape sensor’s state to black
        Return “front left tape sensor now black”
    Else if the front left tape sensor is green and the previous state was not green
        Set the front left tape sensor’s state to green
        Return “front left tape sensor now green”
    Else if the front left tape sensor is white and the previous state was not white
        Set the front left tape sensor’s state to white
        Return “front left tape sensor now white”
    Get the current state of the front right tape sensor
    If the front right tape sensor is black and the previous state was not black
        Set the front right tape sensor’s state to black
        Return “front right tape sensor now black”
    Else if the front right tape sensor is green and the previous state was not green
        Set the front right tape sensor’s state to green
        Return “front right tape sensor now green”
    Else if the front right tape sensor is white and the previous state was not white
        Set the front right tape sensor’s state to white
        Return “front right tape sensor now white”
    Get the current state of the back left tape sensor
    If the back left tape sensor is black and the previous state was not black
        Set the back left tape sensor’s state to black
        Return “back left tape sensor now black”
    Else if the back left tape sensor is green and the previous state was not green
        Set the back left tape sensor’s state to green
        Return “back left tape sensor now green”
    Else if the back left tape sensor is white and the previous state was not white
        Set the back left tape sensor’s state to white
        Return “back left tape sensor now white”
    Get the current state of the back right tape sensor
    If the back right tape sensor is black and the previous state was not black
        Set the back right tape sensor’s state to black
        Return “back right tape sensor now black”
    Else if the back right tape sensor is green and the previous state was not green
        Set the back right tape sensor’s state to green
        Return “back right tape sensor now green”
    Else if the back right tape sensor is white and the previous state was not white
        Set the back right tape sensor’s state to white
        Return “back right tape sensor now white”
    Get the current duty cycle of the front 12-inch IR sensor
    If the front 12-inch IR sensor is under the zero duty cycle high threshold
        If the previous state was not zero duty cycle
            Set the front 12-inch IR sensor’s state to zero duty cycle
            Return “front 12-inch IR sensor now zero duty cycle”
    Else if the front 12-inch IR sensor is within the thirty duty cycle band
        If the previous state was not thirty duty cycle
            Set the front 12-inch IR sensor’s state to thirty duty cycle
            Return “front 12-inch IR sensor now thirty duty cycle”
    Else if the front 12-inch IR sensor is within the seventy duty cycle band
        If the previous state was not seventy duty cycle
            Set the front 12-inch IR sensor’s state to seventy duty cycle
            Return “front 12-inch IR sensor now seventy duty cycle”
    Else
        If the previous state was not other duty cycle
            Set the front 12-inch IR sensor’s state to other duty cycle
            Return “front 12-inch IR sensor now other duty cycle”
    Get the current duty cycle of the back 12-inch IR sensor
    If the back 12-inch IR sensor is under the zero duty cycle high threshold
        If the previous state was not zero duty cycle
            Set the back 12-inch IR sensor’s state to zero duty cycle
            Return “back 12-inch IR sensor now zero duty cycle”
    Else if the back 12-inch IR sensor is within the thirty duty cycle band
        If the previous state was not thirty duty cycle
            Set the back 12-inch IR sensor’s state to thirty duty cycle
            Return “back 12-inch IR sensor now thirty duty cycle”
    Else if the back 12-inch IR sensor is within the seventy duty cycle band
        If the previous state was not seventy duty cycle
            Set the back 12-inch IR sensor’s state to seventy duty cycle
            Return “back 12-inch IR sensor now seventy duty cycle”
    Else
        If the previous state was not other duty cycle
            Set the back 12-inch IR sensor’s state to other duty cycle
            Return “back 12-inch IR sensor now other duty cycle”
    Get the current duty cycle of the front left 9-inch IR sensor
    If the front left 9-inch IR sensor is under the zero duty cycle high threshold
        If the previous state was not zero duty cycle
            Set the front left 9-inch IR sensor’s state to zero duty cycle
            Return “front left 9-inch IR sensor now zero duty cycle”
    Else if the front left 9-inch IR sensor is within the fifty duty cycle band
        If the previous state was not fifty duty cycle
            Set the front left 9-inch IR sensor’s state to fifty duty cycle
            Return “front left 9-inch IR sensor now fifty duty cycle”
    Else
        If the previous state was not other duty cycle
            Set the front left 9-inch IR sensor’s state to other duty cycle
            Return “front left 9-inch IR sensor now other duty cycle”
    Get the current duty cycle of the front right 9-inch IR sensor
    If the front right 9-inch IR sensor is under the zero duty cycle high threshold
        If the previous state was not zero duty cycle
            Set the front right 9-inch IR sensor’s state to zero duty cycle
            Return “front right 9-inch IR sensor now zero duty cycle”
    Else if the front right 9-inch IR sensor is within the fifty duty cycle band
        If the previous state was not fifty duty cycle
            Set the front right 9-inch IR sensor’s state to fifty duty cycle
            Return “front right 9-inch IR sensor now fifty duty cycle”
    Else
        If the previous state was not other duty cycle
            Set the front right 9-inch IR sensor’s state to other duty cycle
            Return “front right 9-inch IR sensor now other duty cycle”
    Get the current duty cycle of the front center 9-inch IR sensor
    If the front center 9-inch IR sensor is under the zero duty cycle high threshold
        If the previous state was not zero duty cycle
            Set the front center 9-inch IR sensor’s state to zero duty cycle
            Return “front center 9-inch IR sensor now zero duty cycle”
    Else if the front center 9-inch IR sensor is within the fifty duty cycle band
        If the previous state was not fifty duty cycle
            Set the front center 9-inch IR sensor’s state to fifty duty cycle
            Return “front center 9-inch IR sensor now fifty duty cycle”
    Else
        If the previous state was not other duty cycle
            Set the front center 9-inch IR sensor’s state to other duty cycle
            Return “front center 9-inch IR sensor now other duty cycle”
    Get the current duty cycle of the back left 9-inch IR sensor
    If the back left 9-inch IR sensor is under the zero duty cycle high threshold
        If the previous state was not zero duty cycle
            Set the back left 9-inch IR sensor’s state to zero duty cycle
            Return “back left 9-inch IR sensor now zero duty cycle”
    Else if the back left 9-inch IR sensor is within the fifty duty cycle band
        If the previous state was not fifty duty cycle
            Set the back left 9-inch IR sensor’s state to fifty duty cycle
            Return “back left 9-inch IR sensor now fifty duty cycle”
    Else
        If the previous state was not other duty cycle
            Set the back left 9-inch IR sensor’s state to other duty cycle
            Return “back left 9-inch IR sensor now other duty cycle”
    Get the current duty cycle of the back right 9-inch IR sensor
    If the back right 9-inch IR sensor is under the zero duty cycle high threshold
        If the previous state was not zero duty cycle
            Set the back right 9-inch IR sensor’s state to zero duty cycle
            Return “back right 9-inch IR sensor now zero duty cycle”
    Else if the back right 9-inch IR sensor is within the fifty duty cycle band
        If the previous state was not fifty duty cycle
            Set the back right 9-inch IR sensor’s state to fifty duty cycle
            Return “back right 9-inch IR sensor now fifty duty cycle”
    Else
        If the previous state was not other duty cycle
            Set the back right 9-inch IR sensor’s state to other duty cycle
            Return “back right 9-inch IR sensor now other duty cycle”
    Get the current duty cycle of the back center 9-inch IR sensor
    If the back center 9-inch IR sensor is under the zero duty cycle high threshold
        If the previous state was not zero duty cycle
            Set the back center 9-inch IR sensor’s state to zero duty cycle
            Return “back center 9-inch IR sensor now zero duty cycle”
    Else if the back center 9-inch IR sensor is within the fifty duty cycle band
        If the previous state was not fifty duty cycle
            Set the back center 9-inch IR sensor’s state to fifty duty cycle
            Return “back center 9-inch IR sensor now fifty duty cycle”
    Else
        If the previous state was not other duty cycle
            Set the back center 9-inch IR sensor’s state to other duty cycle
            Return “back center 9-inch IR sensor now other duty cycle”
    Get the current state of the flash sensor
    If the flash sensor is on and the previous state was not on
        Set the flash sensor’s state to on
        Return “flash sensor now on”
    Else if the flash sensor is off and the previous state was not off
        Set the flash sensor’s state to off
        Return “flash sensor now off”
    Return “no event”