Psi Swarm robot library version 0.9

Fork of PsiSwarmV9 by Psi Swarm Robot

Committer:
jah128
Date:
Mon May 14 15:35:38 2018 +0000
Revision:
20:1bc6c6dc477b
Parent:
18:9204f74069b4
Updated?

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jah128 0:d6269d17c8cf 1 /* University of York Robotics Laboratory PsiSwarm Library: Colour Sensors Header File
jah128 13:adbd827234a4 2 *
jah128 14:2f1ad77d281e 3 * Copyright 2017 University of York
jah128 6:b340a527add9 4 *
jah128 13:adbd827234a4 5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
jah128 6:b340a527add9 6 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
jah128 6:b340a527add9 7 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS
jah128 13:adbd827234a4 8 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
jah128 6:b340a527add9 9 * See the License for the specific language governing permissions and limitations under the License.
jah128 6:b340a527add9 10 *
jah128 0:d6269d17c8cf 11 * File: colour.h
jah128 0:d6269d17c8cf 12 *
jah128 0:d6269d17c8cf 13 * (C) Dept. Electronics & Computer Science, University of York
jah128 0:d6269d17c8cf 14 * James Hilder, Alan Millard, Alexander Horsfield, Homero Elizondo, Jon Timmis
jah128 0:d6269d17c8cf 15 *
jah128 14:2f1ad77d281e 16 * PsiSwarm Library Version: 0.9
jah128 0:d6269d17c8cf 17 *
jah128 14:2f1ad77d281e 18 * June 2017
jah128 0:d6269d17c8cf 19 *
jah128 0:d6269d17c8cf 20 *
jah128 13:adbd827234a4 21 */
jah128 0:d6269d17c8cf 22
jah128 0:d6269d17c8cf 23
jah128 0:d6269d17c8cf 24 #ifndef COLOUR_H
jah128 0:d6269d17c8cf 25 #define COLOUR_H
jah128 0:d6269d17c8cf 26
jah128 15:66be5ec52c3b 27 #define ENABLE 0x00
jah128 15:66be5ec52c3b 28 #define ATIME 0x01
jah128 15:66be5ec52c3b 29 #define CONTROL 0x0F
jah128 15:66be5ec52c3b 30 #define CDATA 0x14
jah128 15:66be5ec52c3b 31
jah128 13:adbd827234a4 32 /**
jah128 13:adbd827234a4 33 * The Colour class contains the functions for reading the base-mounted and top-mounted I2C colour sensors (optional).
jah128 16:50686c07ad07 34 *
jah128 16:50686c07ad07 35 * Functions for use with the TAOS TCS34725 and TCS34721 I2C colour sensors
jah128 16:50686c07ad07 36 *
jah128 16:50686c07ad07 37 * Example:
jah128 16:50686c07ad07 38 * @code
jah128 16:50686c07ad07 39 * #include "psiswarm.h"
jah128 16:50686c07ad07 40 *
jah128 16:50686c07ad07 41 * int main() {
jah128 16:50686c07ad07 42 * init();
jah128 16:50686c07ad07 43 * colour.start_colour_ticker(150);
jah128 16:50686c07ad07 44 while(1) {
jah128 16:50686c07ad07 45 pc.printf("Colour: %s\n",colour.get_colour_string(colour.get_detected_colour()));
jah128 16:50686c07ad07 46 wait(0.20);
jah128 16:50686c07ad07 47 }
jah128 16:50686c07ad07 48 * }
jah128 16:50686c07ad07 49 * @endcode
jah128 7:aa5a4a257895 50 */
jah128 13:adbd827234a4 51 class Colour
jah128 13:adbd827234a4 52 {
jah128 13:adbd827234a4 53 public:
jah128 16:50686c07ad07 54 /** Set integration time and gain of colour sensor to default values
jah128 16:50686c07ad07 55 *
jah128 16:50686c07ad07 56 */
jah128 16:50686c07ad07 57 void colour_sensor_init();
jah128 16:50686c07ad07 58
jah128 17:bf614e28668f 59 /** Set the white\black calibration values for the colour sensor
jah128 17:bf614e28668f 60 *
jah128 17:bf614e28668f 61 */
jah128 17:bf614e28668f 62 void set_calibration_values(int c_black,int r_black,int g_black,int b_black,int c_white,int r_white,int g_white,int b_white);
jah128 17:bf614e28668f 63
jah128 13:adbd827234a4 64 /** Set the gain of the base colour sensor
jah128 13:adbd827234a4 65 *
jah128 13:adbd827234a4 66 * @param gain The gain value for the sensor
jah128 13:adbd827234a4 67 */
jah128 13:adbd827234a4 68 void set_base_colour_sensor_gain(char gain);
jah128 7:aa5a4a257895 69
jah128 13:adbd827234a4 70 /** Set the integration time constant for the base colour sensor
jah128 13:adbd827234a4 71 *
jah128 13:adbd827234a4 72 * @param gain The gain value for the sensor
jah128 13:adbd827234a4 73 */
jah128 13:adbd827234a4 74 void set_base_colour_sensor_integration_time(char int_time);
jah128 13:adbd827234a4 75
jah128 13:adbd827234a4 76 /** Enable the base colour sensor
jah128 13:adbd827234a4 77 */
jah128 13:adbd827234a4 78 void enable_base_colour_sensor(void);
jah128 0:d6269d17c8cf 79
jah128 15:66be5ec52c3b 80 /** Disable (power-down) the base colour sensor
jah128 15:66be5ec52c3b 81 */
jah128 15:66be5ec52c3b 82 void disable_base_colour_sensor(void);
jah128 15:66be5ec52c3b 83
jah128 13:adbd827234a4 84 /** Read the values from the base colour sensor
jah128 13:adbd827234a4 85 *
jah128 13:adbd827234a4 86 * @param Pointer to 3 x int array for r-g-b values
jah128 13:adbd827234a4 87 */
jah128 13:adbd827234a4 88 void read_base_colour_sensor_values(int * store_array);
jah128 15:66be5ec52c3b 89
jah128 15:66be5ec52c3b 90 /** Function enables colour sensor, takes a reading, returns a single int (range -1 to 8)
jah128 15:66be5ec52c3b 91 *
jah128 15:66be5ec52c3b 92 * @return Output of identify_colour_from_calibrated_colour_scores - int range -1 to 8
jah128 15:66be5ec52c3b 93 */
jah128 15:66be5ec52c3b 94 int detect_colour_once();
jah128 15:66be5ec52c3b 95
jah128 15:66be5ec52c3b 96 /** Returns the most recent identified colour from detect_colour_once or detect_colour_ticker (range -1 to 8)
jah128 15:66be5ec52c3b 97 *
jah128 15:66be5ec52c3b 98 * @return Output of identify_colour_from_calibrated_colour_scores - int range -1 to 8
jah128 15:66be5ec52c3b 99 */
jah128 15:66be5ec52c3b 100 int get_detected_colour();
jah128 15:66be5ec52c3b 101
jah128 15:66be5ec52c3b 102 /** Attempts to identify a colour from a given array of calibrated colour values
jah128 15:66be5ec52c3b 103 *
jah128 15:66be5ec52c3b 104 * @param calibrate_colour_array_in : Pointer to the calibrate colour array [output of get_calibrated_colour]
jah128 15:66be5ec52c3b 105 * @return int defining detected colour: 0=RED 1=YELLOW 2=GREEN 3=CYAN 4=BLUE 5=MAGENTA 6=WHITE 7=BLACK 8=UNSURE
jah128 15:66be5ec52c3b 106 */
jah128 15:66be5ec52c3b 107 int identify_colour_from_calibrated_colour_scores(float * calibrate_colour_array_in);
jah128 15:66be5ec52c3b 108
jah128 15:66be5ec52c3b 109 /** Translate an input array of raw CT,R,G,B values into calibrated, normalised values
jah128 15:66be5ec52c3b 110 *
jah128 15:66be5ec52c3b 111 * @param colour_array_in : 4-element int input array [output of read_base_colour_sensor_values()]
jah128 15:66be5ec52c3b 112 * @param colour_array_out : Target 4-element float array of normalised values [0=CT, range 0-1 1=R 2=G 3=B, Sum R+G+B=3.0]
jah128 15:66be5ec52c3b 113 */
jah128 15:66be5ec52c3b 114 void get_calibrated_colour(int * colour_array_in, float * colour_array_out);
jah128 15:66be5ec52c3b 115
jah128 15:66be5ec52c3b 116 /** Returns a string form of the output of identify_colour_from_calibrated_colour_scores()
jah128 15:66be5ec52c3b 117 *
jah128 15:66be5ec52c3b 118 * @param colour_index : Output of identify_colour_from_calibrated_colour_scores()
jah128 15:66be5ec52c3b 119 * @return 7-character String (eg 0="RED ")
jah128 15:66be5ec52c3b 120 */
jah128 15:66be5ec52c3b 121 const char * get_colour_string(int colour_index);
jah128 15:66be5ec52c3b 122
jah128 15:66be5ec52c3b 123 /** Starts a polling ticker that cyclically checks to see if a colour can be detected
jah128 15:66be5ec52c3b 124 *
jah128 15:66be5ec52c3b 125 * @param period_ms : The approximate cycle period in milliseconds
jah128 15:66be5ec52c3b 126 */
jah128 15:66be5ec52c3b 127 void start_colour_ticker(int period_ms);
jah128 15:66be5ec52c3b 128
jah128 16:50686c07ad07 129 /** Stops the polling ticker
jah128 16:50686c07ad07 130 *
jah128 16:50686c07ad07 131 */
jah128 16:50686c07ad07 132 void stop_colour_ticker(void);
jah128 16:50686c07ad07 133
jah128 15:66be5ec52c3b 134
jah128 15:66be5ec52c3b 135
jah128 15:66be5ec52c3b 136 void IF_colour_ticker_start();
jah128 15:66be5ec52c3b 137 void IF_colour_ticker_main();
jah128 13:adbd827234a4 138 char IF_check_base_colour_sensor(void);
jah128 15:66be5ec52c3b 139 int IF_writeSingleRegister( char address, char data );
jah128 15:66be5ec52c3b 140 int IF_writeMultipleRegisters( char address, char* data, int quantity );
jah128 15:66be5ec52c3b 141 char IF_readSingleRegister( char address );
jah128 15:66be5ec52c3b 142 int IF_readMultipleRegisters( char address, char* output, int quantity );
jah128 15:66be5ec52c3b 143 float IF_roundTowardsZero( const float value );
jah128 18:9204f74069b4 144 const char * IF_get_calibration_values_string(void);
jah128 13:adbd827234a4 145 };
jah128 0:d6269d17c8cf 146 #endif