Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: SoftPWM USBDevice mbed
main.cpp@2:31a9ee6d066d, 2015-04-17 (annotated)
- Committer:
- Dzak
- Date:
- Fri Apr 17 04:46:12 2015 +0000
- Revision:
- 2:31a9ee6d066d
- Parent:
- 1:2c0e9ef138a4
- Child:
- 3:7a2b052c95a3
Finalized Directional Byte send protocol over Serial. Included working and tested implementation for Tx and description for working implementation of the Rx side on Stage Controller.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Dzak | 0:651738eb850c | 1 | #include "mbed.h" |
| Dzak | 2:31a9ee6d066d | 2 | //#include "USBSerial.h" |
| Dzak | 0:651738eb850c | 3 | #include "SoftPWM.h" |
| Dzak | 0:651738eb850c | 4 | |
| Dzak | 0:651738eb850c | 5 | AnalogIn Y_Axis(P0_13); |
| Dzak | 0:651738eb850c | 6 | AnalogIn X_Axis(P0_11); |
| Dzak | 1:2c0e9ef138a4 | 7 | AnalogIn Z_Axis(P0_15); |
| Dzak | 0:651738eb850c | 8 | |
| Dzak | 0:651738eb850c | 9 | SoftPWM X_PWM(P0_9); //P0_9 |
| Dzak | 0:651738eb850c | 10 | SoftPWM Y_PWM(P0_10); //P0_10 |
| Dzak | 1:2c0e9ef138a4 | 11 | SoftPWM Z_PWM(P0_4); //P0_4 |
| Dzak | 0:651738eb850c | 12 | |
| Dzak | 2:31a9ee6d066d | 13 | //USBSerial serial; |
| Dzak | 2:31a9ee6d066d | 14 | |
| Dzak | 2:31a9ee6d066d | 15 | Serial Send_Stage_Controller(P0_19,NC); |
| Dzak | 2:31a9ee6d066d | 16 | //Serial Receive_Stage_Controller(NC,P0_18); //NULL-MOdem |
| Dzak | 0:651738eb850c | 17 | |
| Dzak | 0:651738eb850c | 18 | #define MAX_LOGIC 10000 // Full-deflection Max Range |
| Dzak | 0:651738eb850c | 19 | #define MIN_LOGIC 0 // Full-deflection Min Range |
| Dzak | 0:651738eb850c | 20 | |
| Dzak | 1:2c0e9ef138a4 | 21 | #define SWITCH_LEVELS 5 // # of possible levels |
| Dzak | 0:651738eb850c | 22 | #define FWD 1 |
| Dzak | 0:651738eb850c | 23 | #define REV 0 |
| Dzak | 0:651738eb850c | 24 | |
| Dzak | 0:651738eb850c | 25 | |
| Dzak | 0:651738eb850c | 26 | |
| Dzak | 0:651738eb850c | 27 | //NOTE: Period MUST be defined 1st, them DC. |
| Dzak | 0:651738eb850c | 28 | |
| Dzak | 1:2c0e9ef138a4 | 29 | float Period_Array [SWITCH_LEVELS] = {0.01, 0.1, 100.0, 0.1, 0.01}; // Period Corresponding to 10, 100, 0, 100, 10 Hz |
| Dzak | 0:651738eb850c | 30 | uint16_t SpeedThreshold = MAX_LOGIC / SWITCH_LEVELS; |
| Dzak | 0:651738eb850c | 31 | |
| Dzak | 0:651738eb850c | 32 | uint16_t X_Speed = 4; |
| Dzak | 0:651738eb850c | 33 | uint16_t Y_Speed = 4; |
| Dzak | 1:2c0e9ef138a4 | 34 | uint16_t Z_Speed = 4; |
| Dzak | 0:651738eb850c | 35 | |
| Dzak | 2:31a9ee6d066d | 36 | char X_Direction = FWD; |
| Dzak | 2:31a9ee6d066d | 37 | char Y_Direction = FWD; |
| Dzak | 2:31a9ee6d066d | 38 | char Z_Direction = FWD; |
| Dzak | 0:651738eb850c | 39 | |
| Dzak | 0:651738eb850c | 40 | uint16_t tempX_Speed = 1; |
| Dzak | 0:651738eb850c | 41 | uint16_t tempY_Speed = 1; |
| Dzak | 1:2c0e9ef138a4 | 42 | uint16_t tempZ_Speed = 1; |
| Dzak | 0:651738eb850c | 43 | |
| Dzak | 2:31a9ee6d066d | 44 | char Direction_Byte = 0x00; //This byte contains all the info about directions. USed for transfer to Stage Controller |
| Dzak | 2:31a9ee6d066d | 45 | |
| Dzak | 2:31a9ee6d066d | 46 | |
| Dzak | 0:651738eb850c | 47 | main() |
| Dzak | 0:651738eb850c | 48 | { |
| Dzak | 0:651738eb850c | 49 | while(1) |
| Dzak | 0:651738eb850c | 50 | { |
| Dzak | 0:651738eb850c | 51 | wait_ms(5); |
| Dzak | 0:651738eb850c | 52 | |
| Dzak | 0:651738eb850c | 53 | X_Speed = (uint16_t) (X_Axis.read() * MAX_LOGIC) / (SpeedThreshold + SpeedThreshold / SWITCH_LEVELS); |
| Dzak | 0:651738eb850c | 54 | |
| Dzak | 0:651738eb850c | 55 | Y_Speed = (uint16_t) (Y_Axis.read() * MAX_LOGIC) / (SpeedThreshold + SpeedThreshold / SWITCH_LEVELS); |
| Dzak | 0:651738eb850c | 56 | |
| Dzak | 1:2c0e9ef138a4 | 57 | Z_Speed = (uint16_t) (Z_Axis.read() * MAX_LOGIC) / (SpeedThreshold + SpeedThreshold / SWITCH_LEVELS); |
| Dzak | 1:2c0e9ef138a4 | 58 | |
| Dzak | 1:2c0e9ef138a4 | 59 | if (X_Speed > 2) {X_Direction = FWD;} |
| Dzak | 1:2c0e9ef138a4 | 60 | if (X_Speed < 2) {X_Direction = REV;} |
| Dzak | 1:2c0e9ef138a4 | 61 | if (Y_Speed > 2) {Y_Direction = FWD;} |
| Dzak | 1:2c0e9ef138a4 | 62 | if (Y_Speed < 2) {Y_Direction = REV;} |
| Dzak | 1:2c0e9ef138a4 | 63 | if (Z_Speed > 2) {Z_Direction = FWD;} |
| Dzak | 1:2c0e9ef138a4 | 64 | if (Z_Speed < 2) {Z_Direction = REV;} |
| Dzak | 0:651738eb850c | 65 | |
| Dzak | 0:651738eb850c | 66 | |
| Dzak | 2:31a9ee6d066d | 67 | |
| Dzak | 2:31a9ee6d066d | 68 | /* |
| Dzak | 2:31a9ee6d066d | 69 | Assemble the Direction byte for ransmission over Serial to Stage Controller. X_Dir bit 2 Y_Dir bit 1, and Z_Dir bit 0 |
| Dzak | 2:31a9ee6d066d | 70 | */ |
| Dzak | 2:31a9ee6d066d | 71 | |
| Dzak | 2:31a9ee6d066d | 72 | |
| Dzak | 2:31a9ee6d066d | 73 | Direction_Byte = Direction_Byte >> 3; //Clear 3 previous LSBits |
| Dzak | 2:31a9ee6d066d | 74 | Direction_Byte = (Direction_Byte | X_Direction) << 1; //Place X_Dir value and shift right: X_Dir is now bit 1 |
| Dzak | 2:31a9ee6d066d | 75 | Direction_Byte = (Direction_Byte | Y_Direction) << 1; //Place X_Dir value and shift right: Y_Dir is now bit 1, X_Dir is now bit 2 |
| Dzak | 2:31a9ee6d066d | 76 | Direction_Byte = (Direction_Byte | Z_Direction); //Place X_Dir value : Z_Dir is now bit 0, Y_Dir is now bit 1, X_Dir is now bit 2 |
| Dzak | 2:31a9ee6d066d | 77 | |
| Dzak | 2:31a9ee6d066d | 78 | Send_Stage_Controller.putc(Direction_Byte); |
| Dzak | 2:31a9ee6d066d | 79 | |
| Dzak | 2:31a9ee6d066d | 80 | |
| Dzak | 2:31a9ee6d066d | 81 | /* |
| Dzak | 2:31a9ee6d066d | 82 | //============================================================================ |
| Dzak | 2:31a9ee6d066d | 83 | //At the receiver side the Direction_Byte will will be decoded as follows: |
| Dzak | 2:31a9ee6d066d | 84 | |
| Dzak | 2:31a9ee6d066d | 85 | char Mask_X = 0x04; |
| Dzak | 2:31a9ee6d066d | 86 | char Mask_Y = 0x02; |
| Dzak | 2:31a9ee6d066d | 87 | char Mask_Z = 0x01; |
| Dzak | 2:31a9ee6d066d | 88 | |
| Dzak | 2:31a9ee6d066d | 89 | char Manual_Direction_Received = Receive_Stage_Controller.getc(); |
| Dzak | 2:31a9ee6d066d | 90 | |
| Dzak | 2:31a9ee6d066d | 91 | char X_Dir_Received = (Manual_Direction_Received & Mask_X) >> 2; |
| Dzak | 2:31a9ee6d066d | 92 | char Y_Dir_Received = (Manual_Direction_Received & Mask_Y) >> 1; |
| Dzak | 2:31a9ee6d066d | 93 | char Z_Dir_Received = Manual_Direction_Received & Mask_Z; |
| Dzak | 2:31a9ee6d066d | 94 | */ |
| Dzak | 2:31a9ee6d066d | 95 | |
| Dzak | 2:31a9ee6d066d | 96 | |
| Dzak | 2:31a9ee6d066d | 97 | /* |
| Dzak | 2:31a9ee6d066d | 98 | //=======================Only For Testing via NULL-Modem======================== |
| Dzak | 2:31a9ee6d066d | 99 | |
| Dzak | 2:31a9ee6d066d | 100 | wait(1); |
| Dzak | 2:31a9ee6d066d | 101 | serial.printf("X_Direction : %u\n",X_Dir_Received); |
| Dzak | 2:31a9ee6d066d | 102 | serial.printf("Y_Direction : %u\n",Y_Dir_Received); |
| Dzak | 2:31a9ee6d066d | 103 | serial.printf("Z_Direction : %u\n",Z_Dir_Received); |
| Dzak | 2:31a9ee6d066d | 104 | |
| Dzak | 2:31a9ee6d066d | 105 | */ |
| Dzak | 2:31a9ee6d066d | 106 | //============================================================================= |
| Dzak | 2:31a9ee6d066d | 107 | |
| Dzak | 2:31a9ee6d066d | 108 | |
| Dzak | 2:31a9ee6d066d | 109 | //serial.printf(" X_Direction : %u", X_Direction); |
| Dzak | 2:31a9ee6d066d | 110 | //serial.printf(" Y_Direction : %u \n",Y_Direction); |
| Dzak | 2:31a9ee6d066d | 111 | //serial.printf(" Z_Direction : %u \n",Z_Direction); |
| Dzak | 0:651738eb850c | 112 | |
| Dzak | 0:651738eb850c | 113 | if(tempX_Speed != X_Speed) |
| Dzak | 0:651738eb850c | 114 | { |
| Dzak | 0:651738eb850c | 115 | tempX_Speed = X_Speed; |
| Dzak | 0:651738eb850c | 116 | X_PWM.period(Period_Array[X_Speed]); |
| Dzak | 0:651738eb850c | 117 | X_PWM.write(0.5); |
| Dzak | 0:651738eb850c | 118 | |
| Dzak | 2:31a9ee6d066d | 119 | //serial.printf(" X_Period : %4.4f \n",Period_Array[X_Speed]); |
| Dzak | 0:651738eb850c | 120 | } |
| Dzak | 0:651738eb850c | 121 | |
| Dzak | 0:651738eb850c | 122 | if(tempY_Speed != Y_Speed) |
| Dzak | 0:651738eb850c | 123 | { |
| Dzak | 0:651738eb850c | 124 | tempY_Speed = Y_Speed; |
| Dzak | 0:651738eb850c | 125 | Y_PWM.period(Period_Array[Y_Speed]); |
| Dzak | 0:651738eb850c | 126 | Y_PWM.write(0.5); |
| Dzak | 2:31a9ee6d066d | 127 | //serial.printf(" Y_Period : %4.4f \n",Period_Array[Y_Speed]); |
| Dzak | 1:2c0e9ef138a4 | 128 | } |
| Dzak | 1:2c0e9ef138a4 | 129 | |
| Dzak | 1:2c0e9ef138a4 | 130 | if(tempZ_Speed != Z_Speed) |
| Dzak | 1:2c0e9ef138a4 | 131 | { |
| Dzak | 1:2c0e9ef138a4 | 132 | tempZ_Speed = Z_Speed; |
| Dzak | 1:2c0e9ef138a4 | 133 | Z_PWM.period(Period_Array[Z_Speed]); |
| Dzak | 1:2c0e9ef138a4 | 134 | Z_PWM.write(0.5); |
| Dzak | 2:31a9ee6d066d | 135 | //serial.printf(" Z_Period : %4.4f \n",Period_Array[Z_Speed]); |
| Dzak | 0:651738eb850c | 136 | } |
| Dzak | 0:651738eb850c | 137 | } |
| Dzak | 0:651738eb850c | 138 | } |
| Dzak | 0:651738eb850c | 139 | |
| Dzak | 0:651738eb850c | 140 |