-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain.bas
154 lines (117 loc) · 4.1 KB
/
train.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
;======================================================
; Electric Train on PICAXE microcontroller
; ELEC3204 Major Project
; Date: 29/05/2017
; Authors: Martin Abeleda
; Aminah Thipayawat
; Haibo Wang
; Description:
; Implementation of an electric train on
; the PICAXE microcontroller. The train has
; an open loop and a closed loop mode. In
; the open loop mode, the speed is changed
; using a potentiometer and drives forwards
; and reverse. In closed loop mode, there is
; a fixed speed setpoint.
;======================================================
setfreq M32
SYMBOL mode = b0
SYMBOL sensor = b1
SYMBOL duty = b2
SYMBOL poten = b3
SYMBOL setpoint = b4
SYMBOL MODE_BUTTON = pinC.3
SYMBOL STOP_BUTTON = pinC.4
SYMBOL SPEED_SENSOR = B.1
SYMBOL POTENTIOMETER = B.2
SYMBOL OPEN_LED = B.3
SYMBOL CLOSED_LED = B.4
setup:
let mode = 0
let setpoint = 95
gosub openLoopSetup
FVRSETUP FVR4096
adcconfig %001 ; Vref- is 0V, Vref+ is V+
main:
IF MODE_BUTTON = 1 THEN
pause 500
IF MODE_BUTTON = 1 THEN
gosub stateChange
ENDIF
ENDIF
IF STOP_BUTTON = 1 THEN
pause 500
IF STOP_BUTTON = 1 THEN
gosub eStop
ENDIF
ENDIF
IF mode = 0 THEN ; open loop state
goto openLoop
ELSEIF mode = 1 THEN ; closed loop state
goto closedLoop
ELSEIF mode = 2 THEN
goto stopState
ENDIF
openLoop:
pause 100 ; wait for 0.5 s
readadc POTENTIOMETER, poten ; read potentiometer
IF poten >= 0 AND poten < 51 THEN
let duty = 191 ; slow reverse
ELSEIF poten >= 51 AND poten < 102 THEN
let duty = 160 ; stop
ELSEIF poten >= 102 AND poten < 153 THEN
let duty = 127 ; slow forward
ELSEIF poten >= 153 AND poten < 204 THEN
let duty = 95 ; medium forward
ELSEIF poten >= 204 AND poten < 255 THEN
let duty = 47 ; fast forward
ENDIF
hpwm 1, 0, 0, 79, duty ; complementary pwm at 100kHz
goto main
closedLoop:
pause 300 ; wait for 0.5 s
readadc SPEED_SENSOR, sensor ; implement feedback from sensor
sertxd(#sensor, " ", #duty, cr, lf) ; debugging
if sensor > setpoint AND duty < 159 THEN ; if sensed speed is too slow
inc duty ; slow down motor
endif
if sensor < setpoint AND duty > 5 THEN ; if sensed speed is too fast
dec duty ; speed up motor
endif
if sensor = setpoint THEN ; if the speed is correct
let duty = duty ; use the dame duty cycle
endif
hpwm 1, 0, 0, 79, duty ; set pwm
goto main
stopState:
sertxd("E-Stop", cr, lf)
high OPEN_LED ; change state LEDs
high CLOSED_LED
goto main
closedLoopSetup:
let duty = 95 ; initialise duty cycle
low OPEN_LED ; change state LEDs
high CLOSED_LED
hpwm 1, 0, 0, 79, duty ; complementary pwm at 100kHz
return
openLoopSetup:
high OPEN_LED ; change state LEDs
low CLOSED_LED
return
stateChange:
IF mode = 0 THEN
let mode = 1
gosub closedLoopSetup
ELSEIF mode = 1 THEN
let mode = 0
gosub openLoopSetup
ELSEIF mode = 2 THEN
let mode = 0
gosub openLoopSetup
ENDIF
return
eStop:
let mode = 2
let duty = 160
hpwm 1, 0, 0, 79, duty
return