![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
First Commit
Dependencies: mbed Crypto_light mbed-rtos
Spin it 2 win it
main.cpp
- Committer:
- TrebleStick
- Date:
- 2018-03-20
- Revision:
- 20:a435105305fe
- Parent:
- 19:526fd700e1b3
- Child:
- 21:0d1025dc6433
File content as of revision 20:a435105305fe:
#include "mbed.h" #include "Crypto_light/hash/SHA256.h" #include "mbed-rtos/rtos/rtos.h" //Photointerrupter input pins #define I1pin D2 #define I2pin D11 #define I3pin D12 //Incremental encoder input pins #define CHA D7 #define CHB D8 //Motor Drive output pins //Mask in output byte #define L1Lpin D4 //0x01 #define L1Hpin D5 //0x02 #define L2Lpin D3 //0x04 #define L2Hpin D6 //0x08 #define L3Lpin D9 //0x10 #define L3Hpin D10 //0x20 #define CHAR_ARR_SIZE 18 //Max length of input codes #define MAX_PWM_PERIOD 2000 #define MAX_TORQUE 1000 //Mapping from sequential drive states to motor phase outputs /* State L1 L2 L3 0 H - L 1 - H L 2 L H - 3 L - H 4 - L H 5 H L - 6 - - - 7 - - - */ //Drive state to output table const int8_t driveTable[] = {0x12,0x18,0x09,0x21,0x24,0x06,0x00,0x00}; //Mapping from interrupter inputs to sequential rotor states. 0x00 and 0x07 are not valid const int8_t stateMap[] = {0x07,0x05,0x03,0x04,0x01,0x00,0x02,0x07}; //const int8_t stateMap[] = {0x07,0x01,0x03,0x02,0x05,0x00,0x04,0x07}; //Alternative if phase order of input or drive is reversed //Phase lead to make motor spin const int8_t lead = 2; //2 for forwards, -2 for backwards //Rotor offset at motor state 0 volatile int8_t orState = 0; //Set initial torque of 1000 volatile uint32_t torque = 1000; enum MSG {MSG_RESET, MSG_HASHCOUNT, MSG_NONCE_OK, MSG_OVERFLOW, MSG_ROT_PEN, MSG_MAX_SPD, MSG_NEW_KEY, MSG_INP_ERR, MSG_TORQUE, MSG_TEST, MSG_CUR_SPD}; //Instantiate the serial port RawSerial pc(SERIAL_TX, SERIAL_RX); //Status LED DigitalOut led1(LED1); //Photointerrupter inputs InterruptIn I1(I1pin); InterruptIn I2(I2pin); InterruptIn I3(I3pin); //Motor Drive outputs PwmOut L1L(L1Lpin); DigitalOut L1H(L1Hpin); PwmOut L2L(L2Lpin); DigitalOut L2H(L2Hpin); PwmOut L3L(L3Lpin); DigitalOut L3H(L3Hpin); typedef struct { uint8_t code; int32_t data; } message_t ; Mail<message_t,16> outMessages; void putMessage(uint8_t code, int32_t data) {//uint64_t message_t *pMessage = outMessages.alloc(); pMessage->code = code; pMessage->data = data; outMessages.put(pMessage); } Thread commOutT (osPriorityNormal, 512); void commOutFn() { while(1) { osEvent newEvent = outMessages.get(); message_t *pMessage = (message_t*)newEvent.value.p; pc.printf("Message: [%d], data: 0x%08x\r\n", pMessage->code,pMessage->data); outMessages.free(pMessage); } } //Global varible for the Bitcoin Key,maxspeed and rotations_pending volatile uint64_t newKey = 0; //check initialise value? **** volatile float maxspeed = 0, rotations_pending = 0; //mutex variables Mutex newKey_mutex; Mutex maxspeed_mutex; Mutex rotations_pending_mutex; //Instantiate a Queue to buffer incoming characters Queue<void, 8> inCharQ; //serial port ISR to receive each incoming byte and place into queue void serialISR() { uint8_t newChar = pc.getc(); inCharQ.put((void*)newChar); } Thread motorCtrlT (osPriorityNormal, 2048); int8_t readRotorState(); void motorOut(int8_t,uint32_t); volatile int32_t motorPosition; void photointerrupter_isr() { static int8_t oldRotorState = 0; int8_t rotorState = readRotorState(); motorOut((rotorState-orState+lead+6)%6, torque); //+6 to make sure the remainder is positive if (rotorState - oldRotorState == 5) motorPosition--; else if (rotorState - oldRotorState == -5) motorPosition++; else motorPosition += (rotorState - oldRotorState); oldRotorState = rotorState; } //TODO Implement timer for greater accuracy void motorCtrlTick(){ motorCtrlT.signal_set(0x1); } float currentSpeed = 0; void motorCtrlFn(){ int32_t posStart, posEnd; Ticker motorCtrlTicker; motorCtrlTicker.attach_us(&motorCtrlTick,100000); // putMessage(MSG_TEST, 0x505); uint8_t cnt = 0; while(1){ posStart = motorPosition; motorCtrlT.signal_wait(0x1); posEnd = motorPosition; currentSpeed = (posEnd-posStart)*10; if(cnt >= 9) { // putMessage(MSG_CUR_SPD, currentSpeed); cnt = 0; } else{ cnt++; } } } void setNewCmd(char s[CHAR_ARR_SIZE]) { uint64_t newKey_; uint32_t torque_; float maxspeed_, rotations_pending_; //R if (sscanf(s, "R%f", &rotations_pending_)) { rotations_pending_mutex.lock(); rotations_pending = rotations_pending_; rotations_pending_mutex.unlock(); putMessage(MSG_ROT_PEN, rotations_pending); //V } else if (sscanf(s, "V%f", &maxspeed_)) { maxspeed_mutex.lock(); maxspeed = maxspeed_; maxspeed_mutex.unlock(); putMessage(MSG_MAX_SPD, maxspeed); //K } else if (sscanf(s, "K%llx", &newKey_)) { newKey_mutex.lock(); newKey = newKey_; newKey_mutex.unlock(); putMessage(MSG_NEW_KEY, newKey); } else if (sscanf(s, "T%u", &torque_)) { torque = torque_; photointerrupter_isr(); //Give it a kick putMessage(MSG_TORQUE, torque); //ERROR } else{ putMessage(MSG_INP_ERR, 0x404); } } Thread decodeT (osPriorityNormal, 512); void decodeFn() { pc.attach(&serialISR); char charSeq[CHAR_ARR_SIZE] = ""; uint8_t bufPos = 0; while(1) { if(bufPos >= CHAR_ARR_SIZE) { putMessage(MSG_OVERFLOW, bufPos); bufPos = 0; } else{ osEvent newEvent = inCharQ.get(); uint8_t newChar = (uint8_t)newEvent.value.p; if(newChar == '\r' || newChar == '\n') { charSeq[bufPos] = '\0'; bufPos = 0; setNewCmd(charSeq); } else { charSeq[bufPos] = newChar; bufPos++; } } } } volatile uint16_t hashcount = 0; void do_hashcount() { //putMessage(MSG_HASHCOUNT, hashcount); hashcount = 0; } //Set a given drive state void motorOut(int8_t driveState, uint32_t t){ //Lookup the output byte from the drive state. int8_t driveOut = driveTable[driveState & 0x07]; //Turn off first if (~driveOut & 0x01) L1L.pulsewidth_us(0); if (~driveOut & 0x02) L1H = 1; if (~driveOut & 0x04) L2L.pulsewidth_us(0); if (~driveOut & 0x08) L2H = 1; if (~driveOut & 0x10) L3L.pulsewidth_us(0); if (~driveOut & 0x20) L3H = 1; //Then turn on if (driveOut & 0x01) L1L.pulsewidth_us(t); if (driveOut & 0x02) L1H = 0; if (driveOut & 0x04) L2L.pulsewidth_us(t); if (driveOut & 0x08) L2H = 0; if (driveOut & 0x10) L3L.pulsewidth_us(t); if (driveOut & 0x20) L3H = 0; } //Convert photointerrupter inputs to a rotor state inline int8_t readRotorState(){ return stateMap[I1 + 2*I2 + 4*I3]; } //Basic synchronisation routine int8_t motorHome() { //Put the motor in drive state 0 and wait for it to stabilise motorOut(0, MAX_TORQUE); wait(2.0); //Get the rotor state return readRotorState(); } //Main int main() { putMessage(MSG_RESET, 0); commOutT.start(&commOutFn); motorCtrlT.start(&motorCtrlFn); decodeT.start(&decodeFn); //Run the motor synchronisation orState = motorHome(); //orState is subtracted from future rotor state inputs to align rotor and motor states I1.rise(&photointerrupter_isr); I2.rise(&photointerrupter_isr); I3.rise(&photointerrupter_isr); I1.fall(&photointerrupter_isr); I2.fall(&photointerrupter_isr); I3.fall(&photointerrupter_isr); L1L.period_us(MAX_PWM_PERIOD); L2L.period_us(MAX_PWM_PERIOD); L3L.period_us(MAX_PWM_PERIOD); //Calling the ISR once starts the motor movement photointerrupter_isr(); SHA256 sha256; uint8_t sequence[] = {0x45,0x6D,0x62,0x65,0x64,0x64,0x65,0x64, 0x20,0x53,0x79,0x73,0x74,0x65,0x6D,0x73, 0x20,0x61,0x72,0x65,0x20,0x66,0x75,0x6E, 0x20,0x61,0x6E,0x64,0x20,0x64,0x6F,0x20, 0x61,0x77,0x65,0x73,0x6F,0x6D,0x65,0x20, 0x74,0x68,0x69,0x6E,0x67,0x73,0x21,0x20, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; uint64_t* key = (uint64_t*)((int)sequence + 48); uint64_t* nonce = (uint64_t*)((int)sequence + 56); uint8_t hash[32]; Ticker hashcounter; hashcounter.attach(&do_hashcount, 1.0); //Poll the rotor state and set the motor outputs accordingly to spin the motor while (1) { *key = newKey; sha256.computeHash(hash, sequence, 64); if (hash[0] == 0 && hash[1] == 0) { //putMessage(MSG_NONCE_OK, *nonce); } (*nonce)++; hashcount++; } }