myWRIO
C++ framework for NationalInstruments myRIO
main.cpp
Go to the documentation of this file.
1 #include "MyRIO.h"
2 #include <math.h>
3 
4 using namespace myRIO;
5 using namespace std;
6 
7 double map(double x, double in_min, double in_max, double out_min, double out_max) {
8  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
9 }
10 
11 int main() {
12  if(!myRIO_init()) {cout << "Error initializing myRIO"; return -1;}
13 
14  Motor motorLeft(PWMA1, CCW, 8.823);
15  Motor motorRight(PWMA0, CW, 8.831);
16 
17  PWM buzzer(PWMA2, 300, 0);
18  PWM ledForce(PWMB1, 10e3, 0);
19  PWM ledProx(PWMB2, 10e3, 0);
20 
21  MotorPID motorLeftPid(0.95, 40);
22  MotorPID motorRightPid(0.75, 35);
23 
24  const long sampleTimeUs = 500;
25 
26  Wifi w([&](long data) {
27  double motorLeftSpeed = static_cast<char>((data&0xFF00)>>8);
28  double motorRightSpeed = static_cast<char>(data&0xFF);
29 
30  if(motorLeftSpeed > 127) motorLeftSpeed -= 255;
31  if(motorRightSpeed > 127) motorRightSpeed -= 255;
32 
33  motorLeftSpeed = map(motorLeftSpeed, -128, 127, -850, 850);
34  motorRightSpeed = map(motorRightSpeed, -128, 127, -850, 850);
35 
36  if(abs((int)ceil(motorLeftSpeed))<50) motorLeftSpeed = 0;
37  if(abs((int)ceil(motorRightSpeed))<50) motorRightSpeed = 0;
38 
39 
40  motorLeftPid.setSetpoint(motorLeftSpeed);
41  motorRightPid.setSetpoint(motorRightSpeed);
42 
43  DIO::writeLed(LED0, data&0x40000000);
44  DIO::writeLed(LED1, data&0x20000000);
45  DIO::writeLed(LED2, data&0x10000000);
46  DIO::writeLed(LED3, data&0x08000000);
47 
48  if(data&0x08000000)
49  buzzer.setDutyCycle(50);
50  else
51  buzzer.setDutyCycle(0);
52  });
53  //while(!w.isConnected());
54 
58  while(1) {
59  long encL = motorLeft.getEncoderPulses();
60  if(motorLeft.getDefaultDirection()==CCW) encL = - encL;
61  double correctedL = motorLeftPid.compute(encL);
62  motorLeft.setAngularSpeedAndDirection(correctedL);
63 
64  long encR = motorRight.getEncoderPulses();
65  if(motorRight.getDefaultDirection()==CCW) encR = - encR;
66  double correctedR = motorRightPid.compute(encR);
67  motorRight.setAngularSpeedAndDirection(correctedR);
68 
69  Time::wait_us(sampleTimeUs);
70 
71 
72  double force = AIO::readPin(AI0);
73  double dutyCycle = map(force, 0, 5, 0, 100);
74  if(dutyCycle>100) dutyCycle = 100;
75  ledForce.setDutyCycle(dutyCycle);
76 
77  double proxi = AIO::readPin(BI2);
78  proxi = 27.726*pow(proxi, -1.2045);
79  dutyCycle = map(proxi, 10, 100, 100, 0);
80  if(dutyCycle>100) dutyCycle = 100;
81  if(dutyCycle<0) dutyCycle = 0;
82  ledProx.setDutyCycle(dutyCycle);
83  }
84 
85  return 0;
86 }
87 
88 
89 
void setAngularSpeedAndDirection(double speed)
Set the speed in angular speed and the direction.
Definition: Motor.cpp:60
void setDutyCycle(double dutyCycle)
Set the Duty Cycle.
Definition: PWM.cpp:103
This class can be used to regulate a motor speed.
Definition: MotorPID.h:14
static void writeLed(int led, bool state)
Write to a led.
Definition: DIO.cpp:12
Pin PWMB_2.
Definition: PWM.h:25
Pin PWMB_1.
Definition: PWM.h:24
bool getDefaultDirection()
Get the default direction of the motor.
Definition: Motor.cpp:81
void setSetpoint(double setpoint)
Set the setpoint.
Definition: MotorPID.cpp:22
Definition: AIO.h:24
Use this class to exchange data via wifi.
Definition: Wifi.h:18
long getEncoderPulses()
Get the Encoder values.
Definition: Motor.cpp:29
double compute(long enc)
Refresh the PID output.
Definition: MotorPID.cpp:38
Definition: AIO.h:30
int main()
Definition: main.cpp:11
Pin PWMA_2.
Definition: PWM.h:22
double map(double x, double in_min, double in_max, double out_min, double out_max)
Definition: main.cpp:7
static double readPin(int pin)
Read an analog pin.
Definition: AIO.cpp:34
Outputs a PWM signal (3.3V)
Definition: PWM.h:33
bool myRIO_init()
Definition: MyRIO.cpp:7
static void wait_us(long int us)
Wait function in microseconds.
Definition: Time.cpp:85
Pin PWMA_0.
Definition: PWM.h:20
Pin PWMA_1.
Definition: PWM.h:21
Definition: Acc.h:8
Combination of PWM and Encoder to work with motors.
Definition: Motor.h:22