Psi Swarm robot library version 0.9

Fork of PsiSwarmV9 by Psi Swarm Robot

Committer:
jah128
Date:
Thu Jun 01 23:02:32 2017 +0000
Revision:
16:50686c07ad07
Parent:
15:66be5ec52c3b
Child:
17:bf614e28668f
Added colour sensor code

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 16:50686c07ad07 32 #define CS_C_BLACK 63
jah128 16:50686c07ad07 33 #define CS_C_WHITE 802
jah128 16:50686c07ad07 34 #define CS_R_BLACK 22
jah128 16:50686c07ad07 35 #define CS_R_WHITE 244
jah128 16:50686c07ad07 36 #define CS_G_BLACK 24
jah128 16:50686c07ad07 37 #define CS_G_WHITE 297
jah128 16:50686c07ad07 38 #define CS_B_BLACK 16
jah128 16:50686c07ad07 39 #define CS_B_WHITE 232
jah128 16:50686c07ad07 40
jah128 16:50686c07ad07 41
jah128 13:adbd827234a4 42 /**
jah128 13:adbd827234a4 43 * The Colour class contains the functions for reading the base-mounted and top-mounted I2C colour sensors (optional).
jah128 16:50686c07ad07 44 *
jah128 16:50686c07ad07 45 * Functions for use with the TAOS TCS34725 and TCS34721 I2C colour sensors
jah128 16:50686c07ad07 46 *
jah128 16:50686c07ad07 47 * Example:
jah128 16:50686c07ad07 48 * @code
jah128 16:50686c07ad07 49 * #include "psiswarm.h"
jah128 16:50686c07ad07 50 *
jah128 16:50686c07ad07 51 * int main() {
jah128 16:50686c07ad07 52 * init();
jah128 16:50686c07ad07 53 * colour.start_colour_ticker(150);
jah128 16:50686c07ad07 54 while(1) {
jah128 16:50686c07ad07 55 pc.printf("Colour: %s\n",colour.get_colour_string(colour.get_detected_colour()));
jah128 16:50686c07ad07 56 wait(0.20);
jah128 16:50686c07ad07 57 }
jah128 16:50686c07ad07 58 * }
jah128 16:50686c07ad07 59 * @endcode
jah128 7:aa5a4a257895 60 */
jah128 13:adbd827234a4 61 class Colour
jah128 13:adbd827234a4 62 {
jah128 13:adbd827234a4 63 public:
jah128 16:50686c07ad07 64 /** Set integration time and gain of colour sensor to default values
jah128 16:50686c07ad07 65 *
jah128 16:50686c07ad07 66 */
jah128 16:50686c07ad07 67 void colour_sensor_init();
jah128 16:50686c07ad07 68
jah128 13:adbd827234a4 69 /** Set the gain of the base colour sensor
jah128 13:adbd827234a4 70 *
jah128 13:adbd827234a4 71 * @param gain The gain value for the sensor
jah128 13:adbd827234a4 72 */
jah128 13:adbd827234a4 73 void set_base_colour_sensor_gain(char gain);
jah128 7:aa5a4a257895 74
jah128 13:adbd827234a4 75 /** Set the integration time constant for the base colour sensor
jah128 13:adbd827234a4 76 *
jah128 13:adbd827234a4 77 * @param gain The gain value for the sensor
jah128 13:adbd827234a4 78 */
jah128 13:adbd827234a4 79 void set_base_colour_sensor_integration_time(char int_time);
jah128 13:adbd827234a4 80
jah128 13:adbd827234a4 81 /** Enable the base colour sensor
jah128 13:adbd827234a4 82 */
jah128 13:adbd827234a4 83 void enable_base_colour_sensor(void);
jah128 0:d6269d17c8cf 84
jah128 15:66be5ec52c3b 85 /** Disable (power-down) the base colour sensor
jah128 15:66be5ec52c3b 86 */
jah128 15:66be5ec52c3b 87 void disable_base_colour_sensor(void);
jah128 15:66be5ec52c3b 88
jah128 13:adbd827234a4 89 /** Read the values from the base colour sensor
jah128 13:adbd827234a4 90 *
jah128 13:adbd827234a4 91 * @param Pointer to 3 x int array for r-g-b values
jah128 13:adbd827234a4 92 */
jah128 13:adbd827234a4 93 void read_base_colour_sensor_values(int * store_array);
jah128 15:66be5ec52c3b 94
jah128 15:66be5ec52c3b 95 /** Function enables colour sensor, takes a reading, returns a single int (range -1 to 8)
jah128 15:66be5ec52c3b 96 *
jah128 15:66be5ec52c3b 97 * @return Output of identify_colour_from_calibrated_colour_scores - int range -1 to 8
jah128 15:66be5ec52c3b 98 */
jah128 15:66be5ec52c3b 99 int detect_colour_once();
jah128 15:66be5ec52c3b 100
jah128 15:66be5ec52c3b 101 /** Returns the most recent identified colour from detect_colour_once or detect_colour_ticker (range -1 to 8)
jah128 15:66be5ec52c3b 102 *
jah128 15:66be5ec52c3b 103 * @return Output of identify_colour_from_calibrated_colour_scores - int range -1 to 8
jah128 15:66be5ec52c3b 104 */
jah128 15:66be5ec52c3b 105 int get_detected_colour();
jah128 15:66be5ec52c3b 106
jah128 15:66be5ec52c3b 107 /** Attempts to identify a colour from a given array of calibrated colour values
jah128 15:66be5ec52c3b 108 *
jah128 15:66be5ec52c3b 109 * @param calibrate_colour_array_in : Pointer to the calibrate colour array [output of get_calibrated_colour]
jah128 15:66be5ec52c3b 110 * @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 111 */
jah128 15:66be5ec52c3b 112 int identify_colour_from_calibrated_colour_scores(float * calibrate_colour_array_in);
jah128 15:66be5ec52c3b 113
jah128 15:66be5ec52c3b 114 /** Translate an input array of raw CT,R,G,B values into calibrated, normalised values
jah128 15:66be5ec52c3b 115 *
jah128 15:66be5ec52c3b 116 * @param colour_array_in : 4-element int input array [output of read_base_colour_sensor_values()]
jah128 15:66be5ec52c3b 117 * @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 118 */
jah128 15:66be5ec52c3b 119 void get_calibrated_colour(int * colour_array_in, float * colour_array_out);
jah128 15:66be5ec52c3b 120
jah128 15:66be5ec52c3b 121 /** Returns a string form of the output of identify_colour_from_calibrated_colour_scores()
jah128 15:66be5ec52c3b 122 *
jah128 15:66be5ec52c3b 123 * @param colour_index : Output of identify_colour_from_calibrated_colour_scores()
jah128 15:66be5ec52c3b 124 * @return 7-character String (eg 0="RED ")
jah128 15:66be5ec52c3b 125 */
jah128 15:66be5ec52c3b 126 const char * get_colour_string(int colour_index);
jah128 15:66be5ec52c3b 127
jah128 15:66be5ec52c3b 128 /** Starts a polling ticker that cyclically checks to see if a colour can be detected
jah128 15:66be5ec52c3b 129 *
jah128 15:66be5ec52c3b 130 * @param period_ms : The approximate cycle period in milliseconds
jah128 15:66be5ec52c3b 131 */
jah128 15:66be5ec52c3b 132 void start_colour_ticker(int period_ms);
jah128 15:66be5ec52c3b 133
jah128 16:50686c07ad07 134 /** Stops the polling ticker
jah128 16:50686c07ad07 135 *
jah128 16:50686c07ad07 136 */
jah128 16:50686c07ad07 137 void stop_colour_ticker(void);
jah128 16:50686c07ad07 138
jah128 15:66be5ec52c3b 139
jah128 15:66be5ec52c3b 140
jah128 15:66be5ec52c3b 141 void IF_colour_ticker_start();
jah128 15:66be5ec52c3b 142 void IF_colour_ticker_main();
jah128 13:adbd827234a4 143 char IF_check_base_colour_sensor(void);
jah128 15:66be5ec52c3b 144 int IF_writeSingleRegister( char address, char data );
jah128 15:66be5ec52c3b 145 int IF_writeMultipleRegisters( char address, char* data, int quantity );
jah128 15:66be5ec52c3b 146 char IF_readSingleRegister( char address );
jah128 15:66be5ec52c3b 147 int IF_readMultipleRegisters( char address, char* output, int quantity );
jah128 15:66be5ec52c3b 148 float IF_roundTowardsZero( const float value );
jah128 13:adbd827234a4 149 };
jah128 0:d6269d17c8cf 150 #endif