test

Dependencies:   mbed Watchdog

Dependents:   STM32-MC_node

Files at this revision

API Documentation at this revision

Comitter:
nestedslk
Date:
Tue Jul 21 12:18:14 2020 +0000
Parent:
4:f6e22dd39313
Child:
6:a760ce6defbe
Commit message:
ultrasonics working

Changed in this revision

AS5045/AS5045.cpp Show annotated file Show diff for this revision Revisions of this file
AS5045/AS5045.h Show annotated file Show diff for this revision Revisions of this file
global.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/AS5045/AS5045.cpp	Sat Jul 18 14:59:04 2020 +0000
+++ b/AS5045/AS5045.cpp	Tue Jul 21 12:18:14 2020 +0000
@@ -7,20 +7,20 @@
  *	@note
  *		PinName CS is the digital output pin number
  */
- 
- 
- Timer timer_enc;
+
+
+Timer timer_enc;
 AS5045::AS5045(PinName CS) :
-  _spi(NC,D12, D13),	// MBED SPI init
-  _cs(CS)				// Digital output pin init
+    _spi(NC,D12, D13),	// MBED SPI init
+    _cs(CS)				// Digital output pin init
 {
-	// Set SPI bitwidth
-	_spi.format(9, 2);
+    // Set SPI bitwidth9
+    _spi.format(9, 2);
 
-	// Set SPI frequency
-	_spi.frequency(50000);//SPI_FREQ);
-	// Set the digital output high
-	_cs = 1;
+    // Set SPI frequency
+    _spi.frequency(500000);//SPI_FREQ);
+    // Set the digital output high
+    _cs = 1;
 }
 
 /** Read tick amount from encoder (position)
@@ -30,29 +30,42 @@
  */
 int AS5045::getPosition()
 {
-	unsigned int upper,		// Upper part of the tick amount integer
-				lower;		// Lower part of the tick amount integer
-
-	// Set the chip select pin low
-	_cs = 0;
-	timer_enc.reset();
-        timer_enc.start();
-	wait_ms(5);
-	// Read data from the encoder
- 	upper = (_spi.write(0x00)) ;
- 	lower = (_spi.write(0x00));
- 	
-// 	lower = lower >> 6;   
-//    upper = (upper >> 6)+lower;
-//   upper = upper & 0xffc0;
-   upper = upper >> 6;
-
-	// Set the chip select pin high
- 	_cs = 1;
-wait_ms(5);
- 	// Return full 9-bits tick amount
-	return upper; 
-	//return upper ;
+    unsigned int upper,		// Upper part of the tick amount integer
+             lower;		// Lower part of the tick amount integer
+//
+//    // Set the chip select pin low
+//    //timer_enc.reset();
+//    timer_enc.start();
+//    _cs = 0;
+//    
+//    wait_ms(1);
+//    // Read data from the encoder
+//    upper = (_spi.write(0x00)) ;
+//// 	lower = (_spi.write(0x00));
+//
+//// 	lower = lower >> 6;
+////    upper = (upper >> 6)+lower;
+////   upper = upper & 0xffc0;
+////    upper = upper >> 6;
+//
+//    // Set the chip select pin high
+//    _cs = 1;
+//    //wait_ms(5);
+    // Return full 9-bits tick amount
+   // return (upper>>5);
+    //return upper ;
+    
+    _cs = 0;
+   upper = _spi.write(0x00);
+   lower = _spi.write(0x00);
+  _cs = 1;
+ 
+ 
+ 	//upper &=~ 0xF0;//mask out the first 4 bits
+//     EncoderByteData  = upper << 8; //shift MSB to correct EncoderByteData in EncoderByteData message
+//     EncoderByteData  += lower; // add LSB to EncoderByteData message to complete message
+  return ((upper << 3)+(lower >> 6));
+  //return EncoderByteData; 
 }
 
 /** Convert position of the encoder to degrees
@@ -62,9 +75,9 @@
  */
 float AS5045::getRotation()
 {
-	// Get data from the encoder
- 	float value = (float)getPosition();
+    // Get data from the encoder
+    float value = (float)getPosition();
 
- 	// Return degrees of rotation of the encoder
- 	return value * RESOLUTION;
+    // Return degrees of rotation of the encoder
+    return value * RESOLUTION;
 }
--- a/AS5045/AS5045.h	Sat Jul 18 14:59:04 2020 +0000
+++ b/AS5045/AS5045.h	Tue Jul 21 12:18:14 2020 +0000
@@ -14,7 +14,8 @@
 
 private:
   SPI _spi;												// MBED SPI instance
-  DigitalOut _cs;										// MBED digital output pin
+  DigitalOut _cs;
+  uint16_t EncoderByteData;										// MBED digital output pin
 
   static const float MAX_VALUE = 1024;			// Maximum possible encoder position value (ticks for full rotation)
   static const float RESOLUTION = 0.08789;	// Encoder resolution (0.08789 degrees per tick)
--- a/global.h	Sat Jul 18 14:59:04 2020 +0000
+++ b/global.h	Tue Jul 21 12:18:14 2020 +0000
@@ -10,9 +10,9 @@
 
 #define BLINKING_RATE     500
 
-#define TEST_ULTRASONIC 0
+#define TEST_ULTRASONIC 1
 #define TEST_ENCODERS   0
-#define TEST_IR         1
+#define TEST_IR         0
 
 // rs-485 pins
 #define UART1_TX    PB_6
--- a/main.cpp	Sat Jul 18 14:59:04 2020 +0000
+++ b/main.cpp	Tue Jul 21 12:18:14 2020 +0000
@@ -1,6 +1,7 @@
 #include <global.h>
 
 Serial pc(UART1_TX, UART1_RX);
+//Serial pc(USBTX, USBRX);
 Timer timer;
 
 int main()
@@ -29,55 +30,12 @@
         timer.reset();
         timer.start();
         sensor1.startMeasurement();
-        while (sensor1.isNewDataReady() == false && timer.read_ms() < time_to_check_us) {
-//           pc.printf("c1");
-           // if (sensor1.isNewDataReady() == true) {
-//                pc.printf("Distance1: %5.1f mm \r\n", sensor1.getDistance_cm());
-//            }
-        }
-        //timer.reset();
-//        timer.start();
-////        pc.printf("\r\n");
-//        sensor2.startMeasurement();
-//        while (sensor2.isNewDataReady() == false && timer.read_ms() < time_to_check_us) {
-////                pc.printf("c2");
-//        }  
-//        timer.reset();
-//        timer.start();
-//        pc.printf("\r\n");
-        //sensor3.startMeasurement();
-//        while (sensor3.isNewDataReady() == false && timer.read_ms() < time_to_check_us) {
-//                pc.printf("c3");
-//        }       
-//        
-//        pc.printf("\r\n");         
-//        pc.printf("Distance1: %5.1f mm -- Distance2: %5.1f mm -- -- Distance3: %5.1f mm \r\n", sensor1.getDistance_cm()), sensor2.getDistance_cm(), sensor3.getDistance_cm());
+        //while (sensor1.isNewDataReady() == false && timer.read_ms() < time_to_check_us) {
+        //pc.printf("Distance1: %5.1f mm -- Distance2: %5.1f mm -- -- Distance3: %5.1f mm \r\n", sensor1.getDistance_cm()), sensor2.getDistance_cm(), sensor3.getDistance_cm());
         pc.printf("Distance1: %5.1f mm \r\n", sensor1.getDistance_cm());
 
         timer.stop();
-//        wait_ms(150 - timer.read_ms());
-
-        //
-//          timer.reset();
-//          timer.start();
-//
-//          sensor2.startMeasurement();
-//          pc.printf("Distance2: %5.1f mm \r\n", sensor2.getDistance_cm());
-//
-//          timer.stop();
-//          wait_ms(500 - timer.read_ms());
-//
-//          timer.reset();
-//          timer.start();
-//
-//          sensor3.startMeasurement();
-//          pc.printf("Distance3: %5.1f mm \r\n", sensor3.getDistance_cm());
-//
-//          timer.stop();
-        //wait_ms(500 - timer.read_ms()); // time the loop
-
-//
-//          wait_ms(500 - timer.read_ms()); // time the loop
+        wait_ms(10);
 
     }
 
@@ -90,16 +48,14 @@
     AS5045 encoder_2(SP1_NSS2);
     while(1) {
         led = !led;
-        int encoder_1_Value = encoder_1.getPosition();
+        double encoder_1_Value = encoder_1.getPosition();
         wait_ms(30);
-        int encoder_2_Value = encoder_2.getPosition();
+        double encoder_2_Value = encoder_2.getPosition();
         wait_ms(30);
         //encoder_1_Value =
         //if( encoder_1_Value.IsValid() && encoder_2_Value.IsValid() )
-        
-        if(encoder_1_Value < min1_value) min1_value = encoder_1_Value;
-        if(encoder_1_Value > max1_value) max1_value = encoder_1_Value;
-        pc.printf("Sensor Values = %d --- %d \n\r",encoder_1_Value, encoder_2_Value ); //
+    
+        pc.printf("Sensor Values = %f --- %f \n\r",encoder_1_Value, encoder_2_Value ); //
         //else
         //pc.printf("Invalid data read");
 
@@ -109,12 +65,14 @@
 #endif
 
 # if TEST_IR
+PwmIn a(PB_4);
     //E18_D80NK infared1 (IR1_PB12_OUT);
 //    E18_D80NK infared2 (IR2_PB13_OUT);
-//    while (true) {
-//        pc.printf ("Is there any obstacle: %d ---- %d ",  infared1.checkObstacle(),infared2.checkObstacle() );
-//        pc.printf ("\n\r");
-//    }
+    while (true) {
+       // pc.printf ("Is there any obstacle: %d ---- %d ",  infared1.checkObstacle(),infared2.checkObstacle() );
+       
+        pc.printf ("\n\r");
+    }
 
 
 #endif