DoubleCoilGun

Dependents:   Telliskivi2_2014

Fork of CoilGun by Reiko Randoja

Committer:
Reiko
Date:
Sat Sep 17 11:50:59 2016 +0000
Revision:
12:7cc6aeaa4bf8
Parent:
11:49959d33916f
Shorter discharge kicks

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Reiko 6:4c75db8a43db 1 #include "coilgun.h"
Reiko 5:0e061c4013a2 2
Reiko 10:7518047a0375 3 Coilgun::Coilgun(PinName kickPinName, PinName chipPinName, PinName chargePinName, PinName donePinName):
Reiko 10:7518047a0375 4 kickPin(kickPinName), chipPin(chipPinName), chargePin(chargePinName), donePin(donePinName)
Reiko 6:4c75db8a43db 5 {
mlaane 8:eee78d8bfdb9 6 isCharged = false;
Reiko 6:4c75db8a43db 7 state = idle;
Reiko 10:7518047a0375 8 kickPin = 0;
Reiko 10:7518047a0375 9 chipPin = 0;
Reiko 6:4c75db8a43db 10 chargePin = 0;
Reiko 11:49959d33916f 11 donePin.fall(this, &Coilgun::chargeEnd);
mlaane 8:eee78d8bfdb9 12
Reiko 10:7518047a0375 13 currentKickLength = 0;
Reiko 10:7518047a0375 14 currentKickDelay = 0;
Reiko 10:7518047a0375 15 currentChipLength = 0;
Reiko 10:7518047a0375 16 currentChipDelay = 0;
Reiko 10:7518047a0375 17
Reiko 10:7518047a0375 18 kickActive = false;
Reiko 10:7518047a0375 19 chipActive = false;
Reiko 10:7518047a0375 20
Reiko 10:7518047a0375 21 //discharge(); // For safety reasons.
Reiko 6:4c75db8a43db 22 }
Reiko 6:4c75db8a43db 23
Reiko 10:7518047a0375 24 void Coilgun::kick(unsigned int kickLength, unsigned int kickDelay, unsigned int chipLength, unsigned int chipDelay) {
Reiko 10:7518047a0375 25 kick(kickLength, kickDelay, chipLength, chipDelay, true);
Reiko 6:4c75db8a43db 26 }
Reiko 6:4c75db8a43db 27
Reiko 10:7518047a0375 28 void Coilgun::kick(unsigned int kickLength, unsigned int kickDelay, unsigned int chipLength, unsigned int chipDelay, bool change_state = true) {
Reiko 7:95d16e38d0d8 29 if (change_state) {//Used to keep state when discharging
Reiko 6:4c75db8a43db 30 changeState(kicking);
Reiko 7:95d16e38d0d8 31 }
Reiko 5:0e061c4013a2 32
Reiko 10:7518047a0375 33 currentKickLength = kickLength;
Reiko 10:7518047a0375 34 currentKickDelay = kickDelay;
Reiko 10:7518047a0375 35 currentChipLength = chipLength;
Reiko 10:7518047a0375 36 currentChipDelay = chipDelay;
Reiko 10:7518047a0375 37
Reiko 6:4c75db8a43db 38 chargePin = 0; // Maybe not needed anymore? (still a safety?)
Reiko 10:7518047a0375 39
Reiko 10:7518047a0375 40 kickActive = true;
Reiko 10:7518047a0375 41 chipActive = true;
Reiko 10:7518047a0375 42
Reiko 10:7518047a0375 43 if (currentKickDelay > 0) {
Reiko 10:7518047a0375 44 kickDelayTimeout.attach_us(this, &Coilgun::kickDelayEnd, currentKickDelay);
Reiko 10:7518047a0375 45 } else {
Reiko 10:7518047a0375 46 kickDelayEnd();
Reiko 10:7518047a0375 47 }
Reiko 10:7518047a0375 48
Reiko 10:7518047a0375 49 if (currentChipDelay > 0) {
Reiko 10:7518047a0375 50 chipDelayTimeout.attach_us(this, &Coilgun::chipDelayEnd, currentChipDelay);
Reiko 10:7518047a0375 51 } else {
Reiko 10:7518047a0375 52 chipDelayEnd();
Reiko 10:7518047a0375 53 }
Reiko 10:7518047a0375 54
Reiko 10:7518047a0375 55 }
Reiko 10:7518047a0375 56
Reiko 10:7518047a0375 57 void Coilgun::kickDelayEnd(void) {
Reiko 10:7518047a0375 58 kickDelayTimeout.detach();
Reiko 10:7518047a0375 59
Reiko 10:7518047a0375 60 if (currentKickLength > 0) {
Reiko 10:7518047a0375 61 kickPin = 1;
Reiko 10:7518047a0375 62 kickTimeout.attach_us(this, &Coilgun::kickEnd, currentKickLength);
Reiko 10:7518047a0375 63 } else {
Reiko 10:7518047a0375 64 kickEnd();
Reiko 10:7518047a0375 65 }
Reiko 6:4c75db8a43db 66 }
Reiko 0:2d5820be7f51 67
Reiko 6:4c75db8a43db 68 void Coilgun::kickEnd(void) {
Reiko 6:4c75db8a43db 69 kickTimeout.detach();
Reiko 10:7518047a0375 70 kickPin = 0;
Reiko 10:7518047a0375 71 kickActive = false;
Reiko 10:7518047a0375 72
Reiko 10:7518047a0375 73 if (!chipActive && state != discharging){ //Used when discharging
Reiko 12:7cc6aeaa4bf8 74 chargePin = 1; // autocharge
Reiko 10:7518047a0375 75 state = idle; //(let state stay "discharging")
Reiko 10:7518047a0375 76 }
Reiko 10:7518047a0375 77 }
Reiko 6:4c75db8a43db 78
Reiko 10:7518047a0375 79 void Coilgun::chipDelayEnd(void) {
Reiko 10:7518047a0375 80 chipDelayTimeout.detach();
Reiko 10:7518047a0375 81
Reiko 10:7518047a0375 82 if (currentChipLength > 0) {
Reiko 10:7518047a0375 83 chipPin = 1;
Reiko 10:7518047a0375 84 chipTimeout.attach_us(this, &Coilgun::chipEnd, currentChipLength);
Reiko 10:7518047a0375 85 } else {
Reiko 10:7518047a0375 86 chipEnd();
Reiko 10:7518047a0375 87 }
Reiko 10:7518047a0375 88 }
Reiko 10:7518047a0375 89
Reiko 10:7518047a0375 90 void Coilgun::chipEnd(void) {
Reiko 10:7518047a0375 91 chipTimeout.detach();
Reiko 10:7518047a0375 92 chipPin = 0;
Reiko 10:7518047a0375 93 chipActive = false;
Reiko 10:7518047a0375 94
Reiko 10:7518047a0375 95 if (!kickActive && state != discharging){ //Used when discharging
Reiko 12:7cc6aeaa4bf8 96 chargePin = 1; // autocharge
Reiko 6:4c75db8a43db 97 state = idle; //(let state stay "discharging")
Reiko 5:0e061c4013a2 98 }
Reiko 5:0e061c4013a2 99
Reiko 6:4c75db8a43db 100 }
Reiko 6:4c75db8a43db 101
Reiko 6:4c75db8a43db 102 void Coilgun::charge() {
Reiko 6:4c75db8a43db 103 changeState(charging);
Reiko 10:7518047a0375 104
Reiko 10:7518047a0375 105 kickPin = 0;
Reiko 10:7518047a0375 106 chipPin = 0;
Reiko 10:7518047a0375 107 kickActive = false;
Reiko 10:7518047a0375 108 chipActive = false;
Reiko 10:7518047a0375 109
Reiko 6:4c75db8a43db 110 chargePin = 1;
mlaane 8:eee78d8bfdb9 111 isCharged = true;
Reiko 6:4c75db8a43db 112 }
Reiko 6:4c75db8a43db 113
Reiko 6:4c75db8a43db 114 void Coilgun::chargeEnd(){
Reiko 6:4c75db8a43db 115 chargePin = 0;
Reiko 6:4c75db8a43db 116 state = idle;
Reiko 6:4c75db8a43db 117 }
Reiko 0:2d5820be7f51 118
Reiko 6:4c75db8a43db 119 void Coilgun::discharge() {
Reiko 6:4c75db8a43db 120 changeState(discharging);
Reiko 6:4c75db8a43db 121 dischargeTimeout.attach(this, &Coilgun::dischargeEnd, 10.0); // End discharging after 10 seconds
Reiko 11:49959d33916f 122 discharger.attach_us(this, &Coilgun::dischargeKick, 50000); // calls short kick every 50ms
mlaane 8:eee78d8bfdb9 123 isCharged = false;
Reiko 6:4c75db8a43db 124 }
Reiko 7:95d16e38d0d8 125
Reiko 6:4c75db8a43db 126 void Coilgun::dischargeEnd(void) {
Reiko 6:4c75db8a43db 127 discharger.detach();
Reiko 6:4c75db8a43db 128 state = idle;
Reiko 6:4c75db8a43db 129 }
Reiko 7:95d16e38d0d8 130
Reiko 6:4c75db8a43db 131 void Coilgun::dischargeKick(void) {
Reiko 12:7cc6aeaa4bf8 132 kick(200, 0, 0, 0, false);
Reiko 6:4c75db8a43db 133 }
Reiko 6:4c75db8a43db 134
Reiko 6:4c75db8a43db 135 void Coilgun::changeState(State new_state){
Reiko 6:4c75db8a43db 136 switch(state){
Reiko 6:4c75db8a43db 137 case idle:
Reiko 7:95d16e38d0d8 138 //nothing to end.
Reiko 7:95d16e38d0d8 139 break;
Reiko 6:4c75db8a43db 140 case kicking:
Reiko 7:95d16e38d0d8 141 kickEnd();
Reiko 10:7518047a0375 142 chipEnd();
Reiko 7:95d16e38d0d8 143 break;
Reiko 6:4c75db8a43db 144 case charging:
Reiko 7:95d16e38d0d8 145 chargeEnd();
Reiko 7:95d16e38d0d8 146 break;
Reiko 6:4c75db8a43db 147 case discharging:
Reiko 7:95d16e38d0d8 148 dischargeEnd();
Reiko 7:95d16e38d0d8 149 break;
Reiko 6:4c75db8a43db 150 default:
Reiko 7:95d16e38d0d8 151 //error - no such state
Reiko 7:95d16e38d0d8 152 break;
Reiko 5:0e061c4013a2 153 }
Reiko 6:4c75db8a43db 154 //set new state
Reiko 6:4c75db8a43db 155 state = new_state;
Reiko 6:4c75db8a43db 156 }