David Giles
/
CM-MBED_RSEDP_Test_Suite_LPC1768_V3
This is a complete listing of the RS-EDP software for the mbed module to support the RS-EDP platform.
SourceFiles/RSEDP_Test_MC2.cpp@0:5b7639d1f2c4, 2010-11-19 (annotated)
- Committer:
- DavidGilesHitex
- Date:
- Fri Nov 19 09:49:16 2010 +0000
- Revision:
- 0:5b7639d1f2c4
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DavidGilesHitex | 0:5b7639d1f2c4 | 1 | /* Test Menu For I2C Master Mode System Controller */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 2 | /* *********************************************** */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 3 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 4 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 5 | /* Include Files Here */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 6 | #include "mbed.h" /* Header file for mbed module */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 7 | #include "misra_types.h" /* MISRA Types */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 8 | #include "defines.h" /* User defines */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 9 | #include "RSEDP_Slave_Address_Defines.h" /* Slave address of I2C Devices defined hhere */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 10 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 11 | #include "RSEDP_Mbed_Module.h" /* all the low level peripheral drivers on te mbed module - IO ports, UART, SPI, I2C etc */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 12 | #include "RSEDP_Base_Board.h" /* All the base board functions - EEPROM and erial latch */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 13 | #include "RSEDP_Comms_Module.h" /* Functions to control the real time clock */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 14 | #include "RSEDP_Digital_IO_Module.h" /* All the functions to control the serial input and output latches */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 15 | #include "RSEDP_Analogue_Module.h" /* Functions to control digital pot and serial ADC */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 16 | #include "RSEDP_MC1.h" /* Brushed DC Motor Drive MC1 Module */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 17 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 18 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 19 | #include "MC2_Motor_Driver_I2C_Master.h" |
DavidGilesHitex | 0:5b7639d1f2c4 | 20 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 21 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 22 | #define MOTOR_SPEED_DEMAND_FORWARD_MAX 1023u |
DavidGilesHitex | 0:5b7639d1f2c4 | 23 | #define MOTOR_SPEED_DEMAND_FORWARD_MIN 1u |
DavidGilesHitex | 0:5b7639d1f2c4 | 24 | #define MOTOR_SPEED_DEMAND_REVERSE_MAX 1023u |
DavidGilesHitex | 0:5b7639d1f2c4 | 25 | #define MOTOR_SPEED_DEMAND_REVERSE_MIN 1u |
DavidGilesHitex | 0:5b7639d1f2c4 | 26 | #define RAMP_UP_SPEED_MAX 1023u |
DavidGilesHitex | 0:5b7639d1f2c4 | 27 | #define RAMP_UP_SPEED_MIN 1u |
DavidGilesHitex | 0:5b7639d1f2c4 | 28 | #define RAMP_DOWN_SPEED_MAX 1023u |
DavidGilesHitex | 0:5b7639d1f2c4 | 29 | #define RAMP_DOWN_SPEED_MIN 1u |
DavidGilesHitex | 0:5b7639d1f2c4 | 30 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 31 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 32 | /* Global Function Prototypes here */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 33 | void I2C_MMSC_Test_Suite(void); /* Suites of tests for Master Mode Control */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 34 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 35 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 36 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 37 | /* Local Static Function Prototypes Here */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 38 | static void i2C_control_submenu(void); |
DavidGilesHitex | 0:5b7639d1f2c4 | 39 | static sint32_t i2C_cs_ping(void); |
DavidGilesHitex | 0:5b7639d1f2c4 | 40 | static sint32_t i2C_cs_reset(void); |
DavidGilesHitex | 0:5b7639d1f2c4 | 41 | static sint32_t i2C_cs_emergency_stop(void); |
DavidGilesHitex | 0:5b7639d1f2c4 | 42 | static sint32_t i2C_cs_normal_stop(void); |
DavidGilesHitex | 0:5b7639d1f2c4 | 43 | static sint32_t i2C_cs_set_new_direction_forward(void); |
DavidGilesHitex | 0:5b7639d1f2c4 | 44 | static sint32_t i2C_cs_set_new_direction_reverse(void); |
DavidGilesHitex | 0:5b7639d1f2c4 | 45 | static sint32_t choose_the_ramp_up_speed(void); |
DavidGilesHitex | 0:5b7639d1f2c4 | 46 | static sint32_t i2C_cs_set_ramp_up_speed(uint16_t rampup); |
DavidGilesHitex | 0:5b7639d1f2c4 | 47 | static sint32_t choose_the_ramp_down_speed(void); |
DavidGilesHitex | 0:5b7639d1f2c4 | 48 | static sint32_t i2C_cs_set_ramp_down_speed(uint16_t rampdown); |
DavidGilesHitex | 0:5b7639d1f2c4 | 49 | static sint32_t choose_the_motor_demand_forward(void); |
DavidGilesHitex | 0:5b7639d1f2c4 | 50 | static sint32_t i2C_cs_set_motor_demand_forward(uint16_t demandforward); |
DavidGilesHitex | 0:5b7639d1f2c4 | 51 | static sint32_t choose_the_motor_demand_reverse(void); |
DavidGilesHitex | 0:5b7639d1f2c4 | 52 | static sint32_t i2C_cs_set_motor_demand_reverse(uint16_t demandreverse); |
DavidGilesHitex | 0:5b7639d1f2c4 | 53 | static sint32_t choose_the_rotary_encoder_counts(void); |
DavidGilesHitex | 0:5b7639d1f2c4 | 54 | static sint32_t i2C_cs_set_rotary_encoder_counts(uint32_t rotary_encoder_counter); |
DavidGilesHitex | 0:5b7639d1f2c4 | 55 | static sint32_t i2C_cs_start_rotation(void); |
DavidGilesHitex | 0:5b7639d1f2c4 | 56 | static sint32_t i2C_cs_goto_home(void); |
DavidGilesHitex | 0:5b7639d1f2c4 | 57 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 58 | static sint32_t i2C_cs_read_RPMs(void); |
DavidGilesHitex | 0:5b7639d1f2c4 | 59 | static sint32_t i2C_cs_read_motor_currents(void); |
DavidGilesHitex | 0:5b7639d1f2c4 | 60 | static sint32_t i2C_cs_read_Vbus_voltages(void); |
DavidGilesHitex | 0:5b7639d1f2c4 | 61 | static sint32_t i2C_cs_read_demand_speed_potRPM(void); |
DavidGilesHitex | 0:5b7639d1f2c4 | 62 | static sint32_t i2C_cs_read_hall_sensors(void); |
DavidGilesHitex | 0:5b7639d1f2c4 | 63 | static sint32_t i2C_cs_read_status_flags(void); |
DavidGilesHitex | 0:5b7639d1f2c4 | 64 | static sint32_t i2C_cs_read_Maximum_RPM(void); |
DavidGilesHitex | 0:5b7639d1f2c4 | 65 | static sint32_t i2C_cs_read_Rotary_Encoder(void); |
DavidGilesHitex | 0:5b7639d1f2c4 | 66 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 67 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 68 | static void i2C_cs_execute_sequence(void); |
DavidGilesHitex | 0:5b7639d1f2c4 | 69 | static uint8_t get_a_number_digit(void); |
DavidGilesHitex | 0:5b7639d1f2c4 | 70 | static uint32_t get_a_complete_number(void); |
DavidGilesHitex | 0:5b7639d1f2c4 | 71 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 72 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 73 | /* Static Variables available at File Scope */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 74 | static uint8_t Slave_Address = DSPIC_SLAVE_ADDRESS_MIN; |
DavidGilesHitex | 0:5b7639d1f2c4 | 75 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 76 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 77 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 78 | /*This funtciton will control the other PIC on the board via I2C command */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 79 | void I2C_MMSC_Test_Suite(void) |
DavidGilesHitex | 0:5b7639d1f2c4 | 80 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 81 | uint16_t keypress = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 82 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 83 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 84 | Slave_Address = DSPIC_SLAVE_ADDRESS_MIN; /* 7 bit slave address */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 85 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 86 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 87 | setup_CNTRL_I2C_Master_Mode(); /* Setup the I2C Peripheral for master mode */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 88 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 89 | pc.printf("\n\n\n\rI2C Master Mode Controller.\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 90 | pc.printf("This module will communicate and control the dsPIC I2C Slave devices\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 91 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 92 | do { |
DavidGilesHitex | 0:5b7639d1f2c4 | 93 | pc.printf("\n\rChoose the I2C Slave Address you want to control from this device\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 94 | pc.printf("Press the + and - key to increase and decrease the selection\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 95 | pc.printf("& then Return to finish.\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 96 | pc.printf("or Q to quit and return to main menu.\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 97 | pc.printf("\rNew Value is: %03d",Slave_Address); |
DavidGilesHitex | 0:5b7639d1f2c4 | 98 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 99 | do { |
DavidGilesHitex | 0:5b7639d1f2c4 | 100 | keypress = pc.getc(); /* Read character from PC keyboard */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 101 | if ((Slave_Address < DSPIC_SLAVE_ADDRESS_MAX) && (keypress == '+')) |
DavidGilesHitex | 0:5b7639d1f2c4 | 102 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 103 | Slave_Address++; |
DavidGilesHitex | 0:5b7639d1f2c4 | 104 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 105 | if ((Slave_Address > DSPIC_SLAVE_ADDRESS_MIN) && (keypress == '-')) |
DavidGilesHitex | 0:5b7639d1f2c4 | 106 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 107 | Slave_Address--; |
DavidGilesHitex | 0:5b7639d1f2c4 | 108 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 109 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 110 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 111 | pc.printf("\rNew Value is: %03d",Slave_Address); |
DavidGilesHitex | 0:5b7639d1f2c4 | 112 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 113 | } while ((keypress != 'q') && (keypress != '\r')); |
DavidGilesHitex | 0:5b7639d1f2c4 | 114 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 115 | if (keypress == '\r') |
DavidGilesHitex | 0:5b7639d1f2c4 | 116 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 117 | i2C_control_submenu(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 118 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 119 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 120 | } while (keypress != 'q'); |
DavidGilesHitex | 0:5b7639d1f2c4 | 121 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 122 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 123 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 124 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 125 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 126 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 127 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 128 | /* Sub Menu */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 129 | static void i2C_control_submenu(void) |
DavidGilesHitex | 0:5b7639d1f2c4 | 130 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 131 | uint8_t temp8 = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 132 | uint16_t temp16 = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 133 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 134 | do |
DavidGilesHitex | 0:5b7639d1f2c4 | 135 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 136 | pc.printf("\n\n\n\rTest Menu for Slave Address:%d\n\r", Slave_Address, 3u); |
DavidGilesHitex | 0:5b7639d1f2c4 | 137 | pc.printf("0 - Ping The Slave Address and Test For Acknowledge\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 138 | pc.printf("1 - RESET the Slave Address MCU\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 139 | pc.printf("2 - Emergency Stop\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 140 | pc.printf("3 - Normal Stop\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 141 | pc.printf("4 - Set New Direction As Forward\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 142 | pc.printf("5 - Set New Direction As Reverse\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 143 | pc.printf("6 - Set The Ramp Up Speed\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 144 | pc.printf("7 - Set The Ramp Down Speed\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 145 | pc.printf("8 - Set The Maximum Motor Demand Speed Forward/Clockwise\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 146 | pc.printf("9 - Set The Maximum Motor Demand Speed Backward/Counter Clockwise\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 147 | pc.printf("a - Set The Number of Rotary Encoder Pulses You Want To move\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 148 | pc.printf("b - Start The Motor Turning\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 149 | pc.printf("c - Goto/Locate Home Position\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 150 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 151 | pc.printf("d - Read The Tacho RPM Speeds\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 152 | pc.printf("e - Read The Motor Currents\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 153 | pc.printf("f - Read The Vbus Voltages\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 154 | pc.printf("g - Read The Demand Speed Pot\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 155 | pc.printf("h - Read The Hall Sensors\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 156 | pc.printf("i - Read The Motor Status Flags\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 157 | pc.printf("j - Read The Maximum RPM Speed OF The Motor\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 158 | pc.printf("k - Read The Rotary Encoder Difference/Overshoot Value\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 159 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 160 | pc.printf("z - Execute A Sequence Of Tests\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 161 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 162 | pc.printf("RETURN - Quit this menu and return to Sub Menu\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 163 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 164 | pc.printf("Please select the test option\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 165 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 166 | temp16 = pc.getc(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 167 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 168 | temp8 = (uint8_t) (temp16); |
DavidGilesHitex | 0:5b7639d1f2c4 | 169 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 170 | if (temp8 == '0') |
DavidGilesHitex | 0:5b7639d1f2c4 | 171 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 172 | i2C_cs_ping(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 173 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 174 | if (temp8 == '1') |
DavidGilesHitex | 0:5b7639d1f2c4 | 175 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 176 | i2C_cs_reset(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 177 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 178 | if (temp8 == '2') |
DavidGilesHitex | 0:5b7639d1f2c4 | 179 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 180 | i2C_cs_emergency_stop(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 181 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 182 | if (temp8 == '3') |
DavidGilesHitex | 0:5b7639d1f2c4 | 183 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 184 | i2C_cs_normal_stop(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 185 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 186 | if (temp8 == '4') |
DavidGilesHitex | 0:5b7639d1f2c4 | 187 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 188 | i2C_cs_set_new_direction_forward(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 189 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 190 | if (temp8 == '5') |
DavidGilesHitex | 0:5b7639d1f2c4 | 191 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 192 | i2C_cs_set_new_direction_reverse(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 193 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 194 | if (temp8 == '6') |
DavidGilesHitex | 0:5b7639d1f2c4 | 195 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 196 | choose_the_ramp_up_speed(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 197 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 198 | if (temp8 == '7') |
DavidGilesHitex | 0:5b7639d1f2c4 | 199 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 200 | choose_the_ramp_down_speed(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 201 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 202 | if (temp8 == '8') |
DavidGilesHitex | 0:5b7639d1f2c4 | 203 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 204 | choose_the_motor_demand_forward(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 205 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 206 | if (temp8 == '9') |
DavidGilesHitex | 0:5b7639d1f2c4 | 207 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 208 | choose_the_motor_demand_reverse(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 209 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 210 | if (temp8 == 'a') |
DavidGilesHitex | 0:5b7639d1f2c4 | 211 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 212 | choose_the_rotary_encoder_counts(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 213 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 214 | if (temp8 == 'b') |
DavidGilesHitex | 0:5b7639d1f2c4 | 215 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 216 | i2C_cs_start_rotation(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 217 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 218 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 219 | if (temp8 == 'c') |
DavidGilesHitex | 0:5b7639d1f2c4 | 220 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 221 | i2C_cs_goto_home(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 222 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 223 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 224 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 225 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 226 | if (temp8 == 'd') |
DavidGilesHitex | 0:5b7639d1f2c4 | 227 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 228 | i2C_cs_read_RPMs(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 229 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 230 | if (temp8 == 'e') |
DavidGilesHitex | 0:5b7639d1f2c4 | 231 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 232 | i2C_cs_read_motor_currents(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 233 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 234 | if (temp8 == 'f') |
DavidGilesHitex | 0:5b7639d1f2c4 | 235 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 236 | i2C_cs_read_Vbus_voltages(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 237 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 238 | if (temp8 == 'g') |
DavidGilesHitex | 0:5b7639d1f2c4 | 239 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 240 | i2C_cs_read_demand_speed_potRPM(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 241 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 242 | if (temp8 == 'h') |
DavidGilesHitex | 0:5b7639d1f2c4 | 243 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 244 | i2C_cs_read_hall_sensors(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 245 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 246 | if (temp8 == 'i') |
DavidGilesHitex | 0:5b7639d1f2c4 | 247 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 248 | i2C_cs_read_status_flags(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 249 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 250 | if (temp8 == 'j') |
DavidGilesHitex | 0:5b7639d1f2c4 | 251 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 252 | i2C_cs_read_Maximum_RPM(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 253 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 254 | if (temp8 == 'k') |
DavidGilesHitex | 0:5b7639d1f2c4 | 255 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 256 | i2C_cs_read_Rotary_Encoder(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 257 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 258 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 259 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 260 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 261 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 262 | if (temp8 == 'z') |
DavidGilesHitex | 0:5b7639d1f2c4 | 263 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 264 | i2C_cs_execute_sequence(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 265 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 266 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 267 | } while (temp8 != '\r'); |
DavidGilesHitex | 0:5b7639d1f2c4 | 268 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 269 | pc.printf("Returning to main menu...\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 270 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 271 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 272 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 273 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 274 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 275 | /* Ping The I2C Slave Address */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 276 | static sint32_t i2C_cs_ping(void) |
DavidGilesHitex | 0:5b7639d1f2c4 | 277 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 278 | sint32_t Ack_Status = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 279 | uint16_t keypress = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 280 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 281 | pc.printf("\n\rPinging the I2C Slave Address:%d\r\n", Slave_Address, 3u); |
DavidGilesHitex | 0:5b7639d1f2c4 | 282 | do { |
DavidGilesHitex | 0:5b7639d1f2c4 | 283 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 284 | Ack_Status = I2C0_dsPIC_Ping(Slave_Address); |
DavidGilesHitex | 0:5b7639d1f2c4 | 285 | if (Ack_Status != ACK) |
DavidGilesHitex | 0:5b7639d1f2c4 | 286 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 287 | pc.printf("NO Acknowledge received\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 288 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 289 | else{ |
DavidGilesHitex | 0:5b7639d1f2c4 | 290 | pc.printf("Acknowledge received\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 291 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 292 | wait(1.0); |
DavidGilesHitex | 0:5b7639d1f2c4 | 293 | keypress = pc.getc(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 294 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 295 | } while ((Ack_Status != ACK) && (keypress != '\r')); /* Loop until keypress or we get an Ack */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 296 | return Ack_Status; |
DavidGilesHitex | 0:5b7639d1f2c4 | 297 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 298 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 299 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 300 | /* Reset The MCU at the I2C Slave Address */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 301 | static sint32_t i2C_cs_reset(void) |
DavidGilesHitex | 0:5b7639d1f2c4 | 302 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 303 | sint32_t Ack_Status = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 304 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 305 | pc.printf("\n\rResetting the target MCU at the I2C Slave Address:%03d\r\n", Slave_Address); |
DavidGilesHitex | 0:5b7639d1f2c4 | 306 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 307 | Ack_Status = I2C0_dsPIC_Reset(Slave_Address); |
DavidGilesHitex | 0:5b7639d1f2c4 | 308 | return Ack_Status; |
DavidGilesHitex | 0:5b7639d1f2c4 | 309 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 310 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 311 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 312 | /* Emergency Stop */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 313 | static sint32_t i2C_cs_emergency_stop(void) |
DavidGilesHitex | 0:5b7639d1f2c4 | 314 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 315 | sint32_t Ack_Status = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 316 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 317 | pc.printf("\n\rEmergency Stop the motor - command sent\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 318 | Ack_Status = I2C0_dsPIC_Emergency_Stop(Slave_Address); |
DavidGilesHitex | 0:5b7639d1f2c4 | 319 | return Ack_Status; |
DavidGilesHitex | 0:5b7639d1f2c4 | 320 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 321 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 322 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 323 | /* Normal Stop */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 324 | static sint32_t i2C_cs_normal_stop(void) |
DavidGilesHitex | 0:5b7639d1f2c4 | 325 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 326 | sint32_t Ack_Status = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 327 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 328 | pc.printf("\n\rNormal Stop the motor - command sent\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 329 | Ack_Status = I2C0_dsPIC_Normal_Stop(Slave_Address); |
DavidGilesHitex | 0:5b7639d1f2c4 | 330 | return Ack_Status; |
DavidGilesHitex | 0:5b7639d1f2c4 | 331 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 332 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 333 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 334 | /* Set The direction as Forward/Clockwise */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 335 | static sint32_t i2C_cs_set_new_direction_forward(void) |
DavidGilesHitex | 0:5b7639d1f2c4 | 336 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 337 | sint32_t Ack_Status = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 338 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 339 | pc.printf("\n\rSetting The Motor Direction - FORWARD - command sent\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 340 | Ack_Status = I2C0_dsPIC_Set_Motor_Direction(Slave_Address, CLOCKWISE); |
DavidGilesHitex | 0:5b7639d1f2c4 | 341 | return Ack_Status; |
DavidGilesHitex | 0:5b7639d1f2c4 | 342 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 343 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 344 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 345 | /* Set The direction as reverse or counter clockwise */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 346 | static sint32_t i2C_cs_set_new_direction_reverse(void) |
DavidGilesHitex | 0:5b7639d1f2c4 | 347 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 348 | sint32_t Ack_Status = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 349 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 350 | pc.printf("\n\rSetting The Motor Direction - REVERSE - command sent\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 351 | Ack_Status = I2C0_dsPIC_Set_Motor_Direction(Slave_Address, COUNTER_CLOCKWISE); |
DavidGilesHitex | 0:5b7639d1f2c4 | 352 | return Ack_Status; |
DavidGilesHitex | 0:5b7639d1f2c4 | 353 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 354 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 355 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 356 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 357 | /* Choose the Ramp Up Speed */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 358 | static sint32_t choose_the_ramp_up_speed(void) |
DavidGilesHitex | 0:5b7639d1f2c4 | 359 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 360 | uint16_t static rampup = RAMP_UP_SPEED_MIN; |
DavidGilesHitex | 0:5b7639d1f2c4 | 361 | uint16_t keypress = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 362 | sint32_t Ack_Status = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 363 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 364 | pc.printf("\n\rSelect the ramp up speed using the + and - keys. Press Return when done.\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 365 | pc.printf("\rNew Value is: %04d", rampup); |
DavidGilesHitex | 0:5b7639d1f2c4 | 366 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 367 | do { |
DavidGilesHitex | 0:5b7639d1f2c4 | 368 | keypress = pc.getc(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 369 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 370 | if ((rampup < RAMP_UP_SPEED_MAX) && (keypress == '+')) |
DavidGilesHitex | 0:5b7639d1f2c4 | 371 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 372 | rampup++; |
DavidGilesHitex | 0:5b7639d1f2c4 | 373 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 374 | if ((rampup > RAMP_UP_SPEED_MIN) && (keypress == '-')) |
DavidGilesHitex | 0:5b7639d1f2c4 | 375 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 376 | rampup--; |
DavidGilesHitex | 0:5b7639d1f2c4 | 377 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 378 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 379 | pc.printf("\rNew Value is: %04d", rampup); |
DavidGilesHitex | 0:5b7639d1f2c4 | 380 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 381 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 382 | } while (keypress != '\r'); |
DavidGilesHitex | 0:5b7639d1f2c4 | 383 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 384 | Ack_Status = i2C_cs_set_ramp_up_speed(rampup); |
DavidGilesHitex | 0:5b7639d1f2c4 | 385 | return Ack_Status; |
DavidGilesHitex | 0:5b7639d1f2c4 | 386 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 387 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 388 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 389 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 390 | /* Set the ramp up speed */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 391 | static sint32_t i2C_cs_set_ramp_up_speed(uint16_t rampup) |
DavidGilesHitex | 0:5b7639d1f2c4 | 392 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 393 | sint32_t Ack_Status = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 394 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 395 | pc.printf("\n\rSetting The Ramp Up Speed to %04d - command sent\n\r", rampup); |
DavidGilesHitex | 0:5b7639d1f2c4 | 396 | Ack_Status = I2C0_dsPIC_Set_Ramp_Up_Speed(Slave_Address, rampup); |
DavidGilesHitex | 0:5b7639d1f2c4 | 397 | return Ack_Status; |
DavidGilesHitex | 0:5b7639d1f2c4 | 398 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 399 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 400 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 401 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 402 | /* Choose the Ramp down Speed */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 403 | static sint32_t choose_the_ramp_down_speed(void) |
DavidGilesHitex | 0:5b7639d1f2c4 | 404 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 405 | uint16_t static rampdown = RAMP_DOWN_SPEED_MIN; |
DavidGilesHitex | 0:5b7639d1f2c4 | 406 | uint16_t keypress = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 407 | sint32_t Ack_Status = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 408 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 409 | pc.printf("\n\rSelect the ramp down speed using the + and - keys. Press Return when done.\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 410 | pc.printf("\rNew Value is: %04d", rampdown); |
DavidGilesHitex | 0:5b7639d1f2c4 | 411 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 412 | do { |
DavidGilesHitex | 0:5b7639d1f2c4 | 413 | keypress = pc.getc(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 414 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 415 | if ((rampdown < RAMP_DOWN_SPEED_MAX) && (keypress == '+')) |
DavidGilesHitex | 0:5b7639d1f2c4 | 416 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 417 | rampdown++; |
DavidGilesHitex | 0:5b7639d1f2c4 | 418 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 419 | if ((rampdown > RAMP_DOWN_SPEED_MIN) && (keypress == '-')) |
DavidGilesHitex | 0:5b7639d1f2c4 | 420 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 421 | rampdown--; |
DavidGilesHitex | 0:5b7639d1f2c4 | 422 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 423 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 424 | pc.printf("\rNew Value is: %04d", rampdown); |
DavidGilesHitex | 0:5b7639d1f2c4 | 425 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 426 | } while (keypress != '\r'); |
DavidGilesHitex | 0:5b7639d1f2c4 | 427 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 428 | Ack_Status = i2C_cs_set_ramp_down_speed(rampdown); |
DavidGilesHitex | 0:5b7639d1f2c4 | 429 | return Ack_Status; |
DavidGilesHitex | 0:5b7639d1f2c4 | 430 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 431 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 432 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 433 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 434 | /* Set The Ramp Down Speed */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 435 | static sint32_t i2C_cs_set_ramp_down_speed(uint16_t rampdown) |
DavidGilesHitex | 0:5b7639d1f2c4 | 436 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 437 | sint32_t Ack_Status = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 438 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 439 | pc.printf("\n\rSetting The Ramp Down Speed to %04d - command sent\n\r", rampdown); |
DavidGilesHitex | 0:5b7639d1f2c4 | 440 | Ack_Status = I2C0_dsPIC_Set_Ramp_Down_Speed(Slave_Address, rampdown); |
DavidGilesHitex | 0:5b7639d1f2c4 | 441 | return Ack_Status; |
DavidGilesHitex | 0:5b7639d1f2c4 | 442 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 443 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 444 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 445 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 446 | /* Choose the Motor Demand Forward Speed */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 447 | static sint32_t choose_the_motor_demand_forward(void) |
DavidGilesHitex | 0:5b7639d1f2c4 | 448 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 449 | uint16_t static forwarddemand = MOTOR_SPEED_DEMAND_FORWARD_MIN; |
DavidGilesHitex | 0:5b7639d1f2c4 | 450 | uint16_t keypress = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 451 | sint32_t Ack_Status = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 452 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 453 | pc.printf("\n\rSelect the Forward Motor Demand speed using the + and - keys. Press Return when done.\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 454 | pc.printf("\rNew Value is: %04d", forwarddemand); |
DavidGilesHitex | 0:5b7639d1f2c4 | 455 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 456 | do { |
DavidGilesHitex | 0:5b7639d1f2c4 | 457 | keypress = pc.getc(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 458 | if ((forwarddemand < MOTOR_SPEED_DEMAND_FORWARD_MAX) && (keypress == '+')) |
DavidGilesHitex | 0:5b7639d1f2c4 | 459 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 460 | forwarddemand += 9u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 461 | if (forwarddemand > MOTOR_SPEED_DEMAND_FORWARD_MAX) |
DavidGilesHitex | 0:5b7639d1f2c4 | 462 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 463 | forwarddemand = MOTOR_SPEED_DEMAND_FORWARD_MAX; |
DavidGilesHitex | 0:5b7639d1f2c4 | 464 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 465 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 466 | if ((forwarddemand > MOTOR_SPEED_DEMAND_FORWARD_MIN) && (keypress == '-')) |
DavidGilesHitex | 0:5b7639d1f2c4 | 467 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 468 | if (forwarddemand <= 8u) |
DavidGilesHitex | 0:5b7639d1f2c4 | 469 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 470 | forwarddemand = MOTOR_SPEED_DEMAND_FORWARD_MIN; |
DavidGilesHitex | 0:5b7639d1f2c4 | 471 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 472 | else{ |
DavidGilesHitex | 0:5b7639d1f2c4 | 473 | forwarddemand-= 8u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 474 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 475 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 476 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 477 | pc.printf("\rNew Value is: %04d", forwarddemand); |
DavidGilesHitex | 0:5b7639d1f2c4 | 478 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 479 | } while (keypress != '\r'); |
DavidGilesHitex | 0:5b7639d1f2c4 | 480 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 481 | Ack_Status = i2C_cs_set_motor_demand_forward(forwarddemand); |
DavidGilesHitex | 0:5b7639d1f2c4 | 482 | return Ack_Status; |
DavidGilesHitex | 0:5b7639d1f2c4 | 483 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 484 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 485 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 486 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 487 | /* Set the motor demand Speed */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 488 | static sint32_t i2C_cs_set_motor_demand_forward(uint16_t demandforward) |
DavidGilesHitex | 0:5b7639d1f2c4 | 489 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 490 | sint32_t Ack_Status = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 491 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 492 | pc.printf("\n\rSetting The Forward Motor Demand Speed to %04d - command sent\n\r",demandforward); |
DavidGilesHitex | 0:5b7639d1f2c4 | 493 | Ack_Status = I2C0_dsPIC_Set_Motor_Speed_Demand_Forward(Slave_Address, demandforward); |
DavidGilesHitex | 0:5b7639d1f2c4 | 494 | return Ack_Status; |
DavidGilesHitex | 0:5b7639d1f2c4 | 495 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 496 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 497 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 498 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 499 | /* Choose the Motor Demand Reverse Speed */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 500 | static sint32_t choose_the_motor_demand_reverse(void) |
DavidGilesHitex | 0:5b7639d1f2c4 | 501 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 502 | uint16_t static reversedemand = MOTOR_SPEED_DEMAND_REVERSE_MIN; |
DavidGilesHitex | 0:5b7639d1f2c4 | 503 | uint16_t keypress = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 504 | sint32_t Ack_Status = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 505 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 506 | pc.printf("\n\rSelect the Reverse Motor Demand speed using the + and - keys. Press Return when done.\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 507 | pc.printf("\rNew Value is: %04d", reversedemand); |
DavidGilesHitex | 0:5b7639d1f2c4 | 508 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 509 | do { |
DavidGilesHitex | 0:5b7639d1f2c4 | 510 | keypress = pc.getc(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 511 | if ((reversedemand < MOTOR_SPEED_DEMAND_REVERSE_MAX) && (keypress == '+')) |
DavidGilesHitex | 0:5b7639d1f2c4 | 512 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 513 | reversedemand += 9u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 514 | if (reversedemand > MOTOR_SPEED_DEMAND_REVERSE_MAX) |
DavidGilesHitex | 0:5b7639d1f2c4 | 515 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 516 | reversedemand = MOTOR_SPEED_DEMAND_REVERSE_MAX; |
DavidGilesHitex | 0:5b7639d1f2c4 | 517 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 518 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 519 | if ((reversedemand > MOTOR_SPEED_DEMAND_REVERSE_MIN) && (keypress == '-')) |
DavidGilesHitex | 0:5b7639d1f2c4 | 520 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 521 | if (reversedemand <= 8) |
DavidGilesHitex | 0:5b7639d1f2c4 | 522 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 523 | reversedemand = MOTOR_SPEED_DEMAND_REVERSE_MIN; |
DavidGilesHitex | 0:5b7639d1f2c4 | 524 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 525 | else{ |
DavidGilesHitex | 0:5b7639d1f2c4 | 526 | reversedemand -= 8u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 527 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 528 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 529 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 530 | pc.printf("\rNew Value is: %04d", reversedemand); |
DavidGilesHitex | 0:5b7639d1f2c4 | 531 | } while (keypress != '\r'); |
DavidGilesHitex | 0:5b7639d1f2c4 | 532 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 533 | Ack_Status = i2C_cs_set_motor_demand_reverse(reversedemand); |
DavidGilesHitex | 0:5b7639d1f2c4 | 534 | return Ack_Status; |
DavidGilesHitex | 0:5b7639d1f2c4 | 535 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 536 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 537 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 538 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 539 | /* Set the Reverse motor demand speed */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 540 | static sint32_t i2C_cs_set_motor_demand_reverse(uint16_t demandreverse) |
DavidGilesHitex | 0:5b7639d1f2c4 | 541 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 542 | sint32_t Ack_Status = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 543 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 544 | pc.printf("\n\rSetting The Reverse Motor Demand Speed to %04d - command sent\n\r",demandreverse); |
DavidGilesHitex | 0:5b7639d1f2c4 | 545 | Ack_Status = I2C0_dsPIC_Set_Motor_Speed_Demand_Reverse(Slave_Address, demandreverse); |
DavidGilesHitex | 0:5b7639d1f2c4 | 546 | return Ack_Status; |
DavidGilesHitex | 0:5b7639d1f2c4 | 547 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 548 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 549 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 550 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 551 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 552 | /* Select the number of rotart encoder counts you want to move */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 553 | static sint32_t choose_the_rotary_encoder_counts(void) |
DavidGilesHitex | 0:5b7639d1f2c4 | 554 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 555 | uint32_t static encoder_counts = 25000u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 556 | sint32_t Ack_Status = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 557 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 558 | pc.printf("\n\rType In The Target Number of Rotary Encoder Counts you want the motor to move. \n\rPress Return when done.\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 559 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 560 | encoder_counts = get_a_complete_number(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 561 | pc.printf("\rNew Value is: %010d", encoder_counts); |
DavidGilesHitex | 0:5b7639d1f2c4 | 562 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 563 | Ack_Status = i2C_cs_set_rotary_encoder_counts(encoder_counts); |
DavidGilesHitex | 0:5b7639d1f2c4 | 564 | return Ack_Status; |
DavidGilesHitex | 0:5b7639d1f2c4 | 565 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 566 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 567 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 568 | /* Set the number of counts you want to move - send to the Slave unit */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 569 | static sint32_t i2C_cs_set_rotary_encoder_counts(uint32_t rotary_encoder_counter) |
DavidGilesHitex | 0:5b7639d1f2c4 | 570 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 571 | sint32_t Ack_Status = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 572 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 573 | pc.printf("\n\rSetting The Rotary Encoder Counter Target to %010d - command sent\n\r", rotary_encoder_counter); |
DavidGilesHitex | 0:5b7639d1f2c4 | 574 | Ack_Status = I2C0_dsPIC_Set_Rotation_Counts(Slave_Address, rotary_encoder_counter); |
DavidGilesHitex | 0:5b7639d1f2c4 | 575 | return Ack_Status; |
DavidGilesHitex | 0:5b7639d1f2c4 | 576 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 577 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 578 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 579 | /* Start Rotation */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 580 | static sint32_t i2C_cs_start_rotation(void) |
DavidGilesHitex | 0:5b7639d1f2c4 | 581 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 582 | sint32_t Ack_Status = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 583 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 584 | pc.printf("\n\rRun The Motor - command sent\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 585 | Ack_Status = I2C0_dsPIC_Start_Motor_Rotation(Slave_Address); |
DavidGilesHitex | 0:5b7639d1f2c4 | 586 | return Ack_Status; |
DavidGilesHitex | 0:5b7639d1f2c4 | 587 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 588 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 589 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 590 | static sint32_t i2C_cs_goto_home(void) |
DavidGilesHitex | 0:5b7639d1f2c4 | 591 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 592 | sint32_t Ack_Status = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 593 | uint8_t rx_array[2] = {0u, 0u}; |
DavidGilesHitex | 0:5b7639d1f2c4 | 594 | uint8_t temp8 = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 595 | uint8_t abort_flag = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 596 | uint16_t keypress = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 597 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 598 | pc.printf("\n\rGoto Home - command sent\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 599 | pc.printf("CLOCKWISE direction - Home Speed 100\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 600 | Ack_Status = I2C0_dsPIC_Goto_Home(Slave_Address, CLOCKWISE, 100u); /* Goto home, run until sensor, CLOCKWISE, speed 100 */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 601 | pc.printf("Waiting for the sensor to reach the home position\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 602 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 603 | wait(0.5); |
DavidGilesHitex | 0:5b7639d1f2c4 | 604 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 605 | do { |
DavidGilesHitex | 0:5b7639d1f2c4 | 606 | Ack_Status = I2C0_dsPIC_Read_Motor_Status(Slave_Address, rx_array); |
DavidGilesHitex | 0:5b7639d1f2c4 | 607 | keypress = pc.getc(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 608 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 609 | if (keypress == '2') |
DavidGilesHitex | 0:5b7639d1f2c4 | 610 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 611 | i2C_cs_emergency_stop(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 612 | abort_flag = 1u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 613 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 614 | if (keypress == '3') |
DavidGilesHitex | 0:5b7639d1f2c4 | 615 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 616 | i2C_cs_normal_stop(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 617 | abort_flag = 1u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 618 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 619 | wait(0.1); |
DavidGilesHitex | 0:5b7639d1f2c4 | 620 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 621 | } while (rx_array[1] == GOTO_HOME); |
DavidGilesHitex | 0:5b7639d1f2c4 | 622 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 623 | temp8 = (rx_array[0] & 0x3cu); /* Strip out the fault flags bits */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 624 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 625 | if ((temp8 == 0x3cu) && (abort_flag == 0u)) |
DavidGilesHitex | 0:5b7639d1f2c4 | 626 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 627 | pc.printf("Home position detected\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 628 | pc.printf("Backing off 1/2 rev\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 629 | Ack_Status = I2C0_dsPIC_Set_Motor_Direction(Slave_Address, COUNTER_CLOCKWISE); |
DavidGilesHitex | 0:5b7639d1f2c4 | 630 | Ack_Status = I2C0_dsPIC_Set_Ramp_Up_Speed(Slave_Address, 1u); |
DavidGilesHitex | 0:5b7639d1f2c4 | 631 | Ack_Status = I2C0_dsPIC_Set_Motor_Speed_Demand_Reverse(Slave_Address, 5u); |
DavidGilesHitex | 0:5b7639d1f2c4 | 632 | Ack_Status = I2C0_dsPIC_Set_Rotation_Counts(Slave_Address, 500u); |
DavidGilesHitex | 0:5b7639d1f2c4 | 633 | Ack_Status = I2C0_dsPIC_Start_Motor_Rotation(Slave_Address); |
DavidGilesHitex | 0:5b7639d1f2c4 | 634 | wait(0.5); |
DavidGilesHitex | 0:5b7639d1f2c4 | 635 | do { |
DavidGilesHitex | 0:5b7639d1f2c4 | 636 | Ack_Status = I2C0_dsPIC_Read_Motor_Status(Slave_Address, rx_array); |
DavidGilesHitex | 0:5b7639d1f2c4 | 637 | keypress = pc.getc(); /* Read character from PC keyboard */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 638 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 639 | if (keypress == '2') |
DavidGilesHitex | 0:5b7639d1f2c4 | 640 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 641 | i2C_cs_emergency_stop(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 642 | abort_flag = 1u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 643 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 644 | if (keypress == '3') |
DavidGilesHitex | 0:5b7639d1f2c4 | 645 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 646 | i2C_cs_normal_stop(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 647 | abort_flag = 1u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 648 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 649 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 650 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 651 | wait(0.1); |
DavidGilesHitex | 0:5b7639d1f2c4 | 652 | } while (rx_array[1] != STOPPED); |
DavidGilesHitex | 0:5b7639d1f2c4 | 653 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 654 | temp8 = (rx_array[0] & 0x3cu); /* Strip out the fault flags bits */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 655 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 656 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 657 | if ((temp8 == 0x3cu) && (abort_flag == 0u)) |
DavidGilesHitex | 0:5b7639d1f2c4 | 658 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 659 | pc.printf("Finished backing off\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 660 | pc.printf("Slow close...\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 661 | Ack_Status = I2C0_dsPIC_Goto_Home(Slave_Address, CLOCKWISE, 1u); /* Goto home, run until sensor, CLOCKWISE, speed 100 */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 662 | pc.printf("Waiting for the sensor to reach home position\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 663 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 664 | wait(0.25); |
DavidGilesHitex | 0:5b7639d1f2c4 | 665 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 666 | do { |
DavidGilesHitex | 0:5b7639d1f2c4 | 667 | Ack_Status = I2C0_dsPIC_Read_Motor_Status(Slave_Address, rx_array); |
DavidGilesHitex | 0:5b7639d1f2c4 | 668 | wait(0.1); |
DavidGilesHitex | 0:5b7639d1f2c4 | 669 | } while (rx_array[1] == GOTO_HOME); |
DavidGilesHitex | 0:5b7639d1f2c4 | 670 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 671 | temp8 = (rx_array[0] & 0x3cu); /* Strip out the fault flags bits */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 672 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 673 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 674 | if ((temp8 == 0x3cu) && (abort_flag == 0u)) |
DavidGilesHitex | 0:5b7639d1f2c4 | 675 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 676 | pc.printf("All done home position achieved\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 677 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 678 | if (temp8 != 0x3cu) |
DavidGilesHitex | 0:5b7639d1f2c4 | 679 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 680 | pc.printf("Fault detected - goto home aborted\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 681 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 682 | if (abort_flag == 1u) |
DavidGilesHitex | 0:5b7639d1f2c4 | 683 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 684 | pc.printf("Motor stopped - goto home aborted\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 685 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 686 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 687 | return Ack_Status; |
DavidGilesHitex | 0:5b7639d1f2c4 | 688 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 689 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 690 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 691 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 692 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 693 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 694 | /* Read the RPM's */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 695 | static sint32_t i2C_cs_read_RPMs(void) |
DavidGilesHitex | 0:5b7639d1f2c4 | 696 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 697 | sint32_t Ack_Status = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 698 | uint16_t keypress = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 699 | uint8_t rx_array[2] = {0u, 0u}; |
DavidGilesHitex | 0:5b7639d1f2c4 | 700 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 701 | pc.printf("\n\rReading the RPM's from the target drive\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 702 | pc.printf("Press any key to continue\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 703 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 704 | do { |
DavidGilesHitex | 0:5b7639d1f2c4 | 705 | pc.printf("RPM "); |
DavidGilesHitex | 0:5b7639d1f2c4 | 706 | Ack_Status = I2C0_dsPIC_Read_Tacho_Speed_Instantaneous(Slave_Address, rx_array); /* Instantaneous first */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 707 | pc.printf("%05d ",((rx_array[0] * 256u) + rx_array[1])); |
DavidGilesHitex | 0:5b7639d1f2c4 | 708 | Ack_Status = I2C0_dsPIC_Read_Tacho_Speed_Average(Slave_Address, rx_array); /* then Average next */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 709 | pc.printf("%05d\r",((rx_array[0] * 256u) + rx_array[1])); |
DavidGilesHitex | 0:5b7639d1f2c4 | 710 | keypress = pc.getc(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 711 | } while (keypress != '\r'); |
DavidGilesHitex | 0:5b7639d1f2c4 | 712 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 713 | pc.printf("\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 714 | return Ack_Status; |
DavidGilesHitex | 0:5b7639d1f2c4 | 715 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 716 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 717 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 718 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 719 | /* Read The Motor Current Signals */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 720 | static sint32_t i2C_cs_read_motor_currents(void) |
DavidGilesHitex | 0:5b7639d1f2c4 | 721 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 722 | sint32_t Ack_Status = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 723 | uint16_t keypress = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 724 | uint8_t rx_array[2] = {0u, 0u}; |
DavidGilesHitex | 0:5b7639d1f2c4 | 725 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 726 | pc.printf("\n\rReading the Motor Current signals from the target drive\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 727 | pc.printf("Press any key to continue\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 728 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 729 | do { |
DavidGilesHitex | 0:5b7639d1f2c4 | 730 | pc.printf("Motor Current "); |
DavidGilesHitex | 0:5b7639d1f2c4 | 731 | Ack_Status = I2C0_dsPIC_Read_Motor_Current_Instantaneous(Slave_Address, rx_array); /* Instantaneous first */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 732 | pc.printf("%05dmA ",((rx_array[0] * 256u) + rx_array[1])); |
DavidGilesHitex | 0:5b7639d1f2c4 | 733 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 734 | Ack_Status = I2C0_dsPIC_Read_Motor_Current_Average(Slave_Address, rx_array); /* then Average next */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 735 | pc.printf("%05dmA\r",((rx_array[0] * 256u) + rx_array[1])); |
DavidGilesHitex | 0:5b7639d1f2c4 | 736 | wait(0.1); |
DavidGilesHitex | 0:5b7639d1f2c4 | 737 | keypress = pc.getc(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 738 | } while (keypress != '\r'); |
DavidGilesHitex | 0:5b7639d1f2c4 | 739 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 740 | pc.printf("\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 741 | return Ack_Status; |
DavidGilesHitex | 0:5b7639d1f2c4 | 742 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 743 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 744 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 745 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 746 | /* Reading The Vbus voltage */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 747 | static sint32_t i2C_cs_read_Vbus_voltages(void) |
DavidGilesHitex | 0:5b7639d1f2c4 | 748 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 749 | sint32_t Ack_Status = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 750 | uint16_t keypress = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 751 | uint16_t temp16 = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 752 | uint8_t rx_array[2] = {0u, 0u}; |
DavidGilesHitex | 0:5b7639d1f2c4 | 753 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 754 | pc.printf("\n\rReading the Vbus Voltage levels from the target drive\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 755 | pc.printf("Press any key to continue\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 756 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 757 | do { |
DavidGilesHitex | 0:5b7639d1f2c4 | 758 | pc.printf("Vbus Voltage "); |
DavidGilesHitex | 0:5b7639d1f2c4 | 759 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 760 | Ack_Status = I2C0_dsPIC_Read_Vbus_Instantaneous(Slave_Address, rx_array); /* Instantaneous Voltage first */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 761 | temp16 = ((rx_array[0] * 256u) + rx_array[1]); |
DavidGilesHitex | 0:5b7639d1f2c4 | 762 | pc.printf("%05dmV ",temp16); |
DavidGilesHitex | 0:5b7639d1f2c4 | 763 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 764 | Ack_Status = I2C0_dsPIC_Read_Vbus_Average(Slave_Address, rx_array); /* then Average next */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 765 | temp16 = ((rx_array[0] *256u) + rx_array[1]); |
DavidGilesHitex | 0:5b7639d1f2c4 | 766 | pc.printf("%05dmV\r",temp16); |
DavidGilesHitex | 0:5b7639d1f2c4 | 767 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 768 | wait(0.1); |
DavidGilesHitex | 0:5b7639d1f2c4 | 769 | keypress = pc.getc(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 770 | } while (keypress != '\r'); |
DavidGilesHitex | 0:5b7639d1f2c4 | 771 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 772 | pc.printf("\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 773 | return Ack_Status; |
DavidGilesHitex | 0:5b7639d1f2c4 | 774 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 775 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 776 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 777 | /* Motor Demand Speed Pot */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 778 | static sint32_t i2C_cs_read_demand_speed_potRPM(void) |
DavidGilesHitex | 0:5b7639d1f2c4 | 779 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 780 | sint32_t Ack_Status = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 781 | uint16_t keypress = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 782 | uint8_t rx_array[2] = {0u, 0u}; |
DavidGilesHitex | 0:5b7639d1f2c4 | 783 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 784 | pc.printf("\n\rReading the Motor Speed Demand Pot\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 785 | pc.printf("Press any key to continue\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 786 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 787 | do { |
DavidGilesHitex | 0:5b7639d1f2c4 | 788 | pc.printf("Demand Pot "); |
DavidGilesHitex | 0:5b7639d1f2c4 | 789 | Ack_Status = I2C0_dsPIC_Read_Demand_Pot_Instantaneous(Slave_Address, rx_array); /* Instantaneous Voltage first */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 790 | pc.printf("%05d ",((rx_array[0] * 256u) + rx_array[1])); |
DavidGilesHitex | 0:5b7639d1f2c4 | 791 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 792 | Ack_Status = I2C0_dsPIC_Read_Demand_Pot_Average(Slave_Address, rx_array); /* then Average next */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 793 | pc.printf("%05d\r",((rx_array[0] * 256u) + rx_array[1])); |
DavidGilesHitex | 0:5b7639d1f2c4 | 794 | wait(0.1); |
DavidGilesHitex | 0:5b7639d1f2c4 | 795 | keypress = pc.getc(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 796 | } while (keypress != '\r'); |
DavidGilesHitex | 0:5b7639d1f2c4 | 797 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 798 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 799 | pc.printf("\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 800 | return Ack_Status; |
DavidGilesHitex | 0:5b7639d1f2c4 | 801 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 802 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 803 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 804 | /* Read the Hall Sensors */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 805 | static sint32_t i2C_cs_read_hall_sensors(void) |
DavidGilesHitex | 0:5b7639d1f2c4 | 806 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 807 | sint32_t Ack_Status = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 808 | uint16_t keypress = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 809 | uint8_t rx_array[2] = {0u, 0u}; |
DavidGilesHitex | 0:5b7639d1f2c4 | 810 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 811 | pc.printf("\n\rReading the Hall Switches \n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 812 | pc.printf("Press any key to continue\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 813 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 814 | do { |
DavidGilesHitex | 0:5b7639d1f2c4 | 815 | pc.printf("Hall Sensors "); |
DavidGilesHitex | 0:5b7639d1f2c4 | 816 | Ack_Status = I2C0_dsPIC_Read_Hall_Sensor_Positions(Slave_Address, rx_array); |
DavidGilesHitex | 0:5b7639d1f2c4 | 817 | pc.printf("%05d\r",((rx_array[0] * 256u) + rx_array[1])); |
DavidGilesHitex | 0:5b7639d1f2c4 | 818 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 819 | wait(0.1); |
DavidGilesHitex | 0:5b7639d1f2c4 | 820 | keypress = pc.getc(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 821 | } while (keypress != '\r'); |
DavidGilesHitex | 0:5b7639d1f2c4 | 822 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 823 | pc.printf("\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 824 | return Ack_Status; |
DavidGilesHitex | 0:5b7639d1f2c4 | 825 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 826 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 827 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 828 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 829 | /* Read The Status Register */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 830 | static sint32_t i2C_cs_read_status_flags(void) |
DavidGilesHitex | 0:5b7639d1f2c4 | 831 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 832 | sint32_t Ack_Status = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 833 | uint16_t keypress = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 834 | uint8_t rx_array[2] = {0u, 0u}; |
DavidGilesHitex | 0:5b7639d1f2c4 | 835 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 836 | union u_type |
DavidGilesHitex | 0:5b7639d1f2c4 | 837 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 838 | struct bit_structure |
DavidGilesHitex | 0:5b7639d1f2c4 | 839 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 840 | unsigned bit7: 1u; /* Motor Run Flag */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 841 | unsigned bit6: 1u; /* Direction */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 842 | unsigned bit5: 1u; /* RPM Fault Flag */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 843 | unsigned bit4: 1u; /* Inst Current Fault Flag */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 844 | unsigned bit3: 1u; /* Ave Current Fault Flag */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 845 | unsigned bit2: 1u; /* Ext Comparator Fault Flag */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 846 | unsigned bit1: 1u; /* spare1 */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 847 | unsigned bit0: 1u; /* spare2 */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 848 | } temp8; |
DavidGilesHitex | 0:5b7639d1f2c4 | 849 | uint8_t temp8_byte; |
DavidGilesHitex | 0:5b7639d1f2c4 | 850 | } status_flag_bits; |
DavidGilesHitex | 0:5b7639d1f2c4 | 851 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 852 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 853 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 854 | pc.printf("\n\rReading the Status Flags And State Machine \n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 855 | pc.printf("Press any key to continue\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 856 | pc.printf("Run/Stop Direction RPM Flt InstCur AveCur ExtCom Current State\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 857 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 858 | do { |
DavidGilesHitex | 0:5b7639d1f2c4 | 859 | Ack_Status = I2C0_dsPIC_Read_Motor_Status(Slave_Address, rx_array); |
DavidGilesHitex | 0:5b7639d1f2c4 | 860 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 861 | status_flag_bits.temp8_byte = rx_array[0]; |
DavidGilesHitex | 0:5b7639d1f2c4 | 862 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 863 | if (status_flag_bits.temp8.bit7 == MOTOR_RUNNING) |
DavidGilesHitex | 0:5b7639d1f2c4 | 864 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 865 | pc.printf("RUNNING "); |
DavidGilesHitex | 0:5b7639d1f2c4 | 866 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 867 | else{ |
DavidGilesHitex | 0:5b7639d1f2c4 | 868 | pc.printf("STOPPED "); |
DavidGilesHitex | 0:5b7639d1f2c4 | 869 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 870 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 871 | if (status_flag_bits.temp8.bit6 == CLOCKWISE) |
DavidGilesHitex | 0:5b7639d1f2c4 | 872 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 873 | pc.printf("Clockwise "); |
DavidGilesHitex | 0:5b7639d1f2c4 | 874 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 875 | else{ |
DavidGilesHitex | 0:5b7639d1f2c4 | 876 | pc.printf("Anti CW "); |
DavidGilesHitex | 0:5b7639d1f2c4 | 877 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 878 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 879 | if (status_flag_bits.temp8.bit5 == FAULT_PRESENT) |
DavidGilesHitex | 0:5b7639d1f2c4 | 880 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 881 | pc.printf("FAULT "); |
DavidGilesHitex | 0:5b7639d1f2c4 | 882 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 883 | else{ |
DavidGilesHitex | 0:5b7639d1f2c4 | 884 | pc.printf("NO FAULT "); |
DavidGilesHitex | 0:5b7639d1f2c4 | 885 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 886 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 887 | if (status_flag_bits.temp8.bit4 == FAULT_PRESENT) |
DavidGilesHitex | 0:5b7639d1f2c4 | 888 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 889 | pc.printf("FAULT "); |
DavidGilesHitex | 0:5b7639d1f2c4 | 890 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 891 | else{ |
DavidGilesHitex | 0:5b7639d1f2c4 | 892 | pc.printf("NO FAULT "); |
DavidGilesHitex | 0:5b7639d1f2c4 | 893 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 894 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 895 | if (status_flag_bits.temp8.bit3 == FAULT_PRESENT) |
DavidGilesHitex | 0:5b7639d1f2c4 | 896 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 897 | pc.printf("FAULT "); |
DavidGilesHitex | 0:5b7639d1f2c4 | 898 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 899 | else{ |
DavidGilesHitex | 0:5b7639d1f2c4 | 900 | pc.printf("NO FAULT "); |
DavidGilesHitex | 0:5b7639d1f2c4 | 901 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 902 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 903 | if (status_flag_bits.temp8.bit2 == FAULT_PRESENT) |
DavidGilesHitex | 0:5b7639d1f2c4 | 904 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 905 | pc.printf("FAULT "); |
DavidGilesHitex | 0:5b7639d1f2c4 | 906 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 907 | else{ |
DavidGilesHitex | 0:5b7639d1f2c4 | 908 | pc.printf("NO FAULT "); |
DavidGilesHitex | 0:5b7639d1f2c4 | 909 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 910 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 911 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 912 | if (rx_array[1] == STOPPED) |
DavidGilesHitex | 0:5b7639d1f2c4 | 913 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 914 | pc.printf("Stopped "); |
DavidGilesHitex | 0:5b7639d1f2c4 | 915 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 916 | if (rx_array[1] == STOPPING) |
DavidGilesHitex | 0:5b7639d1f2c4 | 917 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 918 | pc.printf("Stopping "); |
DavidGilesHitex | 0:5b7639d1f2c4 | 919 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 920 | if (rx_array[1] == NEW_MOTOR_DEMAND_FORWARD) |
DavidGilesHitex | 0:5b7639d1f2c4 | 921 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 922 | pc.printf("New Demand Forward"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 923 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 924 | if (rx_array[1] == NEW_MOTOR_DEMAND_REVERSE) |
DavidGilesHitex | 0:5b7639d1f2c4 | 925 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 926 | pc.printf("New Demand Reverse"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 927 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 928 | if (rx_array[1] == NORMAL_RUNNING) |
DavidGilesHitex | 0:5b7639d1f2c4 | 929 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 930 | pc.printf("Normal Running "); |
DavidGilesHitex | 0:5b7639d1f2c4 | 931 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 932 | if (rx_array[1] == ROTATE_X_QUAD_COUNTS) |
DavidGilesHitex | 0:5b7639d1f2c4 | 933 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 934 | pc.printf("Rotating X Counts "); |
DavidGilesHitex | 0:5b7639d1f2c4 | 935 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 936 | if (rx_array[1] == LAST_REVOLUTION) |
DavidGilesHitex | 0:5b7639d1f2c4 | 937 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 938 | pc.printf("Last Revolution "); |
DavidGilesHitex | 0:5b7639d1f2c4 | 939 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 940 | if (rx_array[1] == GOTO_HOME) |
DavidGilesHitex | 0:5b7639d1f2c4 | 941 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 942 | pc.printf("Goto Home Position"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 943 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 944 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 945 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 946 | pc.printf("\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 947 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 948 | wait(0.1); |
DavidGilesHitex | 0:5b7639d1f2c4 | 949 | keypress = pc.getc(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 950 | } while (keypress != '\r'); |
DavidGilesHitex | 0:5b7639d1f2c4 | 951 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 952 | pc.printf("\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 953 | return Ack_Status; |
DavidGilesHitex | 0:5b7639d1f2c4 | 954 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 955 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 956 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 957 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 958 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 959 | /* Read The Maximum RPM Speed of the motor */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 960 | static sint32_t i2C_cs_read_Maximum_RPM(void) |
DavidGilesHitex | 0:5b7639d1f2c4 | 961 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 962 | sint32_t Ack_Status = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 963 | uint16_t keypress = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 964 | uint8_t rx_array[2] = {0u, 0u}; |
DavidGilesHitex | 0:5b7639d1f2c4 | 965 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 966 | pc.printf("\n\rReading the Maximum RPM from the target drive\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 967 | pc.printf("Press any key to continue\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 968 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 969 | Ack_Status = I2C0_dsPIC_Read_Maximum_RPM(Slave_Address, rx_array); /* Maximum RPM */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 970 | pc.printf("Maximum RPM: %05d\n\r",((rx_array[0] * 256u) + rx_array[1])); |
DavidGilesHitex | 0:5b7639d1f2c4 | 971 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 972 | do { |
DavidGilesHitex | 0:5b7639d1f2c4 | 973 | wait(0.1); |
DavidGilesHitex | 0:5b7639d1f2c4 | 974 | keypress = pc.getc(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 975 | } while (keypress != '\r'); |
DavidGilesHitex | 0:5b7639d1f2c4 | 976 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 977 | return Ack_Status; |
DavidGilesHitex | 0:5b7639d1f2c4 | 978 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 979 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 980 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 981 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 982 | /* Read The Difference and overshoot of the rotary encoder on the Slave */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 983 | static sint32_t i2C_cs_read_Rotary_Encoder(void) |
DavidGilesHitex | 0:5b7639d1f2c4 | 984 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 985 | sint32_t Ack_Status = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 986 | uint16_t keypress = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 987 | uint8_t rx_array[4] = {0u, 0u, 0u, 0u}; |
DavidGilesHitex | 0:5b7639d1f2c4 | 988 | sint32_t quad_encoder_difference = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 989 | uint32_t temp32 = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 990 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 991 | pc.printf("\n\rReading the Rotary Encoder Difference/Overshoot from the target drive\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 992 | pc.printf("Press any key to continue\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 993 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 994 | do { |
DavidGilesHitex | 0:5b7639d1f2c4 | 995 | Ack_Status = I2C0_dsPIC_Read_Quad_Encoder_Counter(Slave_Address, rx_array); |
DavidGilesHitex | 0:5b7639d1f2c4 | 996 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 997 | quad_encoder_difference = rx_array[0]; |
DavidGilesHitex | 0:5b7639d1f2c4 | 998 | quad_encoder_difference = (quad_encoder_difference << 8u); |
DavidGilesHitex | 0:5b7639d1f2c4 | 999 | quad_encoder_difference = (quad_encoder_difference + rx_array[1]); |
DavidGilesHitex | 0:5b7639d1f2c4 | 1000 | quad_encoder_difference = (quad_encoder_difference << 8u); |
DavidGilesHitex | 0:5b7639d1f2c4 | 1001 | quad_encoder_difference = (quad_encoder_difference + rx_array[2]); |
DavidGilesHitex | 0:5b7639d1f2c4 | 1002 | quad_encoder_difference = (quad_encoder_difference << 8u); |
DavidGilesHitex | 0:5b7639d1f2c4 | 1003 | quad_encoder_difference = (quad_encoder_difference + rx_array[3]); |
DavidGilesHitex | 0:5b7639d1f2c4 | 1004 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1005 | temp32 = ((~quad_encoder_difference) + 1u); |
DavidGilesHitex | 0:5b7639d1f2c4 | 1006 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1007 | pc.printf("\rDifference/Overshoot:"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 1008 | if (quad_encoder_difference == 0u) |
DavidGilesHitex | 0:5b7639d1f2c4 | 1009 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 1010 | pc.printf(" %010d", quad_encoder_difference); |
DavidGilesHitex | 0:5b7639d1f2c4 | 1011 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 1012 | else if(quad_encoder_difference > 0u) |
DavidGilesHitex | 0:5b7639d1f2c4 | 1013 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 1014 | pc.printf("+%010d", quad_encoder_difference); |
DavidGilesHitex | 0:5b7639d1f2c4 | 1015 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 1016 | else |
DavidGilesHitex | 0:5b7639d1f2c4 | 1017 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 1018 | pc.printf("-%010d", temp32); |
DavidGilesHitex | 0:5b7639d1f2c4 | 1019 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 1020 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1021 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1022 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1023 | wait(0.1); |
DavidGilesHitex | 0:5b7639d1f2c4 | 1024 | keypress = pc.getc(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 1025 | } while (keypress != '\r'); |
DavidGilesHitex | 0:5b7639d1f2c4 | 1026 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1027 | return Ack_Status; |
DavidGilesHitex | 0:5b7639d1f2c4 | 1028 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 1029 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1030 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1031 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1032 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1033 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1034 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1035 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1036 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1037 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1038 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1039 | /* Execute a sequence of instruction */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 1040 | static void i2C_cs_execute_sequence(void) |
DavidGilesHitex | 0:5b7639d1f2c4 | 1041 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 1042 | sint32_t Ack_Status = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 1043 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1044 | pc.printf("\n\n\rTransmitting data From Master I2C to Slave..."); |
DavidGilesHitex | 0:5b7639d1f2c4 | 1045 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1046 | Ack_Status = i2C_cs_ping(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 1047 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1048 | if (Ack_Status == ACK) |
DavidGilesHitex | 0:5b7639d1f2c4 | 1049 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 1050 | pc.printf("Setting up the slave motor parameters...\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 1051 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1052 | i2C_cs_set_new_direction_forward(); /* Motor direction forward */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 1053 | i2C_cs_set_ramp_up_speed(5u); /* Set the Ramp up speed */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 1054 | i2C_cs_set_ramp_down_speed(25u); /* Set the ramp down speed */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 1055 | i2C_cs_set_motor_demand_forward(400u); /* Set the target speed forward */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 1056 | i2C_cs_set_rotary_encoder_counts(50000u); /* Set the number of rotary encoder countes we want to move */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 1057 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1058 | i2C_cs_start_rotation(); /* Start Rotation */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 1059 | pc.printf("Slave Motor is running...\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 1060 | i2C_cs_read_status_flags(); /* Display the fault/status information */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 1061 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1062 | i2C_cs_set_rotary_encoder_counts(150000ul); /* Set the number of rotary encoder countes we want to move */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 1063 | i2C_cs_set_motor_demand_forward(750u); /* Increase the target speed forward */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 1064 | i2C_cs_start_rotation(); /* Start Rotation */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 1065 | i2C_cs_read_status_flags(); /* Display the fault/status information */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 1066 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1067 | pc.printf("Reading some of the motor characteristics...\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 1068 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1069 | i2C_cs_read_RPMs(); /* Display The RPM Figures from the slave */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 1070 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1071 | i2C_cs_read_motor_currents(); /* Display The Motor Currents */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 1072 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1073 | i2C_cs_read_Vbus_voltages(); /* Display the Vbus voltage */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 1074 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1075 | i2C_cs_read_demand_speed_potRPM(); /* Display the demand pot */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 1076 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1077 | i2C_cs_read_status_flags(); /* Display the fault information */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 1078 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1079 | pc.printf("Stopping the motor\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 1080 | i2C_cs_normal_stop(); /* Soft Stop motor rotation */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 1081 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1082 | pc.printf("Press any key to skip to next section\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 1083 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1084 | pc.getc(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 1085 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1086 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1087 | pc.printf("Setting the new direction Reverse/Counter clockwise\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 1088 | i2C_cs_set_new_direction_reverse(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 1089 | i2C_cs_set_motor_demand_reverse(1023u); /* Demand speed in reverse */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 1090 | i2C_cs_set_rotary_encoder_counts(40250ul); /* Set the number of rotary encoder countes we want to move */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 1091 | i2C_cs_start_rotation(); /* Start the motor rotating */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 1092 | i2C_cs_read_status_flags(); /* Display the fault/status information */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 1093 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1094 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1095 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1096 | pc.printf("Press any key to skip to next section\n\r"); |
DavidGilesHitex | 0:5b7639d1f2c4 | 1097 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1098 | pc.getc(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 1099 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1100 | pc.printf("\n\rTests Complete\n\r"); /* Tests Complete */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 1101 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 1102 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1103 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 1104 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1105 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1106 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1107 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1108 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1109 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1110 | /* Get a single number (0-9) from the host PC terminal */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 1111 | static uint8_t get_a_number_digit(void) |
DavidGilesHitex | 0:5b7639d1f2c4 | 1112 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 1113 | sint8_t temp8 = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 1114 | uint8_t break_flag = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 1115 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1116 | do { |
DavidGilesHitex | 0:5b7639d1f2c4 | 1117 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1118 | temp8 = pc.getc(); /* Get a character first of all */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 1119 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1120 | if ((temp8 > 0x2fu) && (temp8 < 0x3au)) |
DavidGilesHitex | 0:5b7639d1f2c4 | 1121 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 1122 | break_flag = 1u; /* Check for valid number digit 0-9 */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 1123 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 1124 | if (temp8 == '\r') |
DavidGilesHitex | 0:5b7639d1f2c4 | 1125 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 1126 | break_flag = 1u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 1127 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 1128 | } while (break_flag == 0u); |
DavidGilesHitex | 0:5b7639d1f2c4 | 1129 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1130 | /* We have valid number key press */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 1131 | pc.printf("%c", temp8); |
DavidGilesHitex | 0:5b7639d1f2c4 | 1132 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1133 | if (temp8 != '\r') |
DavidGilesHitex | 0:5b7639d1f2c4 | 1134 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 1135 | temp8 = temp8 - 48; /* convert to number from ASCII to decimal */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 1136 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 1137 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1138 | return temp8; /* Retrun with a number or '\r' */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 1139 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 1140 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1141 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1142 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1143 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1144 | /* Get a complete number 32 bit from the host terminal */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 1145 | static uint32_t get_a_complete_number(void) |
DavidGilesHitex | 0:5b7639d1f2c4 | 1146 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 1147 | uint8_t n = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 1148 | uint32_t number_input = 0u; |
DavidGilesHitex | 0:5b7639d1f2c4 | 1149 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1150 | do { |
DavidGilesHitex | 0:5b7639d1f2c4 | 1151 | n = get_a_number_digit(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 1152 | if (n != '\r') |
DavidGilesHitex | 0:5b7639d1f2c4 | 1153 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 1154 | number_input = (number_input * 10u) + n; |
DavidGilesHitex | 0:5b7639d1f2c4 | 1155 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 1156 | } while (n != '\r'); |
DavidGilesHitex | 0:5b7639d1f2c4 | 1157 | return number_input; |
DavidGilesHitex | 0:5b7639d1f2c4 | 1158 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 1159 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1160 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1161 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1162 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 1163 |