Solenoid Engine Single – RPI – C – with speed keys

soleng1.c
Code for the Solenoid Engine for a Raspberry PI with log output and faster/slower/quit keys save as soleng1.c

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
/*
#######################################################
# Sol engine V1 #
# compile with gcc soleng1.c -o soleng1 -lwiringPi #
# Ext Kits 4/11/16 #
#######################################################
*/
 
#include <stdio.h>
#include <wiringPi.h>
#include "conio.h"
#include <stdlib.h>
 
//define pins
#define NOTEYE 7
#define SOL 0
#define MAXSPEED 100
#define MINSPEED 2000
#define LOGFILE "sollog.txt"
#define DEBUG 0
#define STATSSEC 5
 
 int StatTimer;
 int Speed=400;
 int Revs=0;
 int Pulses=0;
 int StatCnt=0;
 
 void DoStats(void);
 void DoButtons(void);
 void AppendtoFile( char *text);
 
 
int main (void)
{
 
 int ison;
 int last_ison;
 int ready=1;
 int pulsetime;
 wiringPiSetup () ;
 
 remove(LOGFILE);
 
 pinMode (NOTEYE, INPUT) ;
 pinMode (SOL, OUTPUT) ;
 
 set_conio_terminal_mode();
 StatTimer=millis();
 
 
 for (;;)
 {
//get eye status
 //ison=!digitalRead(NOTEYE); //V1
 ison=digitalRead(NOTEYE); //V2
//start of stroke check speed
    if(ison==1 && last_ison==0){
      //had a pulse
      if (millis()-pulsetime>Speed){
        ready=1;
      }
      Revs++;
    }
 
//if pulse needed turn on solenoid
    if(ready){
      digitalWrite (SOL, ison) ;
      pulsetime=millis();
      if (ison==0){ready=0;Pulses++;}
    }else{
      digitalWrite (SOL, 0) ;
    }
 
//debug
  if(DEBUG){
    printf("ison=%i ready=%i Speed=%i pt=%i mills=%i\n\r",ison,ready,Speed,millis()-pulsetime,millis());
  }
//set last condition
    last_ison=ison;
 
//speed buttons
  DoButtons();
 
//do Stats
  DoStats();
  }
 
 
  return 0 ;
}
 
void DoButtons(){
  char c;
 
//speed buttons
  if(kbhit()){
    c=getch();
    if(c=='q'){exit(0);}
    if(c=='8'){if(Speed>MAXSPEED){Speed--;printf("Speed Up %i\n\r",Speed);}}
    if(c=='2'){if(Speed<MINSPEED){Speed++;printf("Speed Down %i\n\r",Speed);}}
 
  }
}
 
void DoStats(void){
 if (millis()-StatTimer>STATSSEC*1000){
 StatTimer=millis();
 if(DEBUG){ printf("Do Stats \n\r");}
 char text[100];
 int rpm=0;
 rpm=Revs*60/STATSSEC;
 StatCnt++;
 sprintf(text,"CNT:%i REVS:%i PULSES:%i RPM:%i \n\r",StatCnt,Revs,Pulses,rpm);
 
 AppendtoFile(text);
 Pulses=0;
 Revs=0;
 }
}
 
void AppendtoFile( char *text){
 FILE *log;
 char filename[100] = LOGFILE;
 log = fopen (filename, "a");
 
 fprintf(log,"%s",text);
 fclose(log);
}

You will also need this header file conio.h

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
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#include <termios.h>
 
struct termios orig_termios;
 
void reset_terminal_mode()
{
 tcsetattr(0, TCSANOW, &orig_termios);
}
 
void set_conio_terminal_mode()
{
 struct termios new_termios;
 
 /* take two copies - one for now, one for later */
 tcgetattr(0, &orig_termios);
 memcpy(&new_termios, &orig_termios, sizeof(new_termios));
 
 /* register cleanup handler, and set the new terminal mode */
 atexit(reset_terminal_mode);
 cfmakeraw(&new_termios);
 tcsetattr(0, TCSANOW, &new_termios);
}
 
int kbhit()
{
 struct timeval tv = { 0L, 0L };
 fd_set fds;
 FD_ZERO(&fds);
 FD_SET(0, &fds);
 return select(1, &fds, NULL, NULL, &tv);
}
 
int getch()
{
 int r;
 unsigned char c;
 if ((r = read(0, &c, sizeof(c))) < 0) {
 return r;
 } else {
 return c;
 }
}