PH

Dependencies:   IRremouteAndButton QuadDisplayMy LIS3MDL_my sMotor

Committer:
ankazwork
Date:
Mon May 17 05:34:19 2021 +0000
Revision:
0:eb99909151eb
placeholder;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ankazwork 0:eb99909151eb 1 #include "mbed.h"
ankazwork 0:eb99909151eb 2 #include "platform/mbed_thread.h"
ankazwork 0:eb99909151eb 3 #include "UserButton.h"
ankazwork 0:eb99909151eb 4 #include "sMotor.h"
ankazwork 0:eb99909151eb 5 #include "lis3mdl_class.h"
ankazwork 0:eb99909151eb 6 #include "QuadDisplay2.h"
ankazwork 0:eb99909151eb 7
ankazwork 0:eb99909151eb 8 /****************************************************************
ankazwork 0:eb99909151eb 9 * Definitions
ankazwork 0:eb99909151eb 10 ****************************************************************/
ankazwork 0:eb99909151eb 11
ankazwork 0:eb99909151eb 12 // Blinking rate in milliseconds
ankazwork 0:eb99909151eb 13 #define step_speed 1500
ankazwork 0:eb99909151eb 14 #define POINTS 1
ankazwork 0:eb99909151eb 15 #define M_PI 3.14159265358979323846
ankazwork 0:eb99909151eb 16 #define RADIAN M_PI/180.0
ankazwork 0:eb99909151eb 17
ankazwork 0:eb99909151eb 18 /****************************************************************
ankazwork 0:eb99909151eb 19 * Prototypes
ankazwork 0:eb99909151eb 20 *****************************************************************/
ankazwork 0:eb99909151eb 21
ankazwork 0:eb99909151eb 22 void Ini();
ankazwork 0:eb99909151eb 23 void HowManyClicks();
ankazwork 0:eb99909151eb 24 void proximityR_isr();
ankazwork 0:eb99909151eb 25 void proximityF_isr();
ankazwork 0:eb99909151eb 26 //-----------------------
ankazwork 0:eb99909151eb 27 void polling_sensors_isr();
ankazwork 0:eb99909151eb 28 void sensors_task();
ankazwork 0:eb99909151eb 29 //-----------------------
ankazwork 0:eb99909151eb 30 void clk_speed_isr();
ankazwork 0:eb99909151eb 31 /*****************************************************************
ankazwork 0:eb99909151eb 32 * Interface
ankazwork 0:eb99909151eb 33 ******************************************************************/
ankazwork 0:eb99909151eb 34
ankazwork 0:eb99909151eb 35 DigitalOut led1(LED1);
ankazwork 0:eb99909151eb 36 DigitalIn CW_switch(D10);
ankazwork 0:eb99909151eb 37 DigitalIn CCW_switch(D11);
ankazwork 0:eb99909151eb 38 //---
ankazwork 0:eb99909151eb 39 RawSerial pc(USBTX, USBRX);
ankazwork 0:eb99909151eb 40 DevI2C devI2c(D14,D15);
ankazwork 0:eb99909151eb 41 LIS3MDL magnetometer(&devI2c,LIS3MDL_M_MEMS_ADDRESS_LOW);
ankazwork 0:eb99909151eb 42 DigitalOut led2(A0);
ankazwork 0:eb99909151eb 43 SPI spi(D4, D5, D3);
ankazwork 0:eb99909151eb 44 QuadDisplayMy display(&spi,D6);
ankazwork 0:eb99909151eb 45 //---
ankazwork 0:eb99909151eb 46 sMotor motor(A5, A4, A3, A2); // creates new stepper motor: IN1, IN2, IN3, IN4
ankazwork 0:eb99909151eb 47 InterruptIn BlueBatton(BUTTON)
ankazwork 0:eb99909151eb 48
ankazwork 0:eb99909151eb 49 /*****************************************************************
ankazwork 0:eb99909151eb 50 * Time
ankazwork 0:eb99909151eb 51 ******************************************************************/
ankazwork 0:eb99909151eb 52 //Ticker polling_sensors;
ankazwork 0:eb99909151eb 53 //Ticker clk_speed;
ankazwork 0:eb99909151eb 54 /****************************************************************
ankazwork 0:eb99909151eb 55 * Threads
ankazwork 0:eb99909151eb 56 ****************************************************************/
ankazwork 0:eb99909151eb 57 //Thread sensor_daemon;
ankazwork 0:eb99909151eb 58
ankazwork 0:eb99909151eb 59 /*****************************************************************
ankazwork 0:eb99909151eb 60 * Global variables
ankazwork 0:eb99909151eb 61 ******************************************************************/
ankazwork 0:eb99909151eb 62
ankazwork 0:eb99909151eb 63
ankazwork 0:eb99909151eb 64 //----------------
ankazwork 0:eb99909151eb 65 uint8_t button_event;
ankazwork 0:eb99909151eb 66 uint8_t mode=0;
ankazwork 0:eb99909151eb 67 uint8_t RotateDir;
ankazwork 0:eb99909151eb 68 uint16_t RotateSpeed;
ankazwork 0:eb99909151eb 69 //----------------
ankazwork 0:eb99909151eb 70 uint8_t SensorsEn=1;
ankazwork 0:eb99909151eb 71
ankazwork 0:eb99909151eb 72 int16_t M[3];
ankazwork 0:eb99909151eb 73 uint8_t point;
ankazwork 0:eb99909151eb 74 uint32_t rotate_cnt;
ankazwork 0:eb99909151eb 75 //---------
ankazwork 0:eb99909151eb 76 //------------------
ankazwork 0:eb99909151eb 77
ankazwork 0:eb99909151eb 78 /******************************************************************
ankazwork 0:eb99909151eb 79 * Main
ankazwork 0:eb99909151eb 80 ******************************************************************/
ankazwork 0:eb99909151eb 81 int main()
ankazwork 0:eb99909151eb 82 {
ankazwork 0:eb99909151eb 83 // Initialise the digital pin LED1 as an output
ankazwork 0:eb99909151eb 84
ankazwork 0:eb99909151eb 85 Ini();
ankazwork 0:eb99909151eb 86
ankazwork 0:eb99909151eb 87 while (true) {
ankazwork 0:eb99909151eb 88 //led = !led;
ankazwork 0:eb99909151eb 89 HowManyClicks();
ankazwork 0:eb99909151eb 90 if (mode){
ankazwork 0:eb99909151eb 91
ankazwork 0:eb99909151eb 92 }
ankazwork 0:eb99909151eb 93 if (point){
ankazwork 0:eb99909151eb 94 motor.step(POINTS, RotateDir, RotateSpeed);
ankazwork 0:eb99909151eb 95 }else
ankazwork 0:eb99909151eb 96 wait_us(step_speed);
ankazwork 0:eb99909151eb 97
ankazwork 0:eb99909151eb 98 //thread_sleep_for(BLINKING_RATE_MS);
ankazwork 0:eb99909151eb 99 }
ankazwork 0:eb99909151eb 100 }
ankazwork 0:eb99909151eb 101
ankazwork 0:eb99909151eb 102 /**********************************************************
ankazwork 0:eb99909151eb 103 * Functions
ankazwork 0:eb99909151eb 104 ***********************************************************/
ankazwork 0:eb99909151eb 105 void Ini()
ankazwork 0:eb99909151eb 106 {
ankazwork 0:eb99909151eb 107 pc.baud(115200);
ankazwork 0:eb99909151eb 108 ButtonIni(&BlueBatton);
ankazwork 0:eb99909151eb 109 button_event_ind=button_event=0;
ankazwork 0:eb99909151eb 110
ankazwork 0:eb99909151eb 111
ankazwork 0:eb99909151eb 112 //--------------
ankazwork 0:eb99909151eb 113
ankazwork 0:eb99909151eb 114 sensor_daemon.start(sensors_task);
ankazwork 0:eb99909151eb 115 SensorsEn=1;
ankazwork 0:eb99909151eb 116 polling_sensors.attach(&polling_sensors_isr, 0.4);
ankazwork 0:eb99909151eb 117 clk_speed.attach(&clk_speed_isr, 0.01);
ankazwork 0:eb99909151eb 118 mode=0;
ankazwork 0:eb99909151eb 119 point=0;
ankazwork 0:eb99909151eb 120 RotateSpeed=step_speed;
ankazwork 0:eb99909151eb 121 rotate_cnt=0;
ankazwork 0:eb99909151eb 122 }
ankazwork 0:eb99909151eb 123
ankazwork 0:eb99909151eb 124 //------------------
ankazwork 0:eb99909151eb 125 void HowManyClicks()
ankazwork 0:eb99909151eb 126 {
ankazwork 0:eb99909151eb 127 WhatButtonMode(&button_event)
ankazwork 0:eb99909151eb 128 if(button_event) {
ankazwork 0:eb99909151eb 129 switch (button_event) {
ankazwork 0:eb99909151eb 130 case 1:
ankazwork 0:eb99909151eb 131 mode=0;
ankazwork 0:eb99909151eb 132 point=0;
ankazwork 0:eb99909151eb 133 break;
ankazwork 0:eb99909151eb 134 case 2:
ankazwork 0:eb99909151eb 135 mode=1;
ankazwork 0:eb99909151eb 136 point=0;
ankazwork 0:eb99909151eb 137 //RotateDir=1;
ankazwork 0:eb99909151eb 138 //RotateSpeed=step_speed;
ankazwork 0:eb99909151eb 139 break;
ankazwork 0:eb99909151eb 140 case 3:
ankazwork 0:eb99909151eb 141 mode=2;
ankazwork 0:eb99909151eb 142 point=0;
ankazwork 0:eb99909151eb 143 //RotateDir=0;
ankazwork 0:eb99909151eb 144 //RotateSpeed=step_speed;
ankazwork 0:eb99909151eb 145 break;
ankazwork 0:eb99909151eb 146 case 4:
ankazwork 0:eb99909151eb 147 mode=3;
ankazwork 0:eb99909151eb 148 point=0;
ankazwork 0:eb99909151eb 149 //RotateDir=1;
ankazwork 0:eb99909151eb 150 //RotateSpeed=step_speed;
ankazwork 0:eb99909151eb 151 break;
ankazwork 0:eb99909151eb 152 case 5:
ankazwork 0:eb99909151eb 153 mode=4;
ankazwork 0:eb99909151eb 154 point=0;
ankazwork 0:eb99909151eb 155 //RotateSpeed=step_speed*2;
ankazwork 0:eb99909151eb 156 //RotateDir=0;
ankazwork 0:eb99909151eb 157 break;
ankazwork 0:eb99909151eb 158 case 6:
ankazwork 0:eb99909151eb 159 mode=5;
ankazwork 0:eb99909151eb 160 point=1;
ankazwork 0:eb99909151eb 161 RotateSpeed=step_speed*2;
ankazwork 0:eb99909151eb 162 RotateDir=1;
ankazwork 0:eb99909151eb 163 break;
ankazwork 0:eb99909151eb 164 case 7:
ankazwork 0:eb99909151eb 165
ankazwork 0:eb99909151eb 166 break;
ankazwork 0:eb99909151eb 167
ankazwork 0:eb99909151eb 168 default:
ankazwork 0:eb99909151eb 169 break;
ankazwork 0:eb99909151eb 170 }
ankazwork 0:eb99909151eb 171 button_event=0;
ankazwork 0:eb99909151eb 172 }
ankazwork 0:eb99909151eb 173 }
ankazwork 0:eb99909151eb 174
ankazwork 0:eb99909151eb 175 //-----------------
ankazwork 0:eb99909151eb 176 void sensors_task()
ankazwork 0:eb99909151eb 177 {
ankazwork 0:eb99909151eb 178 int status;
ankazwork 0:eb99909151eb 179 char text[5];
ankazwork 0:eb99909151eb 180 int32_t dif;
ankazwork 0:eb99909151eb 181
ankazwork 0:eb99909151eb 182 while (true) {
ankazwork 0:eb99909151eb 183 ThisThread::flags_wait_any(0x1,true);
ankazwork 0:eb99909151eb 184 SensorsEn=0;
ankazwork 0:eb99909151eb 185 status = board->sensor_left->get_distance(&distance_l);
ankazwork 0:eb99909151eb 186 if (status != VL53L0X_ERROR_NONE)
ankazwork 0:eb99909151eb 187 distance_l=8888;
ankazwork 0:eb99909151eb 188 status = board->sensor_right->get_distance(&distance_r);
ankazwork 0:eb99909151eb 189 if (status != VL53L0X_ERROR_NONE)
ankazwork 0:eb99909151eb 190 distance_r=8888;
ankazwork 0:eb99909151eb 191 status = board->sensor_centre->get_distance(&distance_c);
ankazwork 0:eb99909151eb 192 if (status != VL53L0X_ERROR_NONE)
ankazwork 0:eb99909151eb 193 distance_c=8888;
ankazwork 0:eb99909151eb 194 switch(mode){
ankazwork 0:eb99909151eb 195 case 0:
ankazwork 0:eb99909151eb 196 sprintf(text,"c%3d",distance_c);
ankazwork 0:eb99909151eb 197 break;
ankazwork 0:eb99909151eb 198 case 1:
ankazwork 0:eb99909151eb 199 sprintf(text,"l%3d", distance_l);
ankazwork 0:eb99909151eb 200 break;
ankazwork 0:eb99909151eb 201 case 2:
ankazwork 0:eb99909151eb 202 sprintf(text,"r%3d", distance_r);
ankazwork 0:eb99909151eb 203 break;
ankazwork 0:eb99909151eb 204 case 3:
ankazwork 0:eb99909151eb 205 dif=((int)distance_r-(int)distance_l);
ankazwork 0:eb99909151eb 206 sprintf(text,"d%3d",dif);
ankazwork 0:eb99909151eb 207 if (abs(dif)>4) {
ankazwork 0:eb99909151eb 208 point=1;
ankazwork 0:eb99909151eb 209 if(dif>0)
ankazwork 0:eb99909151eb 210 RotateDir=1;
ankazwork 0:eb99909151eb 211 else
ankazwork 0:eb99909151eb 212 RotateDir=0;
ankazwork 0:eb99909151eb 213 }else
ankazwork 0:eb99909151eb 214 point=0;
ankazwork 0:eb99909151eb 215 break;
ankazwork 0:eb99909151eb 216 case 4:
ankazwork 0:eb99909151eb 217 sprintf(text,"%d",G[0]);
ankazwork 0:eb99909151eb 218 break;
ankazwork 0:eb99909151eb 219 case 5:
ankazwork 0:eb99909151eb 220 sprintf(text,"%d",dur_time);
ankazwork 0:eb99909151eb 221 break;
ankazwork 0:eb99909151eb 222 default:
ankazwork 0:eb99909151eb 223 sprintf(text,"----");
ankazwork 0:eb99909151eb 224 break;
ankazwork 0:eb99909151eb 225 }
ankazwork 0:eb99909151eb 226 board->display->display_string(text);
ankazwork 0:eb99909151eb 227 SensorsEn=1;
ankazwork 0:eb99909151eb 228 }
ankazwork 0:eb99909151eb 229 }
ankazwork 0:eb99909151eb 230
ankazwork 0:eb99909151eb 231 /**********************************************************
ankazwork 0:eb99909151eb 232 * Interrupt Service Routines
ankazwork 0:eb99909151eb 233 ***********************************************************/
ankazwork 0:eb99909151eb 234 void proximityR_isr()
ankazwork 0:eb99909151eb 235 {
ankazwork 0:eb99909151eb 236 prox=1;
ankazwork 0:eb99909151eb 237 }
ankazwork 0:eb99909151eb 238 //---------------------
ankazwork 0:eb99909151eb 239 void proximityF_isr()
ankazwork 0:eb99909151eb 240 {
ankazwork 0:eb99909151eb 241 prox=0;
ankazwork 0:eb99909151eb 242 }
ankazwork 0:eb99909151eb 243
ankazwork 0:eb99909151eb 244 //---------------------
ankazwork 0:eb99909151eb 245 void polling_sensors_isr()
ankazwork 0:eb99909151eb 246 {
ankazwork 0:eb99909151eb 247 if (SensorsEn){
ankazwork 0:eb99909151eb 248 sensor_daemon.flags_set(0x1);
ankazwork 0:eb99909151eb 249 led = !led;
ankazwork 0:eb99909151eb 250 }
ankazwork 0:eb99909151eb 251 }
ankazwork 0:eb99909151eb 252 //---------------------------------
ankazwork 0:eb99909151eb 253 void clk_speed_isr()
ankazwork 0:eb99909151eb 254 {
ankazwork 0:eb99909151eb 255 rotate_cnt++;
ankazwork 0:eb99909151eb 256 if (rotate_cnt>10)
ankazwork 0:eb99909151eb 257 dur_time_en=1;
ankazwork 0:eb99909151eb 258 }